SUMO - Simulation of Urban MObility
ROPerson.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2002-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
20 // A vehicle as used by router
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
34 #include <utils/common/ToString.h>
40 #include <string>
41 #include <iostream>
42 #include "RORouteDef.h"
43 #include "ROPerson.h"
44 #include "RORoute.h"
45 #include "ROVehicle.h"
46 #include "ROHelper.h"
47 #include "RONet.h"
48 
49 
50 // ===========================================================================
51 // method definitions
52 // ===========================================================================
54  : RORoutable(pars, type) {
55 }
56 
57 
59  for (std::vector<PlanItem*>::const_iterator it = myPlan.begin(); it != myPlan.end(); ++it) {
60  delete *it;
61  }
62 }
63 
64 
65 void
66 ROPerson::addTrip(const ROEdge* const from, const ROEdge* const to, const SVCPermissions modeSet,
67  const std::string& vTypes, const double departPos, const double arrivalPos, const std::string& busStop, double walkFactor) {
68  PersonTrip* trip = new PersonTrip(from, to, modeSet, departPos, arrivalPos, busStop, walkFactor);
69  RONet* net = RONet::getInstance();
72 
73  for (StringTokenizer st(vTypes); st.hasNext();) {
74  pars.vtypeid = st.next();
77  if (type == 0) {
78  delete trip;
79  throw InvalidArgument("The vehicle type '" + pars.vtypeid + "' in a trip for person '" + getID() + "' is not known.");
80  }
81  pars.id = getID() + "_" + toString(trip->getVehicles().size());
82  trip->addVehicle(new ROVehicle(pars, new RORouteDef("!" + pars.id, 0, false, false), type, net));
83  }
84  if ((modeSet & SVC_PASSENGER) != 0 && trip->getVehicles().empty()) {
85  pars.id = getID() + "_0";
86  trip->addVehicle(new ROVehicle(pars, new RORouteDef("!" + pars.id, 0, false, false), net->getVehicleTypeSecure(DEFAULT_VTYPE_ID), net));
87  }
88  myPlan.push_back(trip);
89 }
90 
91 
92 void
93 ROPerson::addRide(const ROEdge* const from, const ROEdge* const to, const std::string& lines, const std::string& destStop) {
94  if (myPlan.empty() || myPlan.back()->isStop()) {
95  myPlan.push_back(new PersonTrip());
96  }
97  myPlan.back()->addTripItem(new Ride(from, to, lines, -1., destStop));
98 }
99 
100 
101 void
102 ROPerson::addWalk(const ConstROEdgeVector& edges, const double duration, const double speed, const double departPos, const double arrivalPos, const std::string& busStop) {
103  if (myPlan.empty() || myPlan.back()->isStop()) {
104  myPlan.push_back(new PersonTrip());
105  }
106  myPlan.back()->addTripItem(new Walk(edges, -1., duration, speed, departPos, arrivalPos, busStop));
107 }
108 
109 
110 void
111 ROPerson::addStop(const SUMOVehicleParameter::Stop& stopPar, const ROEdge* const stopEdge) {
112  myPlan.push_back(new Stop(stopPar, stopEdge));
113 }
114 
115 
116 void
117 ROPerson::Ride::saveAsXML(OutputDevice& os, const bool extended) const {
119  if (extended && cost >= 0.) {
120  os.writeAttr(SUMO_ATTR_COST, cost);
121  }
122  if (from != 0) {
123  os.writeAttr(SUMO_ATTR_FROM, from->getID());
124  }
125  if (to != 0) {
126  os.writeAttr(SUMO_ATTR_TO, to->getID());
127  }
128  if (destStop != "") {
129  os.writeAttr(SUMO_ATTR_BUS_STOP, destStop);
130  }
131  os.writeAttr(SUMO_ATTR_LINES, lines);
132  os.closeTag();
133 }
134 
135 
136 void
137 ROPerson::Walk::saveAsXML(OutputDevice& os, const bool extended) const {
139  if (extended && cost >= 0.) {
140  os.writeAttr(SUMO_ATTR_COST, cost);
141  }
142  if (dur > 0) {
143  os.writeAttr(SUMO_ATTR_DURATION, dur);
144  }
145  if (v > 0) {
146  os.writeAttr(SUMO_ATTR_SPEED, v);
147  }
148  os.writeAttr(SUMO_ATTR_EDGES, edges);
149  if (dep != 0.) {
151  }
152  if (arr != 0.) {
154  }
155  if (destStop != "") {
156  os.writeAttr(SUMO_ATTR_BUS_STOP, destStop);
157  }
158  os.closeTag();
159 }
160 
161 
162 void
163 ROPerson::PersonTrip::saveVehicles(OutputDevice& os, OutputDevice* const typeos, bool asAlternatives, OptionsCont& options) const {
164  for (std::vector<ROVehicle*>::const_iterator it = myVehicles.begin(); it != myVehicles.end(); ++it) {
165  (*it)->saveAsXML(os, typeos, asAlternatives, options);
166  }
167 }
168 
169 
170 bool
171 ROPerson::computeIntermodal(const RORouterProvider& provider, PersonTrip* const trip, const ROVehicle* const veh, MsgHandler* const errorHandler) {
172  std::vector<ROIntermodalRouter::TripItem> result;
173  provider.getIntermodalRouter().compute(trip->getOrigin(), trip->getDestination(), trip->getDepartPos(), trip->getArrivalPos(),
174  getType()->maxSpeed * trip->getWalkFactor(), veh, trip->getModes(), getParameter().depart, result);
175  bool carUsed = false;
176  for (std::vector<ROIntermodalRouter::TripItem>::const_iterator it = result.begin(); it != result.end(); ++it) {
177  if (!it->edges.empty()) {
178  if (it->line == "") {
179  if (it + 1 == result.end() && !trip->hasBusStopDest()) {
180  trip->addTripItem(new Walk(it->edges, it->cost, trip->getDepartPos(false), trip->getArrivalPos(false)));
181  } else {
182  trip->addTripItem(new Walk(it->edges, it->cost, trip->getDepartPos(false), trip->getArrivalPos(false), it->destStop));
183  }
184  } else if (veh != 0 && it->line == veh->getID()) {
185  trip->addTripItem(new Ride(it->edges.front(), it->edges.back(), veh->getID(), it->cost, it->destStop));
186  veh->getRouteDefinition()->addLoadedAlternative(new RORoute(veh->getID() + "_RouteDef", it->edges));
187  carUsed = true;
188  } else {
189  trip->addTripItem(new Ride(0, 0, it->line, it->cost, it->destStop));
190  }
191  }
192  }
193  if (result.empty()) {
194  errorHandler->inform("No route for trip in person '" + getID() + "'.");
195  myRoutingSuccess = false;
196  }
197  return carUsed;
198 }
199 
200 
201 void
203  const bool /* removeLoops */, MsgHandler* errorHandler) {
204  myRoutingSuccess = true;
205  for (std::vector<PlanItem*>::iterator it = myPlan.begin(); it != myPlan.end(); ++it) {
206  if ((*it)->needsRouting()) {
207  PersonTrip* trip = static_cast<PersonTrip*>(*it);
208  std::vector<ROVehicle*>& vehicles = trip->getVehicles();
209  if (vehicles.empty()) {
210  computeIntermodal(provider, trip, 0, errorHandler);
211  } else {
212  for (std::vector<ROVehicle*>::iterator v = vehicles.begin(); v != vehicles.end();) {
213  if (!computeIntermodal(provider, trip, *v, errorHandler)) {
214  v = vehicles.erase(v);
215  } else {
216  ++v;
217  }
218  }
219  }
220  }
221  }
222 }
223 
224 
225 void
226 ROPerson::saveAsXML(OutputDevice& os, OutputDevice* const typeos, bool asAlternatives, OptionsCont& options) const {
227  // write the person's vehicles
228  for (std::vector<PlanItem*>::const_iterator it = myPlan.begin(); it != myPlan.end(); ++it) {
229  (*it)->saveVehicles(os, typeos, asAlternatives, options);
230  }
231 
232  if (typeos != 0 && getType() != 0 && !getType()->saved) {
233  getType()->write(*typeos);
234  getType()->saved = true;
235  }
236  if (getType() != 0 && !getType()->saved) {
237  getType()->write(os);
238  getType()->saved = asAlternatives;
239  }
240 
241  // write the person
242  getParameter().write(os, options, SUMO_TAG_PERSON);
243 
244  for (std::vector<PlanItem*>::const_iterator it = myPlan.begin(); it != myPlan.end(); ++it) {
245  (*it)->saveAsXML(os, asAlternatives);
246  }
247 
248  for (std::map<std::string, std::string>::const_iterator j = getParameter().getMap().begin(); j != getParameter().getMap().end(); ++j) {
250  os.writeAttr(SUMO_ATTR_KEY, (*j).first);
251  os.writeAttr(SUMO_ATTR_VALUE, (*j).second);
252  os.closeTag();
253  }
254  os.closeTag();
255 }
256 
257 
258 /****************************************************************************/
259 
The departure is person triggered.
void addRide(const ROEdge *const from, const ROEdge *const to, const std::string &lines, const std::string &destStop)
Definition: ROPerson.cpp:93
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:260
IntermodalRouter< E, L, N, V > & getIntermodalRouter() const
SVCPermissions getModes() const
Definition: ROPerson.h:262
std::string vtypeid
The vehicle&#39;s type id.
bool hasBusStopDest() const
Definition: ROPerson.h:265
void addVehicle(ROVehicle *veh)
Definition: ROPerson.h:244
Structure representing possible vehicle parameter.
const SUMOVehicleParameter & getParameter() const
Returns the definition of the vehicle / person parameter.
Definition: RORoutable.h:80
bool saved
Information whether this type was already saved (needed by routers)
int SVCPermissions
bitset where each bit declares whether a certain SVC may use this edge/lane
A planItem can be a Stop.
Definition: ROPerson.h:110
int parametersSet
Information for the router which parameter were set, TraCI may modify this (whe changing color) ...
bool computeIntermodal(const RORouterProvider &provider, PersonTrip *const trip, const ROVehicle *const veh, MsgHandler *const errorHandler)
Definition: ROPerson.cpp:171
void saveAsXML(OutputDevice &os, OutputDevice *const typeos, bool asAlternatives, OptionsCont &options) const
Saves the complete person description.
Definition: ROPerson.cpp:226
std::vector< const ROEdge * > ConstROEdgeVector
Definition: ROEdge.h:62
bool myRoutingSuccess
Whether the last routing was successful.
Definition: RORoutable.h:188
const std::string DEFAULT_VTYPE_ID
A planItem can be a Trip which contains multiple tripItems.
Definition: ROPerson.h:223
static RONet * getInstance()
Returns the pointer to the unique instance of RONet (singleton).
Definition: RONet.cpp:61
void saveAsXML(OutputDevice &os, const bool extended) const
Definition: ROPerson.cpp:137
A routable thing such as a vehicle or person.
Definition: RORoutable.h:61
bool compute(const E *from, const E *to, double departPos, double arrivalPos, double speed, const V *const vehicle, const SVCPermissions modeSet, SUMOTime msTime, std::vector< TripItem > &into)
Builds the route between the given edges using the minimum effort at the given time The definition of...
A ride is part of a trip, e.g., go from here to here by car or bus.
Definition: ROPerson.h:160
A vehicle as used by router.
Definition: ROVehicle.h:59
double maxSpeed
The vehicle type&#39;s maximum speed [m/s].
the edges of a route
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:55
void saveVehicles(OutputDevice &os, OutputDevice *const typeos, bool asAlternatives, OptionsCont &options) const
Definition: ROPerson.cpp:163
std::vector< PlanItem * > myPlan
The plan of the person.
Definition: ROPerson.h:337
parameter associated to a certain key
void addStop(const SUMOVehicleParameter::Stop &stopPar, const ROEdge *const stopEdge)
Definition: ROPerson.cpp:111
SUMOTime depart
The vehicle&#39;s departure time.
DepartDefinition departProcedure
Information how the vehicle shall choose the depart time.
void write(OutputDevice &dev, const OptionsCont &oc, const SumoXMLTag tag=SUMO_TAG_VEHICLE, const std::string &typeID="") const
Writes the parameters as a beginning element.
const std::string & getID() const
Returns the id of the routable.
Definition: RORoutable.h:100
vehicle is a passenger car (a "normal" car)
A walk is part of a trip, e.g., go from here to here by foot.
Definition: ROPerson.h:190
A basic edge for routing applications.
Definition: ROEdge.h:77
RORouteDef * getRouteDefinition() const
Returns the definition of the route the vehicle takes.
Definition: ROVehicle.h:82
virtual ~ROPerson()
Destructor.
Definition: ROPerson.cpp:58
void addTrip(const ROEdge *const from, const ROEdge *const to, const SVCPermissions modeSet, const std::string &vTypes, const double departPos, const double arrivalPos, const std::string &busStop, double walkFactor)
Definition: ROPerson.cpp:66
ROPerson(const SUMOVehicleParameter &pars, const SUMOVTypeParameter *type)
Constructor.
Definition: ROPerson.cpp:53
void saveAsXML(OutputDevice &os, const bool extended) const
Definition: ROPerson.cpp:117
double getWalkFactor() const
Definition: ROPerson.h:277
void write(OutputDevice &dev) const
Writes the vtype.
void computeRoute(const RORouterProvider &provider, const bool removeLoops, MsgHandler *errorHandler)
Definition: ROPerson.cpp:202
The router&#39;s network representation.
Definition: RONet.h:74
Structure representing possible vehicle parameter.
const SUMOVTypeParameter * getType() const
Returns the type of the routable.
Definition: RORoutable.h:91
double getArrivalPos(bool replaceDefault=true) const
Definition: ROPerson.h:259
void addWalk(const ConstROEdgeVector &edges, const double duration, const double speed, const double departPos, const double arrivalPos, const std::string &busStop)
Definition: ROPerson.cpp:102
const std::map< std::string, std::string > & getMap() const
Returns the inner key/value map.
Definition of vehicle stop (position and duration)
void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:84
A storage for options typed value containers)
Definition: OptionsCont.h:98
Base class for a vehicle&#39;s route definition.
Definition: RORouteDef.h:62
void addLoadedAlternative(RORoute *alternative)
Adds a single alternative loaded from the file An alternative may also be generated during DUA...
Definition: RORouteDef.cpp:75
double getDepartPos(bool replaceDefault=true) const
Definition: ROPerson.h:256
const int VEHPARS_VTYPE_SET
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:70
std::vector< ROVehicle * > & getVehicles()
Definition: ROPerson.h:247
virtual void addTripItem(TripItem *tripIt)
Definition: ROPerson.h:241
bool closeTag()
Closes the most recently opened tag.
SUMOVTypeParameter * getVehicleTypeSecure(const std::string &id)
Retrieves the named vehicle type.
Definition: RONet.cpp:277
const ROEdge * getOrigin() const
Definition: ROPerson.h:250
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
A complete router&#39;s route.
Definition: RORoute.h:61
std::string id
The vehicle&#39;s id.
const ROEdge * getDestination() const
Definition: ROPerson.h:253