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-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
18 // A vehicle as used by router
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
28 #include <utils/common/ToString.h>
34 #include <string>
35 #include <iostream>
36 #include "RORouteDef.h"
37 #include "ROPerson.h"
38 #include "RORoute.h"
39 #include "ROVehicle.h"
40 #include "ROHelper.h"
41 #include "RONet.h"
42 
43 
44 // ===========================================================================
45 // method definitions
46 // ===========================================================================
48  : RORoutable(pars, type) {
49 }
50 
51 
53  for (std::vector<PlanItem*>::const_iterator it = myPlan.begin(); it != myPlan.end(); ++it) {
54  delete *it;
55  }
56 }
57 
58 
59 void
60 ROPerson::addTrip(const ROEdge* const from, const ROEdge* const to, const SVCPermissions modeSet,
61  const std::string& vTypes, const double departPos, const double arrivalPos, const std::string& busStop, double walkFactor) {
62  PersonTrip* trip = new PersonTrip(from, to, modeSet, departPos, arrivalPos, busStop, walkFactor);
63  RONet* net = RONet::getInstance();
66 
67  for (StringTokenizer st(vTypes); st.hasNext();) {
68  pars.vtypeid = st.next();
71  if (type == nullptr) {
72  delete trip;
73  throw InvalidArgument("The vehicle type '" + pars.vtypeid + "' in a trip for person '" + getID() + "' is not known.");
74  }
75  pars.id = getID() + "_" + toString(trip->getVehicles().size());
76  trip->addVehicle(new ROVehicle(pars, new RORouteDef("!" + pars.id, 0, false, false), type, net));
77  }
78  if (trip->getVehicles().empty()) {
79  if ((modeSet & SVC_PASSENGER) != 0) {
80  pars.id = getID() + "_0";
81  trip->addVehicle(new ROVehicle(pars, new RORouteDef("!" + pars.id, 0, false, false), net->getVehicleTypeSecure(DEFAULT_VTYPE_ID), net));
82  }
83  if ((modeSet & SVC_BICYCLE) != 0) {
84  pars.id = getID() + "_b0";
87  trip->addVehicle(new ROVehicle(pars, new RORouteDef("!" + pars.id, 0, false, false), net->getVehicleTypeSecure(DEFAULT_BIKETYPE_ID), net));
88  }
89  }
90  myPlan.push_back(trip);
91 }
92 
93 
94 void
95 ROPerson::addRide(const ROEdge* const from, const ROEdge* const to, const std::string& lines, double arrivalPos, const std::string& destStop) {
96  if (myPlan.empty() || myPlan.back()->isStop()) {
97  myPlan.push_back(new PersonTrip());
98  }
99  myPlan.back()->addTripItem(new Ride(from, to, lines, -1., arrivalPos, destStop));
100 }
101 
102 
103 void
104 ROPerson::addWalk(const ConstROEdgeVector& edges, const double duration, const double speed, const double departPos, const double arrivalPos, const std::string& busStop) {
105  if (myPlan.empty() || myPlan.back()->isStop()) {
106  myPlan.push_back(new PersonTrip());
107  }
108  myPlan.back()->addTripItem(new Walk(edges, -1., duration, speed, departPos, arrivalPos, busStop));
109 }
110 
111 
112 void
113 ROPerson::addStop(const SUMOVehicleParameter::Stop& stopPar, const ROEdge* const stopEdge) {
114  myPlan.push_back(new Stop(stopPar, stopEdge));
115 }
116 
117 
118 void
119 ROPerson::Ride::saveAsXML(OutputDevice& os, const bool extended) const {
121  std::string comment = "";
122  if (extended && cost >= 0.) {
123  os.writeAttr(SUMO_ATTR_COST, cost);
124  }
125  if (from != nullptr) {
126  os.writeAttr(SUMO_ATTR_FROM, from->getID());
127  }
128  if (to != nullptr) {
129  os.writeAttr(SUMO_ATTR_TO, to->getID());
130  }
131  if (destStop != "") {
132  os.writeAttr(SUMO_ATTR_BUS_STOP, destStop);
133  const std::string name = RONet::getInstance()->getStoppingPlaceName(destStop);
134  if (name != "") {
135  comment = " <!-- " + name + " -->";
136  }
137  }
138  os.writeAttr(SUMO_ATTR_LINES, lines);
139  if (intended != "" && intended != lines) {
140  os.writeAttr(SUMO_ATTR_INTENDED, intended);
141  }
142  if (depart >= 0) {
144  }
145  os.closeTag(comment);
146 }
147 
148 
149 void
150 ROPerson::Walk::saveAsXML(OutputDevice& os, const bool extended) const {
152  std::string comment = "";
153  if (extended && cost >= 0.) {
154  os.writeAttr(SUMO_ATTR_COST, cost);
155  }
156  if (dur > 0) {
157  os.writeAttr(SUMO_ATTR_DURATION, dur);
158  }
159  if (v > 0) {
160  os.writeAttr(SUMO_ATTR_SPEED, v);
161  }
162  os.writeAttr(SUMO_ATTR_EDGES, edges);
163  if (dep != 0.) {
165  }
166  if (arr != 0.) {
168  }
169  if (destStop != "") {
170  os.writeAttr(SUMO_ATTR_BUS_STOP, destStop);
171  const std::string name = RONet::getInstance()->getStoppingPlaceName(destStop);
172  if (name != "") {
173  comment = " <!-- " + name + " -->";
174  }
175  }
176  os.closeTag(comment);
177 }
178 
179 
180 void
181 ROPerson::PersonTrip::saveVehicles(OutputDevice& os, OutputDevice* const typeos, bool asAlternatives, OptionsCont& options) const {
182  for (std::vector<ROVehicle*>::const_iterator it = myVehicles.begin(); it != myVehicles.end(); ++it) {
183  (*it)->saveAsXML(os, typeos, asAlternatives, options);
184  }
185 }
186 
187 
188 bool
189 ROPerson::computeIntermodal(const RORouterProvider& provider, PersonTrip* const trip, const ROVehicle* const veh, MsgHandler* const errorHandler) {
190  std::vector<ROIntermodalRouter::TripItem> result;
191  provider.getIntermodalRouter().compute(trip->getOrigin(), trip->getDestination(), trip->getDepartPos(), trip->getArrivalPos(), trip->getStopDest(),
192  getType()->maxSpeed * trip->getWalkFactor(), veh, trip->getModes(), getParameter().depart, result);
193  bool carUsed = false;
194  for (std::vector<ROIntermodalRouter::TripItem>::const_iterator it = result.begin(); it != result.end(); ++it) {
195  if (!it->edges.empty()) {
196  if (it->line == "") {
197  if (it + 1 == result.end() && trip->getStopDest() == "") {
198  trip->addTripItem(new Walk(it->edges, it->cost, trip->getDepartPos(false), trip->getArrivalPos(false)));
199  } else {
200  trip->addTripItem(new Walk(it->edges, it->cost, trip->getDepartPos(false), trip->getArrivalPos(false), it->destStop));
201  }
202  } else if (veh != nullptr && it->line == veh->getID()) {
203  trip->addTripItem(new Ride(it->edges.front(), it->edges.back(), veh->getID(), it->cost, trip->getArrivalPos(), it->destStop));
204  veh->getRouteDefinition()->addLoadedAlternative(new RORoute(veh->getID() + "_RouteDef", it->edges));
205  carUsed = true;
206  } else {
207  trip->addTripItem(new Ride(nullptr, nullptr, it->line, it->cost, trip->getArrivalPos(), it->destStop, it->intended, TIME2STEPS(it->depart)));
208  }
209  }
210  }
211  if (result.empty()) {
212  errorHandler->inform("No route for trip in person '" + getID() + "'.");
213  myRoutingSuccess = false;
214  }
215  return carUsed;
216 }
217 
218 
219 void
221  const bool /* removeLoops */, MsgHandler* errorHandler) {
222  myRoutingSuccess = true;
223  for (std::vector<PlanItem*>::iterator it = myPlan.begin(); it != myPlan.end(); ++it) {
224  if ((*it)->needsRouting()) {
225  PersonTrip* trip = static_cast<PersonTrip*>(*it);
226  std::vector<ROVehicle*>& vehicles = trip->getVehicles();
227  if (vehicles.empty()) {
228  computeIntermodal(provider, trip, nullptr, errorHandler);
229  } else {
230  for (std::vector<ROVehicle*>::iterator v = vehicles.begin(); v != vehicles.end();) {
231  if (!computeIntermodal(provider, trip, *v, errorHandler)) {
232  v = vehicles.erase(v);
233  } else {
234  ++v;
235  }
236  }
237  }
238  }
239  }
240 }
241 
242 
243 void
244 ROPerson::saveAsXML(OutputDevice& os, OutputDevice* const typeos, bool asAlternatives, OptionsCont& options) const {
245  // write the person's vehicles
246  for (std::vector<PlanItem*>::const_iterator it = myPlan.begin(); it != myPlan.end(); ++it) {
247  (*it)->saveVehicles(os, typeos, asAlternatives, options);
248  }
249 
250  if (typeos != nullptr && getType() != nullptr && !getType()->saved) {
251  getType()->write(*typeos);
252  getType()->saved = true;
253  }
254  if (getType() != nullptr && !getType()->saved) {
255  getType()->write(os);
256  getType()->saved = asAlternatives;
257  }
258 
259  // write the person
260  getParameter().write(os, options, SUMO_TAG_PERSON);
261 
262  for (std::vector<PlanItem*>::const_iterator it = myPlan.begin(); it != myPlan.end(); ++it) {
263  (*it)->saveAsXML(os, asAlternatives);
264  }
265 
266  // write params
268  os.closeTag();
269 }
270 
271 
272 /****************************************************************************/
273 
The departure is person triggered.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:256
IntermodalRouter< E, L, N, V > & getIntermodalRouter() const
SVCPermissions getModes() const
Definition: ROPerson.h:285
std::string vtypeid
The vehicle&#39;s type id.
void addVehicle(ROVehicle *veh)
Definition: ROPerson.h:260
Structure representing possible vehicle parameter.
const SUMOVehicleParameter & getParameter() const
Returns the definition of the vehicle / person parameter.
Definition: RORoutable.h:74
bool saved
Information whether this type was already saved (needed by routers)
vehicle is a bicycle
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:105
int parametersSet
Information for the router which parameter were set, TraCI may modify this (whe changing color) ...
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:65
bool computeIntermodal(const RORouterProvider &provider, PersonTrip *const trip, const ROVehicle *const veh, MsgHandler *const errorHandler)
Definition: ROPerson.cpp:189
void saveAsXML(OutputDevice &os, OutputDevice *const typeos, bool asAlternatives, OptionsCont &options) const
Saves the complete person description.
Definition: ROPerson.cpp:244
const std::string DEFAULT_BIKETYPE_ID
std::vector< const ROEdge * > ConstROEdgeVector
Definition: ROEdge.h:56
bool myRoutingSuccess
Whether the last routing was successful.
Definition: RORoutable.h:182
#define TIME2STEPS(x)
Definition: SUMOTime.h:60
const std::string DEFAULT_VTYPE_ID
A planItem can be a Trip which contains multiple tripItems.
Definition: ROPerson.h:239
static RONet * getInstance()
Returns the pointer to the unique instance of RONet (singleton).
Definition: RONet.cpp:55
void saveAsXML(OutputDevice &os, const bool extended) const
Definition: ROPerson.cpp:150
A routable thing such as a vehicle or person.
Definition: RORoutable.h:55
A ride is part of a trip, e.g., go from here to here by car or bus.
Definition: ROPerson.h:159
A vehicle as used by router.
Definition: ROVehicle.h:53
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:49
void saveVehicles(OutputDevice &os, OutputDevice *const typeos, bool asAlternatives, OptionsCont &options) const
Definition: ROPerson.cpp:181
std::vector< PlanItem * > myPlan
The plan of the person.
Definition: ROPerson.h:360
void addStop(const SUMOVehicleParameter::Stop &stopPar, const ROEdge *const stopEdge)
Definition: ROPerson.cpp:113
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:94
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:203
A basic edge for routing applications.
Definition: ROEdge.h:72
RORouteDef * getRouteDefinition() const
Returns the definition of the route the vehicle takes.
Definition: ROVehicle.h:76
const std::string & getStopDest() const
Definition: ROPerson.h:288
virtual ~ROPerson()
Destructor.
Definition: ROPerson.cpp:52
void writeParams(OutputDevice &device) const
write Params in the given outputdevice
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:60
ROPerson(const SUMOVehicleParameter &pars, const SUMOVTypeParameter *type)
Constructor.
Definition: ROPerson.cpp:47
void saveAsXML(OutputDevice &os, const bool extended) const
Definition: ROPerson.cpp:119
double getWalkFactor() const
Definition: ROPerson.h:300
void write(OutputDevice &dev) const
Writes the vtype.
void computeRoute(const RORouterProvider &provider, const bool removeLoops, MsgHandler *errorHandler)
Definition: ROPerson.cpp:220
The router&#39;s network representation.
Definition: RONet.h:68
Structure representing possible vehicle parameter.
const SUMOVTypeParameter * getType() const
Returns the type of the routable.
Definition: RORoutable.h:85
double getArrivalPos(bool replaceDefault=true) const
Definition: ROPerson.h:282
void addWalk(const ConstROEdgeVector &edges, const double duration, const double speed, const double departPos, const double arrivalPos, const std::string &busStop)
Definition: ROPerson.cpp:104
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:113
A storage for options typed value containers)
Definition: OptionsCont.h:92
Base class for a vehicle&#39;s route definition.
Definition: RORouteDef.h:56
const std::string getStoppingPlaceName(const std::string &id) const
return the name for the given stopping place id
Definition: RONet.cpp:702
void addLoadedAlternative(RORoute *alternative)
Adds a single alternative loaded from the file An alternative may also be generated during DUA...
Definition: RORouteDef.cpp:69
double getDepartPos(bool replaceDefault=true) const
Definition: ROPerson.h:279
void addRide(const ROEdge *const from, const ROEdge *const to, const std::string &lines, double arrivalPos, const std::string &destStop)
Definition: ROPerson.cpp:95
const int VEHPARS_VTYPE_SET
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:64
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
std::vector< ROVehicle * > & getVehicles()
Definition: ROPerson.h:263
virtual void addTripItem(TripItem *tripIt)
Definition: ROPerson.h:257
SUMOVTypeParameter * getVehicleTypeSecure(const std::string &id)
Retrieves the named vehicle type.
Definition: RONet.cpp:277
const ROEdge * getOrigin() const
Definition: ROPerson.h:266
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
A complete router&#39;s route.
Definition: RORoute.h:55
std::string id
The vehicle&#39;s id.
const ROEdge * getDestination() const
Definition: ROPerson.h:269