Eclipse 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-2019 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 
27 #include <string>
28 #include <iostream>
30 #include <utils/common/ToString.h>
37 #include "RORouteDef.h"
38 #include "RORoute.h"
39 #include "ROVehicle.h"
40 #include "ROHelper.h"
41 #include "RONet.h"
42 #include "ROLane.h"
43 #include "ROPerson.h"
44 
45 
46 // ===========================================================================
47 // method definitions
48 // ===========================================================================
50  : RORoutable(pars, type) {
51 }
52 
53 
55  for (std::vector<PlanItem*>::const_iterator it = myPlan.begin(); it != myPlan.end(); ++it) {
56  delete *it;
57  }
58 }
59 
60 
61 void
62 ROPerson::addTrip(const ROEdge* const from, const ROEdge* const to, const SVCPermissions modeSet,
63  const std::string& vTypes, const double departPos, const double arrivalPos, const std::string& busStop, double walkFactor) {
64  PersonTrip* trip = new PersonTrip(from, to, modeSet, departPos, arrivalPos, busStop, walkFactor);
65  RONet* net = RONet::getInstance();
68  if (departPos != 0) {
70  pars.departPos = departPos;
72  }
73  for (StringTokenizer st(vTypes); st.hasNext();) {
74  pars.vtypeid = st.next();
77  if (type == nullptr) {
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 (trip->getVehicles().empty()) {
85  if ((modeSet & SVC_PASSENGER) != 0) {
86  pars.id = getID() + "_0";
87  trip->addVehicle(new ROVehicle(pars, new RORouteDef("!" + pars.id, 0, false, false), net->getVehicleTypeSecure(DEFAULT_VTYPE_ID), net));
88  }
89  if ((modeSet & SVC_BICYCLE) != 0) {
90  pars.id = getID() + "_b0";
93  trip->addVehicle(new ROVehicle(pars, new RORouteDef("!" + pars.id, 0, false, false), net->getVehicleTypeSecure(DEFAULT_BIKETYPE_ID), net));
94  }
95  }
96  myPlan.push_back(trip);
97 }
98 
99 
100 void
101 ROPerson::addRide(const ROEdge* const from, const ROEdge* const to, const std::string& lines, double arrivalPos, const std::string& destStop) {
102  if (myPlan.empty() || myPlan.back()->isStop()) {
103  myPlan.push_back(new PersonTrip());
104  }
105  myPlan.back()->addTripItem(new Ride(from, to, lines, -1., arrivalPos, destStop));
106 }
107 
108 
109 void
110 ROPerson::addWalk(const ConstROEdgeVector& edges, const double duration, const double speed, const double departPos, const double arrivalPos, const std::string& busStop) {
111  if (myPlan.empty() || myPlan.back()->isStop()) {
112  myPlan.push_back(new PersonTrip());
113  }
114  myPlan.back()->addTripItem(new Walk(edges, -1., duration, speed, departPos, arrivalPos, busStop));
115 }
116 
117 
118 void
119 ROPerson::addStop(const SUMOVehicleParameter::Stop& stopPar, const ROEdge* const stopEdge) {
120  myPlan.push_back(new Stop(stopPar, stopEdge));
121 }
122 
123 
124 void
125 ROPerson::Ride::saveAsXML(OutputDevice& os, const bool extended) const {
127  std::string comment = "";
128  if (extended && cost >= 0.) {
129  os.writeAttr(SUMO_ATTR_COST, cost);
130  }
131  if (from != nullptr) {
132  os.writeAttr(SUMO_ATTR_FROM, from->getID());
133  }
134  if (to != nullptr) {
135  os.writeAttr(SUMO_ATTR_TO, to->getID());
136  }
137  if (destStop != "") {
138  os.writeAttr(SUMO_ATTR_BUS_STOP, destStop);
139  const std::string name = RONet::getInstance()->getStoppingPlaceName(destStop);
140  if (name != "") {
141  comment = " <!-- " + name + " -->";
142  }
143  }
144  os.writeAttr(SUMO_ATTR_LINES, lines);
145  if (intended != "" && intended != lines) {
146  os.writeAttr(SUMO_ATTR_INTENDED, intended);
147  }
148  if (depart >= 0) {
150  }
151  os.closeTag(comment);
152 }
153 
154 
155 void
156 ROPerson::Walk::saveAsXML(OutputDevice& os, const bool extended) const {
158  std::string comment = "";
159  if (extended && cost >= 0.) {
160  os.writeAttr(SUMO_ATTR_COST, cost);
161  }
162  if (dur > 0) {
163  os.writeAttr(SUMO_ATTR_DURATION, dur);
164  }
165  if (v > 0) {
166  os.writeAttr(SUMO_ATTR_SPEED, v);
167  }
168  os.writeAttr(SUMO_ATTR_EDGES, edges);
169  if (dep != 0.) {
171  }
172  if (arr != 0. && destStop == "") {
174  }
175  if (destStop != "") {
176  os.writeAttr(SUMO_ATTR_BUS_STOP, destStop);
177  const std::string name = RONet::getInstance()->getStoppingPlaceName(destStop);
178  if (name != "") {
179  comment = " <!-- " + name + " -->";
180  }
181  }
182  os.closeTag(comment);
183 }
184 
187  PersonTrip* result = new PersonTrip(from, to, modes, dep, arr, stopDest, walkFactor);
188  for (auto* item : myTripItems) {
189  result->myTripItems.push_back(item->clone());
190  }
191  return result;
192 }
193 
194 void
195 ROPerson::PersonTrip::saveVehicles(OutputDevice& os, OutputDevice* const typeos, bool asAlternatives, OptionsCont& options) const {
196  for (std::vector<ROVehicle*>::const_iterator it = myVehicles.begin(); it != myVehicles.end(); ++it) {
197  (*it)->saveAsXML(os, typeos, asAlternatives, options);
198  }
199 }
200 
201 void
202 ROPerson::PersonTrip::saveAsXML(OutputDevice& os, const bool extended, const bool asTrip, const bool writeGeoTrip) const {
203  if (asTrip) {
205  if (writeGeoTrip) {
206  Position fromPos = from->getLanes()[0]->getShape().positionAtOffset2D(getDepartPos());
207  if (GeoConvHelper::getFinal().usingGeoProjection()) {
210  os.writeAttr(SUMO_ATTR_FROMLONLAT, fromPos);
212  } else {
213  os.writeAttr(SUMO_ATTR_FROMXY, fromPos);
214  }
215  } else {
216  os.writeAttr(SUMO_ATTR_FROM, from->getID());
217  }
218  if (writeGeoTrip) {
219  Position toPos = to->getLanes()[0]->getShape().positionAtOffset2D(MIN2(getArrivalPos(), to->getLanes()[0]->getShape().length2D()));
223  os.writeAttr(SUMO_ATTR_TOLONLAT, toPos);
225  } else {
226  os.writeAttr(SUMO_ATTR_TOXY, toPos);
227  }
228  } else {
229  os.writeAttr(SUMO_ATTR_TO, to->getID());
230  }
231  std::vector<std::string> allowedModes;
232  if ((modes & SVC_BUS) != 0) {
233  allowedModes.push_back("public");
234  }
235  if ((modes & SVC_PASSENGER) != 0) {
236  allowedModes.push_back("car");
237  }
238  if ((modes & SVC_BICYCLE) != 0) {
239  allowedModes.push_back("bicycle");
240  }
241  if (allowedModes.size() > 0) {
242  os.writeAttr(SUMO_ATTR_MODES, toString(allowedModes));
243  }
244  if (!writeGeoTrip) {
245  if (dep != 0 && dep != std::numeric_limits<double>::infinity()) {
247  }
248  if (arr != 0 && arr != std::numeric_limits<double>::infinity()) {
250  }
251  }
252  if (getStopDest() != "") {
253  os.writeAttr(SUMO_ATTR_BUS_STOP, getStopDest());
254  }
255  if (walkFactor != 1) {
256  os.writeAttr(SUMO_ATTR_WALKFACTOR, walkFactor);
257  }
258  os.closeTag();
259  } else {
260  for (std::vector<TripItem*>::const_iterator it = myTripItems.begin(); it != myTripItems.end(); ++it) {
261  (*it)->saveAsXML(os, extended);
262  }
263  }
264 }
265 
266 SUMOTime
268  SUMOTime result = 0;
269  for (TripItem* tItem : myTripItems) {
270  result += tItem->getDuration();
271  }
272  return result;
273 }
274 
275 bool
277  PersonTrip* const trip, const ROVehicle* const veh, MsgHandler* const errorHandler) {
278  std::vector<ROIntermodalRouter::TripItem> result;
279  provider.getIntermodalRouter().compute(trip->getOrigin(), trip->getDestination(), trip->getDepartPos(), trip->getArrivalPos(), trip->getStopDest(),
280  getType()->maxSpeed * trip->getWalkFactor(), veh, trip->getModes(), time, result);
281  bool carUsed = false;
282  for (std::vector<ROIntermodalRouter::TripItem>::const_iterator it = result.begin(); it != result.end(); ++it) {
283  if (!it->edges.empty()) {
284  if (it->line == "") {
285  if (it + 1 == result.end() && trip->getStopDest() == "") {
286  trip->addTripItem(new Walk(it->edges, it->cost, trip->getDepartPos(false), trip->getArrivalPos(false)));
287  } else {
288  trip->addTripItem(new Walk(it->edges, it->cost, trip->getDepartPos(false), trip->getArrivalPos(false), it->destStop));
289  }
290  } else if (veh != nullptr && it->line == veh->getID()) {
291  trip->addTripItem(new Ride(it->edges.front(), it->edges.back(), veh->getID(), it->cost, trip->getArrivalPos(), it->destStop));
292  veh->getRouteDefinition()->addLoadedAlternative(new RORoute(veh->getID() + "_RouteDef", it->edges));
293  carUsed = true;
294  } else {
295  trip->addTripItem(new Ride(nullptr, nullptr, it->line, it->cost, trip->getArrivalPos(), it->destStop, it->intended, TIME2STEPS(it->depart)));
296  }
297  }
298  }
299  if (result.empty()) {
300  errorHandler->inform("No route for trip in person '" + getID() + "'.");
301  myRoutingSuccess = false;
302  }
303  return carUsed;
304 }
305 
306 
307 void
309  const bool /* removeLoops */, MsgHandler* errorHandler) {
310  myRoutingSuccess = true;
311  SUMOTime time = getParameter().depart;
312  for (std::vector<PlanItem*>::iterator it = myPlan.begin(); it != myPlan.end(); ++it) {
313  if ((*it)->needsRouting()) {
314  PersonTrip* trip = static_cast<PersonTrip*>(*it);
315  std::vector<ROVehicle*>& vehicles = trip->getVehicles();
316  if (vehicles.empty()) {
317  computeIntermodal(time, provider, trip, nullptr, errorHandler);
318  } else {
319  for (std::vector<ROVehicle*>::iterator v = vehicles.begin(); v != vehicles.end();) {
320  if (!computeIntermodal(time, provider, trip, *v, errorHandler)) {
321  v = vehicles.erase(v);
322  } else {
323  ++v;
324  }
325  }
326  }
327  }
328  time += (*it)->getDuration();
329  }
330 }
331 
332 
333 void
334 ROPerson::saveAsXML(OutputDevice& os, OutputDevice* const typeos, bool asAlternatives, OptionsCont& options) const {
335  // write the person's vehicles
336  const bool writeTrip = options.exists("write-trips") && options.getBool("write-trips");
337  const bool writeGeoTrip = writeTrip && options.getBool("write-trips.geo");
338  if (!writeTrip) {
339  for (std::vector<PlanItem*>::const_iterator it = myPlan.begin(); it != myPlan.end(); ++it) {
340  (*it)->saveVehicles(os, typeos, asAlternatives, options);
341  }
342  }
343 
344  if (typeos != nullptr && getType() != nullptr && !getType()->saved) {
345  getType()->write(*typeos);
346  getType()->saved = true;
347  }
348  if (getType() != nullptr && !getType()->saved) {
349  getType()->write(os);
350  getType()->saved = asAlternatives;
351  }
352 
353  // write the person
354  getParameter().write(os, options, SUMO_TAG_PERSON);
355 
356  for (std::vector<PlanItem*>::const_iterator it = myPlan.begin(); it != myPlan.end(); ++it) {
357  (*it)->saveAsXML(os, asAlternatives, writeTrip, writeGeoTrip);
358  }
359 
360  // write params
362  os.closeTag();
363 }
364 
365 
366 /****************************************************************************/
367 
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
long long int SUMOTime
Definition: SUMOTime.h:35
SVCPermissions getModes() const
Definition: ROPerson.h:313
std::string vtypeid
The vehicle&#39;s type id.
PlanItem * clone() const
Definition: ROPerson.cpp:186
void addVehicle(ROVehicle *veh)
Definition: ROPerson.h:288
Structure representing possible vehicle parameter.
int gPrecision
the precision for floating point outputs
Definition: StdDefs.cpp:27
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
The position is given.
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:109
bool usingGeoProjection() const
Returns whether a transformation from geo to metric coordinates will be performed.
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
void saveAsXML(OutputDevice &os, const bool extended, const bool asTrip, const bool writeGeoTrip) const
Definition: ROPerson.cpp:202
void setPrecision(int precision=gPrecision)
Sets the precison or resets it to default.
void saveAsXML(OutputDevice &os, OutputDevice *const typeos, bool asAlternatives, OptionsCont &options) const
Saves the complete person description.
Definition: ROPerson.cpp:334
const std::string DEFAULT_BIKETYPE_ID
std::vector< const ROEdge * > ConstROEdgeVector
Definition: ROEdge.h:57
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
bool myRoutingSuccess
Whether the last routing was successful.
Definition: RORoutable.h:182
#define TIME2STEPS(x)
Definition: SUMOTime.h:59
bool hasNext()
returns the information whether further substrings exist
const std::string DEFAULT_VTYPE_ID
Every person has a plan comprising of multiple planItems.
Definition: ROPerson.h:80
A planItem can be a Trip which contains multiple tripItems.
Definition: ROPerson.h:265
static RONet * getInstance()
Returns the pointer to the unique instance of RONet (singleton).
Definition: RONet.cpp:56
void saveAsXML(OutputDevice &os, const bool extended) const
Definition: ROPerson.cpp:156
void cartesian2geo(Position &cartesian) const
Converts the given cartesian (shifted) position to its geo (lat/long) representation.
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:176
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:48
void saveVehicles(OutputDevice &os, OutputDevice *const typeos, bool asAlternatives, OptionsCont &options) const
Definition: ROPerson.cpp:195
DepartPosDefinition departPosProcedure
Information how the vehicle shall choose the departure position.
std::vector< PlanItem * > myPlan
The plan of the person.
Definition: ROPerson.h:389
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
int gPrecisionGeo
Definition: StdDefs.cpp:28
void addStop(const SUMOVehicleParameter::Stop &stopPar, const ROEdge *const stopEdge)
Definition: ROPerson.cpp:119
bool exists(const std::string &name) const
Returns the information whether the named option is known.
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.
T MIN2(T a, T b)
Definition: StdDefs.h:74
A TripItem is part of a trip, e.g., go from here to here by car.
Definition: ROPerson.h:151
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:224
A basic edge for routing applications.
Definition: ROEdge.h:73
RORouteDef * getRouteDefinition() const
Returns the definition of the route the vehicle takes.
Definition: ROVehicle.h:76
const std::string & getStopDest() const
Definition: ROPerson.h:316
virtual ~ROPerson()
Destructor.
Definition: ROPerson.cpp:54
void writeParams(OutputDevice &device) const
write Params in the given outputdevice
std::vector< TripItem * > myTripItems
the fully specified trips
Definition: ROPerson.h:339
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:62
Definition of vehicle stop (position and duration)
ROPerson(const SUMOVehicleParameter &pars, const SUMOVTypeParameter *type)
Constructor.
Definition: ROPerson.cpp:49
void saveAsXML(OutputDevice &os, const bool extended) const
Definition: ROPerson.cpp:125
vehicle is a bus
double getWalkFactor() const
Definition: ROPerson.h:325
double departPos
(optional) The position the vehicle shall depart from
void write(OutputDevice &dev) const
Writes the vtype.
void computeRoute(const RORouterProvider &provider, const bool removeLoops, MsgHandler *errorHandler)
Definition: ROPerson.cpp:308
The router&#39;s network representation.
Definition: RONet.h:64
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:310
void addWalk(const ConstROEdgeVector &edges, const double duration, const double speed, const double departPos, const double arrivalPos, const std::string &busStop)
Definition: ROPerson.cpp:110
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:118
A storage for options typed value containers)
Definition: OptionsCont.h:90
Base class for a vehicle&#39;s route definition.
Definition: RORouteDef.h:56
static const GeoConvHelper & getFinal()
the coordinate transformation for writing the location element and for tracking the original coordina...
const std::string getStoppingPlaceName(const std::string &id) const
return the name for the given stopping place id
Definition: RONet.cpp:704
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:307
void addRide(const ROEdge *const from, const ROEdge *const to, const std::string &lines, double arrivalPos, const std::string &destStop)
Definition: ROPerson.cpp:101
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:291
virtual void addTripItem(TripItem *tripIt)
Definition: ROPerson.h:285
SUMOVTypeParameter * getVehicleTypeSecure(const std::string &id)
Retrieves the named vehicle type.
Definition: RONet.cpp:279
const int VEHPARS_DEPARTPOS_SET
SUMOTime getDuration() const
return duration sum of all trip items
Definition: ROPerson.cpp:267
bool computeIntermodal(SUMOTime time, const RORouterProvider &provider, PersonTrip *const trip, const ROVehicle *const veh, MsgHandler *const errorHandler)
Definition: ROPerson.cpp:276
const ROEdge * getOrigin() const
Definition: ROPerson.h:294
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:297