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-2017 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 
51 // ===========================================================================
52 // method definitions
53 // ===========================================================================
55  : RORoutable(pars, type) {
56 }
57 
58 
60  for (std::vector<PlanItem*>::const_iterator it = myPlan.begin(); it != myPlan.end(); ++it) {
61  delete *it;
62  }
63 }
64 
65 
66 void
67 ROPerson::addTrip(const ROEdge* const from, const ROEdge* const to, const SVCPermissions modeSet,
68  const std::string& vTypes, const double departPos, const double arrivalPos, const std::string& busStop, double walkFactor) {
69  PersonTrip* trip = new PersonTrip(from, to, modeSet, departPos, arrivalPos, busStop, walkFactor);
70  RONet* net = RONet::getInstance();
73 
74  for (StringTokenizer st(vTypes); st.hasNext();) {
75  pars.vtypeid = st.next();
78  if (type == 0) {
79  delete trip;
80  throw InvalidArgument("The vehicle type '" + pars.vtypeid + "' in a trip for person '" + getID() + "' is not known.");
81  }
82  pars.id = getID() + "_" + toString(trip->getVehicles().size());
83  trip->addVehicle(new ROVehicle(pars, new RORouteDef("!" + pars.id, 0, false, false), type, net));
84  }
85  if ((modeSet & SVC_PASSENGER) != 0 && trip->getVehicles().empty()) {
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  myPlan.push_back(trip);
90 }
91 
92 
93 void
94 ROPerson::addRide(const ROEdge* const from, const ROEdge* const to, const std::string& lines, const std::string& destStop) {
95  if (myPlan.empty() || myPlan.back()->isStop()) {
96  myPlan.push_back(new PersonTrip());
97  }
98  myPlan.back()->addTripItem(new Ride(from, to, lines, destStop));
99 }
100 
101 
102 void
103 ROPerson::addWalk(const ConstROEdgeVector& edges, const double duration, const double speed, const double departPos, const double arrivalPos, const std::string& busStop) {
104  if (myPlan.empty() || myPlan.back()->isStop()) {
105  myPlan.push_back(new PersonTrip());
106  }
107  myPlan.back()->addTripItem(new Walk(edges, duration, speed, departPos, arrivalPos, busStop));
108 }
109 
110 
111 void
112 ROPerson::addStop(const SUMOVehicleParameter::Stop& stopPar, const ROEdge* const stopEdge) {
113  myPlan.push_back(new Stop(stopPar, stopEdge));
114 }
115 
116 
117 void
120  if (from != 0) {
121  os.writeAttr(SUMO_ATTR_FROM, from->getID());
122  }
123  if (to != 0) {
124  os.writeAttr(SUMO_ATTR_TO, to->getID());
125  }
126  if (destStop != "") {
127  os.writeAttr(SUMO_ATTR_BUS_STOP, destStop);
128  }
129  os.writeAttr(SUMO_ATTR_LINES, lines);
130  os.closeTag();
131 }
132 
133 
134 void
137  if (dur > 0) {
138  os.writeAttr(SUMO_ATTR_DURATION, dur);
139  }
140  if (v > 0) {
141  os.writeAttr(SUMO_ATTR_SPEED, v);
142  }
143  os.writeAttr(SUMO_ATTR_EDGES, edges);
144  if (dep != std::numeric_limits<double>::infinity()) {
146  }
147  if (arr != std::numeric_limits<double>::infinity()) {
149  }
150  if (destStop != "") {
151  os.writeAttr(SUMO_ATTR_BUS_STOP, destStop);
152  }
153  os.closeTag();
154 }
155 
156 
157 void
158 ROPerson::PersonTrip::saveVehicles(OutputDevice& os, OutputDevice* const typeos, bool asAlternatives, OptionsCont& options) const {
159  for (std::vector<ROVehicle*>::const_iterator it = myVehicles.begin(); it != myVehicles.end(); ++it) {
160  (*it)->saveAsXML(os, typeos, asAlternatives, options);
161  }
162 }
163 
164 
165 bool
166 ROPerson::computeIntermodal(const RORouterProvider& provider, PersonTrip* const trip, const ROVehicle* const veh, MsgHandler* const errorHandler) {
167  std::vector<ROIntermodalRouter::TripItem> result;
168  provider.getIntermodalRouter().compute(trip->getOrigin(), trip->getDestination(), trip->getDepartPos(), trip->getArrivalPos(),
169  myType->maxSpeed * trip->getWalkFactor(), veh, trip->getModes(), myParameter.depart, result);
170  bool carUsed = false;
171  for (std::vector<ROIntermodalRouter::TripItem>::const_iterator it = result.begin(); it != result.end(); ++it) {
172  if (!it->edges.empty()) {
173  if (it->line == "") {
174  if (it + 1 == result.end() && !trip->hasBusStopDest()) {
175  trip->addTripItem(new Walk(it->edges));
176  } else {
177  trip->addTripItem(new Walk(it->edges, it->destStop));
178  }
179  } else if (veh != 0 && it->line == veh->getID()) {
180  trip->addTripItem(new Ride(it->edges.front(), it->edges.back(), veh->getID(), it->destStop));
181  veh->getRouteDefinition()->addLoadedAlternative(new RORoute(veh->getID() + "_RouteDef", it->edges));
182  carUsed = true;
183  } else {
184  trip->addTripItem(new Ride(0, 0, it->line, it->destStop));
185  }
186  }
187  }
188  if (result.empty()) {
189  errorHandler->inform("No route for trip in person '" + getID() + "'.");
190  myRoutingSuccess = false;
191  }
192  return carUsed;
193 }
194 
195 
196 void
198  const bool /* removeLoops */, MsgHandler* errorHandler) {
199  myRoutingSuccess = true;
200  for (std::vector<PlanItem*>::iterator it = myPlan.begin(); it != myPlan.end(); ++it) {
201  if ((*it)->needsRouting()) {
202  PersonTrip* trip = static_cast<PersonTrip*>(*it);
203  ConstROEdgeVector edges;
204  std::vector<ROVehicle*>& vehicles = trip->getVehicles();
205  if (vehicles.empty()) {
206  computeIntermodal(provider, trip, 0, errorHandler);
207  } else {
208  for (std::vector<ROVehicle*>::iterator v = vehicles.begin(); v != vehicles.end();) {
209  if (!computeIntermodal(provider, trip, *v, errorHandler)) {
210  v = vehicles.erase(v);
211  } else {
212  ++v;
213  }
214  }
215  }
216  }
217  }
218 }
219 
220 
221 void
222 ROPerson::saveAsXML(OutputDevice& os, OutputDevice* const typeos, bool asAlternatives, OptionsCont& options) const {
223  // write the person's vehicles
224  for (std::vector<PlanItem*>::const_iterator it = myPlan.begin(); it != myPlan.end(); ++it) {
225  (*it)->saveVehicles(os, typeos, asAlternatives, options);
226  }
227 
228  if (typeos != 0 && myType != 0 && !myType->saved) {
229  myType->write(*typeos);
230  myType->saved = true;
231  }
232  if (myType != 0 && !myType->saved) {
233  myType->write(os);
234  myType->saved = asAlternatives;
235  }
236 
237  // write the person
238  myParameter.write(os, options, SUMO_TAG_PERSON);
239 
240  for (std::vector<PlanItem*>::const_iterator it = myPlan.begin(); it != myPlan.end(); ++it) {
241  (*it)->saveAsXML(os);
242  }
243 
244  for (std::map<std::string, std::string>::const_iterator j = myParameter.getMap().begin(); j != myParameter.getMap().end(); ++j) {
246  os.writeAttr(SUMO_ATTR_KEY, (*j).first);
247  os.writeAttr(SUMO_ATTR_VALUE, (*j).second);
248  os.closeTag();
249  }
250  os.closeTag();
251 }
252 
253 
254 /****************************************************************************/
255 
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:94
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:255
std::string vtypeid
The vehicle&#39;s type id.
bool hasBusStopDest() const
Definition: ROPerson.h:258
void addVehicle(ROVehicle *veh)
Definition: ROPerson.h:237
Structure representing possible vehicle parameter.
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:111
bool computeIntermodal(const RORouterProvider &provider, PersonTrip *const trip, const ROVehicle *const veh, MsgHandler *const errorHandler)
Definition: ROPerson.cpp:166
void saveAsXML(OutputDevice &os, OutputDevice *const typeos, bool asAlternatives, OptionsCont &options) const
Saves the complete person description.
Definition: ROPerson.cpp:222
std::vector< const ROEdge * > ConstROEdgeVector
Definition: ROEdge.h:62
bool myRoutingSuccess
Whether the last routing was successful.
Definition: RORoutable.h:171
void saveAsXML(OutputDevice &os) const
Definition: ROPerson.cpp:118
const std::string DEFAULT_VTYPE_ID
A planItem can be a Trip which contains multiple tripItems.
Definition: ROPerson.h:216
static RONet * getInstance()
Returns the pointer to the unique instance of RONet (singleton).
Definition: RONet.cpp:62
A routable thing such as a vehicle or person.
Definition: RORoutable.h:62
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:156
void saveAsXML(OutputDevice &os) const
Definition: ROPerson.cpp:135
double getDepartPos() const
Definition: ROPerson.h:249
A vehicle as used by router.
Definition: ROVehicle.h:60
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:56
void saveVehicles(OutputDevice &os, OutputDevice *const typeos, bool asAlternatives, OptionsCont &options) const
Definition: ROPerson.cpp:158
std::vector< PlanItem * > myPlan
The plan of the person.
Definition: ROPerson.h:330
parameter associated to a certain key
void addStop(const SUMOVehicleParameter::Stop &stopPar, const ROEdge *const stopEdge)
Definition: ROPerson.cpp:112
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 vehicle.
Definition: RORoutable.h:92
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:186
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:83
virtual ~ROPerson()
Destructor.
Definition: ROPerson.cpp:59
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:67
ROPerson(const SUMOVehicleParameter &pars, const SUMOVTypeParameter *type)
Constructor.
Definition: ROPerson.cpp:54
const SUMOVTypeParameter *const myType
The type of the vehicle.
Definition: RORoutable.h:168
double getWalkFactor() const
Definition: ROPerson.h:270
void write(OutputDevice &dev) const
Writes the vtype.
void computeRoute(const RORouterProvider &provider, const bool removeLoops, MsgHandler *errorHandler)
Definition: ROPerson.cpp:197
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, TraCI may modify this (whe changing color) ...
void addWalk(const ConstROEdgeVector &edges, const double duration, const double speed, const double departPos, const double arrivalPos, const std::string &busStop)
Definition: ROPerson.cpp:103
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:85
A storage for options typed value containers)
Definition: OptionsCont.h:99
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:76
const int VEHPARS_VTYPE_SET
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
std::vector< ROVehicle * > & getVehicles()
Definition: ROPerson.h:240
virtual void addTripItem(TripItem *tripIt)
Definition: ROPerson.h:234
bool closeTag()
Closes the most recently opened tag.
SUMOVTypeParameter * getVehicleTypeSecure(const std::string &id)
Retrieves the named vehicle type.
Definition: RONet.cpp:281
const ROEdge * getOrigin() const
Definition: ROPerson.h:243
double getArrivalPos() const
Definition: ROPerson.h:252
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.
const ROEdge * getDestination() const
Definition: ROPerson.h:246