SUMO - Simulation of Urban MObility
ROPerson.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // A vehicle as used by router
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2002-2016 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
35 #include <utils/common/ToString.h>
41 #include <string>
42 #include <iostream>
43 #include "RORouteDef.h"
44 #include "ROPerson.h"
45 #include "RORoute.h"
46 #include "ROVehicle.h"
47 #include "ROHelper.h"
48 #include "RONet.h"
49 
50 #ifdef CHECK_MEMORY_LEAKS
51 #include <foreign/nvwa/debug_new.h>
52 #endif // CHECK_MEMORY_LEAKS
53 
54 
55 // ===========================================================================
56 // method definitions
57 // ===========================================================================
59  : RORoutable(pars, type) {
60 }
61 
62 
64  for (std::vector<PlanItem*>::const_iterator it = myPlan.begin(); it != myPlan.end(); ++it) {
65  delete *it;
66  }
67 }
68 
69 
70 void
71 ROPerson::addTrip(const ROEdge* const from, const ROEdge* const to, const SVCPermissions modeSet,
72  const std::string& vTypes, const SUMOReal departPos, const SUMOReal arrivalPos, const std::string& busStop) {
73  PersonTrip* trip = new PersonTrip(from, to, modeSet, departPos, arrivalPos, busStop);
74  RONet* net = RONet::getInstance();
77 
78  for (StringTokenizer st(vTypes); st.hasNext();) {
79  pars.vtypeid = st.next();
82  if (type == 0) {
83  throw InvalidArgument("The vehicle type '" + pars.vtypeid + "' in a trip for person '" + getID() + "' is not known.");
84  }
85  pars.id = getID() + "_" + toString(trip->getVehicles().size());
86  trip->addVehicle(new ROVehicle(pars, new RORouteDef("!" + pars.id, 0, false, false), type, net));
87  }
88  if ((modeSet & SVC_PASSENGER) != 0 && trip->getVehicles().empty()) {
89  pars.id = getID() + "_0";
90  trip->addVehicle(new ROVehicle(pars, new RORouteDef("!" + pars.id, 0, false, false), net->getVehicleTypeSecure(DEFAULT_VTYPE_ID), net));
91  }
92  myPlan.push_back(trip);
93 }
94 
95 
96 void
97 ROPerson::addRide(const ROEdge* const from, const ROEdge* const to, const std::string& lines, const std::string& destStop) {
98  if (myPlan.empty() || myPlan.back()->isStop()) {
99  myPlan.push_back(new PersonTrip());
100  }
101  myPlan.back()->addTripItem(new Ride(from, to, lines, destStop));
102 }
103 
104 
105 void
106 ROPerson::addWalk(const ConstROEdgeVector& edges, const SUMOReal duration, const SUMOReal speed, const SUMOReal departPos, const SUMOReal arrivalPos, const std::string& busStop) {
107  if (myPlan.empty() || myPlan.back()->isStop()) {
108  myPlan.push_back(new PersonTrip());
109  }
110  myPlan.back()->addTripItem(new Walk(edges, duration, speed, departPos, arrivalPos, busStop));
111 }
112 
113 
114 void
115 ROPerson::addStop(const SUMOVehicleParameter::Stop& stopPar, const ROEdge* const stopEdge) {
116  myPlan.push_back(new Stop(stopPar, stopEdge));
117 }
118 
119 
120 void
123  if (from != 0) {
124  os.writeAttr(SUMO_ATTR_FROM, from->getID());
125  }
126  if (to != 0) {
127  os.writeAttr(SUMO_ATTR_TO, to->getID());
128  }
129  if (destStop != "") {
130  os.writeAttr(SUMO_ATTR_BUS_STOP, destStop);
131  }
132  os.writeAttr(SUMO_ATTR_LINES, lines);
133  os.closeTag();
134 }
135 
136 
137 void
140  if (dur > 0) {
141  os.writeAttr(SUMO_ATTR_DURATION, dur);
142  }
143  if (v > 0) {
144  os.writeAttr(SUMO_ATTR_SPEED, v);
145  }
146  os.writeAttr(SUMO_ATTR_EDGES, edges);
147  if (dep != std::numeric_limits<SUMOReal>::infinity()) {
149  }
150  if (arr != std::numeric_limits<SUMOReal>::infinity()) {
152  }
153  if (destStop != "") {
154  os.writeAttr(SUMO_ATTR_BUS_STOP, destStop);
155  }
156  os.closeTag();
157 }
158 
159 
160 void
161 ROPerson::PersonTrip::saveVehicles(OutputDevice& os, OutputDevice* const typeos, bool asAlternatives, OptionsCont& options) const {
162  for (std::vector<ROVehicle*>::const_iterator it = myVehicles.begin(); it != myVehicles.end(); ++it) {
163  (*it)->saveAsXML(os, typeos, asAlternatives, options);
164  }
165 }
166 
167 
168 bool
169 ROPerson::computeIntermodal(const RORouterProvider& provider, PersonTrip* const trip, const ROVehicle* const veh, MsgHandler* const errorHandler) {
170  std::vector<ROIntermodalRouter::TripItem> result;
171  provider.getIntermodalRouter().compute(trip->getOrigin(), trip->getDestination(), trip->getDepartPos(), trip->getArrivalPos(),
172  myType->maxSpeed, veh, trip->getModes(), 0, result);
173  bool carUsed = false;
174  for (std::vector<ROIntermodalRouter::TripItem>::const_iterator it = result.begin(); it != result.end(); ++it) {
175  if (!it->edges.empty()) {
176  if (it->line == "") {
177  trip->addTripItem(new Walk(it->edges, it->destStop));
178  } else if (veh != 0 && it->line == veh->getID()) {
179  trip->addTripItem(new Ride(it->edges.front(), it->edges.back(), veh->getID(), it->destStop));
180  veh->getRouteDefinition()->addLoadedAlternative(new RORoute(veh->getID() + "_RouteDef", it->edges));
181  carUsed = true;
182  } else {
183  trip->addTripItem(new Ride(0, 0, it->line, it->destStop));
184  }
185  }
186  }
187  if (result.empty()) {
188  errorHandler->inform("No route for trip in person '" + getID() + "'.");
189  myRoutingSuccess = false;
190  }
191  return carUsed;
192 }
193 
194 
195 void
197  const bool /* removeLoops */, MsgHandler* errorHandler) {
198  myRoutingSuccess = true;
199  for (std::vector<PlanItem*>::iterator it = myPlan.begin(); it != myPlan.end(); ++it) {
200  if ((*it)->needsRouting()) {
201  PersonTrip* trip = static_cast<PersonTrip*>(*it);
202  ConstROEdgeVector edges;
203  std::vector<ROVehicle*>& vehicles = trip->getVehicles();
204  if (vehicles.empty()) {
205  computeIntermodal(provider, trip, 0, errorHandler);
206  } else {
207  for (std::vector<ROVehicle*>::iterator v = vehicles.begin(); v != vehicles.end();) {
208  if (!computeIntermodal(provider, trip, *v, errorHandler)) {
209  v = vehicles.erase(v);
210  } else {
211  ++v;
212  }
213  }
214  }
215  }
216  }
217 }
218 
219 
220 void
221 ROPerson::saveAsXML(OutputDevice& os, OutputDevice* const typeos, bool asAlternatives, OptionsCont& options) const {
222  // write the person's vehicles
223  for (std::vector<PlanItem*>::const_iterator it = myPlan.begin(); it != myPlan.end(); ++it) {
224  (*it)->saveVehicles(os, typeos, asAlternatives, options);
225  }
226 
227  if (typeos != 0 && myType != 0 && !myType->saved) {
228  myType->write(*typeos);
229  myType->saved = true;
230  }
231  if (myType != 0 && !myType->saved) {
232  myType->write(os);
233  myType->saved = asAlternatives;
234  }
235 
236  // write the person
237  myParameter.write(os, options, SUMO_TAG_PERSON);
238 
239  for (std::vector<PlanItem*>::const_iterator it = myPlan.begin(); it != myPlan.end(); ++it) {
240  (*it)->saveAsXML(os);
241  }
242 
243  for (std::map<std::string, std::string>::const_iterator j = myParameter.getMap().begin(); j != myParameter.getMap().end(); ++j) {
245  os.writeAttr(SUMO_ATTR_KEY, (*j).first);
246  os.writeAttr(SUMO_ATTR_VALUE, (*j).second);
247  os.closeTag();
248  }
249  os.closeTag();
250 }
251 
252 
253 /****************************************************************************/
254 
The departure is person triggered.
SUMOVehicleParameter myParameter
The vehicle&#39;s parameter.
Definition: RORoutable.h:165
void addRide(const ROEdge *const from, const ROEdge *const to, const std::string &lines, const std::string &destStop)
Definition: ROPerson.cpp:97
bool compute(const E *from, const E *to, SUMOReal departPos, SUMOReal arrivalPos, SUMOReal 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...
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:257
std::string vtypeid
The vehicle&#39;s type id.
void addVehicle(ROVehicle *veh)
Definition: ROPerson.h:231
SUMOReal getArrivalPos() const
Definition: ROPerson.h:246
void saveAsXML(OutputDevice &os) const
Definition: ROPerson.cpp:121
Structure representing possible vehicle parameter.
bool saved
Information whether this type was already saved (needed by routers)
int SVCPermissions
A planItem can be a Stop.
Definition: ROPerson.h:109
void addWalk(const ConstROEdgeVector &edges, const SUMOReal duration, const SUMOReal speed, const SUMOReal departPos, const SUMOReal arrivalPos, const std::string &busStop)
Definition: ROPerson.cpp:106
bool computeIntermodal(const RORouterProvider &provider, PersonTrip *const trip, const ROVehicle *const veh, MsgHandler *const errorHandler)
Definition: ROPerson.cpp:169
const ROEdge * getDestination() const
Definition: ROPerson.h:240
SUMOReal getDepartPos() const
Definition: ROPerson.h:243
const std::map< std::string, std::string > & getMap() const
Returns the inner key/value map.
std::vector< const ROEdge * > ConstROEdgeVector
Definition: ROEdge.h:62
bool myRoutingSuccess
Whether the last routing was successful.
Definition: RORoutable.h:171
const std::string DEFAULT_VTYPE_ID
A planItem can be a Trip which contains multiple tripItems.
Definition: ROPerson.h:214
static RONet * getInstance()
Returns the pointer to the unique instance of RONet (singleton).
Definition: RONet.cpp:66
A routable thing such as a vehicle or person.
Definition: RORoutable.h:62
A ride is part of a trip, e.g., go from here to here by car or bus.
Definition: ROPerson.h:154
A vehicle as used by router.
Definition: ROVehicle.h:60
the edges of a route
std::vector< PlanItem * > myPlan
The plan of the person.
Definition: ROPerson.h:316
void addStop(const SUMOVehicleParameter::Stop &stopPar, const ROEdge *const stopEdge)
Definition: ROPerson.cpp:115
void addTrip(const ROEdge *const from, const ROEdge *const to, const SVCPermissions modeSet, const std::string &vTypes, const SUMOReal departPos, const SUMOReal arrivalPos, const std::string &busStop)
Definition: ROPerson.cpp:71
const ROEdge * getOrigin() const
Definition: ROPerson.h:237
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
Writes the parameters as a beginning element.
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:54
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:184
A basic edge for routing applications.
Definition: ROEdge.h:77
virtual ~ROPerson()
Destructor.
Definition: ROPerson.cpp:63
ROPerson(const SUMOVehicleParameter &pars, const SUMOVTypeParameter *type)
Constructor.
Definition: ROPerson.cpp:58
void write(OutputDevice &dev) const
Writes the vtype.
SUMOReal maxSpeed
The vehicle type&#39;s maximum speed [m/s].
SVCPermissions getModes() const
Definition: ROPerson.h:249
const SUMOVTypeParameter *const myType
The type of the vehicle.
Definition: RORoutable.h:168
void computeRoute(const RORouterProvider &provider, const bool removeLoops, MsgHandler *errorHandler)
Definition: ROPerson.cpp:196
The router&#39;s network representation.
Definition: RONet.h:76
Structure representing possible vehicle parameter.
int setParameter
Information for the router which parameter were set.
void saveAsXML(OutputDevice &os, OutputDevice *const typeos, bool asAlternatives, OptionsCont &options) const
Saves the complete person description.
Definition: ROPerson.cpp:221
const std::string & getID() const
Returns the id of the vehicle.
Definition: RORoutable.h:92
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:89
A storage for options typed value containers)
Definition: OptionsCont.h:108
Base class for a vehicle&#39;s route definition.
Definition: RORouteDef.h:63
void addLoadedAlternative(RORoute *alternative)
Adds a single alternative loaded from the file An alternative may also be generated during DUA...
Definition: RORouteDef.cpp:80
const int VEHPARS_VTYPE_SET
IntermodalRouter< E, L, N, V > & getIntermodalRouter() const
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
std::vector< ROVehicle * > & getVehicles()
Definition: ROPerson.h:234
virtual void addTripItem(TripItem *tripIt)
Definition: ROPerson.h:228
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:213
SUMOVTypeParameter * getVehicleTypeSecure(const std::string &id)
Retrieves the named vehicle type.
Definition: RONet.cpp:255
void saveAsXML(OutputDevice &os) const
Definition: ROPerson.cpp:138
RORouteDef * getRouteDefinition() const
Returns the definition of the route the vehicle takes.
Definition: ROVehicle.h:83
void saveVehicles(OutputDevice &os, OutputDevice *const typeos, bool asAlternatives, OptionsCont &options) const
Definition: ROPerson.cpp:161
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
A complete router&#39;s route.
Definition: RORoute.h:62
std::string id
The vehicle&#39;s id.