SUMO - Simulation of Urban MObility
MSPerson.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // The class for modelling person-movements
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2001-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 
34 #include <string>
35 #include <vector>
38 #include <utils/common/ToString.h>
40 #include <utils/geom/GeomHelper.h>
41 #include <microsim/MSNet.h>
42 #include <microsim/MSEdge.h>
43 #include <microsim/MSLane.h>
44 #include "MSPerson.h"
47 #include <microsim/MSVehicle.h>
51 #include "MSPModel.h"
52 
53 // ===========================================================================
54 // method definitions
55 // ===========================================================================
56 /* -------------------------------------------------------------------------
57  * MSPerson::MSPersonStage_Walking - methods
58  * ----------------------------------------------------------------------- */
60  MSStoppingPlace* toStop,
61  SUMOTime walkingTime, double speed,
62  double departPos, double arrivalPos, double departPosLat) :
63  MSTransportable::Stage(*route.back(), toStop, SUMOVehicleParameter::interpretEdgePos(
64  arrivalPos, route.back()->getLength(), SUMO_ATTR_ARRIVALPOS, "person walking to " + route.back()->getID()), MOVING_WITHOUT_VEHICLE), myWalkingTime(walkingTime), myRoute(route),
65  myCurrentInternalEdge(0),
66  myDepartPos(departPos),
67  myDepartPosLat(departPosLat),
68  mySpeed(speed),
69  myPedestrianState(0) {
71  myDepartPos, myRoute.front()->getLength(), SUMO_ATTR_DEPARTPOS, "person walking from " + myRoute.front()->getID());
72  if (walkingTime > 0) {
74  }
75 }
76 
77 
79 }
80 
81 
82 const MSEdge*
84  if (myCurrentInternalEdge != 0) {
85  return myCurrentInternalEdge;
86  } else {
87  return *myRouteStep;
88  }
89 }
90 
91 
92 const MSEdge*
94  return myRoute.front();
95 }
96 
97 
98 double
100  return myPedestrianState == 0 ? -1 : myPedestrianState->getEdgePos(*this, now);
101 }
102 
103 
104 Position
106  return myPedestrianState->getPosition(*this, now);
107 }
108 
109 
110 double
112  return myPedestrianState->getAngle(*this, now);
113 }
114 
115 
116 SUMOTime
118  return myPedestrianState->getWaitingTime(*this, now);
119 }
120 
121 
122 double
124  return myPedestrianState->getSpeed(*this);
125 }
126 
127 
130  return myRoute;
131 }
132 
133 void
135  previous->getEdge()->removePerson(person);
136  myDeparted = now;
137  myRouteStep = myRoute.begin();
138  if (myWalkingTime == 0) {
139  if (!person->proceed(net, now)) {
141  }
142  return;
143  }
144  if (previous->getEdgePos(now) >= 0) {
145  myDepartPos = previous->getEdgePos(now);
146  if (myWalkingTime > 0) {
148  }
149  }
150  myPedestrianState = MSPModel::getModel()->add(dynamic_cast<MSPerson*>(person), this, now);
151  if (myPedestrianState == 0) {
153  return;
154  }
155  (*myRouteStep)->addPerson(person);
156 }
157 
158 void
161  myPedestrianState = 0;
162 }
163 
164 void
166  mySpeed = speed;
167 }
168 
169 
170 double
172  return walkDistance() / STEPS2TIME(myWalkingTime + 1); // avoid systematic rounding errors
173 }
174 
175 
176 double
179  double length = 0;
180  for (ConstMSEdgeVector::const_iterator i = myRoute.begin(); i != myRoute.end(); ++i) {
181  length += (*i)->getLength();
182  }
183  // determine walking direction for depart and arrival
184  bool departFwd = true;
185  bool arriveFwd = true;
186  if (myRoute.front() == myRoute.back()) {
187  if (myDepartPos > myArrivalPos) {
188  departFwd = false;
189  arriveFwd = false;
190  }
191  } else {
192  // disconnected defaults to forward
193  if (myRoute.front()->getFromJunction() == myRoute[1]->getToJunction()) {
194  departFwd = false;
195  }
196  if (myRoute[myRoute.size() - 2]->getFromJunction() == myRoute.back()->getToJunction()) {
197  arriveFwd = false;
198  }
199  }
200  length -= (departFwd ? myDepartPos : myRoute.front()->getLength() - myDepartPos);
201  length -= (arriveFwd ? myRoute.back()->getLength() - myArrivalPos : myArrivalPos);
202  return MAX2(POSITION_EPS, length);
203 }
204 
205 
206 void
209  os.openTag("walk");
210  os.writeAttr("depart", time2string(myDeparted));
211  os.writeAttr("arrival", time2string(myArrived));
212  os.writeAttr("arrivalPos", toString(myArrivalPos));
213  os.closeTag();
214 }
215 
216 
217 void
220  if (myWalkingTime > 0) {
222  } else if (mySpeed > 0) {
224  }
225  os.closeTag();
226 }
227 
228 
229 void
231  os.openTag("event").writeAttr("time", time2string(t)).writeAttr("type", "departure")
232  .writeAttr("agent", p.getID()).writeAttr("link", myRoute.front()->getID()).closeTag();
233 }
234 
235 
236 void
238  os.openTag("event").writeAttr("time", time2string(t)).writeAttr("type", "arrival")
239  .writeAttr("agent", p.getID()).writeAttr("link", myRoute.back()->getID()).closeTag();
240 }
241 
242 
243 bool
245  ((MSEdge*)getEdge())->removePerson(person);
246  //std::cout << SIMTIME << " moveToNextEdge person=" << person->getID() << "\n";
247  if (myRouteStep == myRoute.end() - 1) {
248  if (myDestinationStop != 0) {
250  }
251  if (!person->proceed(MSNet::getInstance(), currentTime)) {
253  }
254  //std::cout << " end walk. myRouteStep=" << (*myRouteStep)->getID() << "\n";
255  return true;
256  } else {
257  if (nextInternal == 0) {
258  ++myRouteStep;
260  } else {
261  myCurrentInternalEdge = nextInternal;
262  }
263  ((MSEdge*) getEdge())->addPerson(person);
264  return false;
265  }
266 }
267 
268 
269 
270 /* -------------------------------------------------------------------------
271  * MSPerson::MSPersonStage_Driving - methods
272  * ----------------------------------------------------------------------- */
274  MSStoppingPlace* toStop, const double arrivalPos, const std::vector<std::string>& lines) :
275  MSTransportable::Stage_Driving(destination, toStop,
276  SUMOVehicleParameter::interpretEdgePos(
277  arrivalPos, destination.getLength(), SUMO_ATTR_ARRIVALPOS, "person riding to " + destination.getID()),
278  lines) {
279 }
280 
281 
283 
284 
285 void
287  if (previous->getDestinationStop() != 0) {
288  // the arrival stop may have an access point
289  myWaitingEdge = &previous->getDestinationStop()->getLane().getEdge();
291  } else {
292  myWaitingEdge = previous->getEdge();
294  }
295  myWaitingPos = previous->getEdgePos(now);
296  myWaitingSince = now;
297  SUMOVehicle* availableVehicle = net->getVehicleControl().getWaitingVehicle(myWaitingEdge, myLines, myWaitingPos, person->getID());
298  if (availableVehicle != 0 && availableVehicle->getParameter().departProcedure == DEPART_TRIGGERED && !availableVehicle->hasDeparted()) {
299  setVehicle(availableVehicle);
300  myWaitingEdge->removePerson(person);
301  myVehicle->addPerson(person);
305  } else {
306  net->getPersonControl().addWaiting(myWaitingEdge, person);
307  myWaitingEdge->addPerson(person);
308  }
309 }
310 
311 
312 std::string
314  return isWaiting4Vehicle() ? "waiting for " + joinToString(myLines, ",") : "driving";
315 }
316 
317 
318 void
320  os.openTag("ride");
321  os.writeAttr("waitingTime", time2string(myDeparted - myWaitingSince));
322  os.writeAttr("vehicle", myVehicleID);
323  os.writeAttr("depart", time2string(myDeparted));
324  os.writeAttr("arrival", time2string(myArrived));
325  os.writeAttr("arrivalPos", toString(myArrivalPos));
326  os.closeTag();
327 }
328 
329 
330 void
334 }
335 
336 
337 
338 /* -------------------------------------------------------------------------
339  * MSPerson - methods
340  * ----------------------------------------------------------------------- */
342  : MSTransportable(pars, vtype, plan) {
343 }
344 
345 
347 }
348 
349 
350 bool
352  MSTransportable::Stage* prior = *myStep;
353  prior->setArrived(time);
354  /*
355  if(myWriteEvents) {
356  (*myStep)->endEventOutput(*this, time, OutputDevice::getDeviceByOption("person-event-output"));
357  }
358  */
359  //if (getID() == "ego") {
360  // std::cout << time2string(time) << " person=" << getID() << " proceed priorStep=" << myStep - myPlan->begin() << " planSize=" << myPlan->size() << "\n";
361  //}
362  myStep++;
363  if (myStep != myPlan->end()) {
364  (*myStep)->proceed(net, this, time, prior);
365  /*
366  if(myWriteEvents) {
367  (*myStep)->beginEventOutput(*this, time, OutputDevice::getDeviceByOption("person-event-output"));
368  }
369  */
370  return true;
371  } else {
372  prior->getEdge()->removePerson(this);
373  return false;
374  }
375 }
376 
377 
378 const std::string&
380 // if (getCurrentStageType() == MOVING_WITHOUT_VEHICLE) {
381 // MSPersonStage_Walking* walkingStage = dynamic_cast<MSPersonStage_Walking*>(*myStep);
382 // assert(walkingStage != 0);
383 // const MSEdge* nextEdge = walkingStage->getPedestrianState()->getNextEdge(*walkingStage);
384 // if (nextEdge != 0) {
385 // return nextEdge->getID();
386 // }
387 // }
388 // return StringUtils::emptyString;
389  const MSEdge* nextEdge = getNextEdgePtr();
390  if (nextEdge != 0) {
391  return nextEdge->getID();
392  }
394 }
395 
396 
397 const MSEdge*
400  MSPersonStage_Walking* walkingStage = dynamic_cast<MSPersonStage_Walking*>(*myStep);
401  assert(walkingStage != 0);
402  return walkingStage->getPedestrianState()->getNextEdge(*walkingStage);
403 
404  }
405  return 0;
406 }
407 
408 
409 void
411  os.openTag("personinfo").writeAttr("id", getID()).writeAttr("depart", time2string(getDesiredDepart()));
412  for (MSTransportablePlan::const_iterator i = myPlan->begin(); i != myPlan->end(); ++i) {
413  (*i)->tripInfoOutput(os);
414  }
415  os.closeTag();
416 }
417 
418 
419 void
422  if (myStep == myPlan->end()) {
423  os.writeAttr("arrival", time2string(MSNet::getInstance()->getCurrentTimeStep()));
424  }
425  for (MSTransportablePlan::const_iterator i = myPlan->begin(); i != myPlan->end(); ++i) {
426  (*i)->routeOutput(os);
427  }
428  os.closeTag();
429  os.lf();
430 }
431 
432 
433 double
436  MSPersonStage_Walking* walkingStage = dynamic_cast<MSPersonStage_Walking*>(*myStep);
437  assert(walkingStage != 0);
438  return walkingStage->getMaxSpeed() / myVType->getMaxSpeed();
439  }
440  return 1;
441 }
442 
443 /****************************************************************************/
444 
The departure is person triggered.
double getEdgePos(SUMOTime now) const
Definition: MSPerson.cpp:99
void addWaiting(const MSEdge *edge, MSTransportable *person)
adds a transportable to the list of transportables waiting for a vehicle on the specified edge ...
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:261
void setArrived(SUMOTime now)
logs end of the step
MSPersonStage_Walking(const ConstMSEdgeVector &route, MSStoppingPlace *toStop, SUMOTime walkingTime, double speed, double departPos, double arrivalPos, double departPosLat)
constructor
Definition: MSPerson.cpp:59
MSEdge & getEdge() const
Returns the lane&#39;s edge.
Definition: MSLane.h:582
virtual ~MSPerson()
destructor
Definition: MSPerson.cpp:346
virtual double getAngle(const MSPerson::MSPersonStage_Walking &stage, SUMOTime now) const =0
return the direction in which the person faces in degrees
A lane area vehicles can halt at.
virtual void proceed(MSNet *net, MSTransportable *person, SUMOTime now, Stage *previous)
proceeds to the next step
Definition: MSPerson.cpp:286
MSEdge * myCurrentInternalEdge
The current internal edge this person is on or 0.
Definition: MSPerson.h:201
MSPersonStage_Driving(const MSEdge &destination, MSStoppingPlace *toStop, const double arrivalPos, const std::vector< std::string > &lines)
constructor
Definition: MSPerson.cpp:273
MSPerson(const SUMOVehicleParameter *pars, MSVehicleType *vtype, MSTransportable::MSTransportablePlan *plan)
constructor
Definition: MSPerson.cpp:341
const MSStoppingPlace * getDestinationStop() const
returns the destination stop (if any)
bool isWaiting4Vehicle() const
Whether the person waits for a vehicle.
Position getPosition(SUMOTime now) const
returns the position of the transportable
Definition: MSPerson.cpp:105
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:60
virtual void remove(PedestrianState *state)=0
remove the specified person from the pedestrian simulation
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:169
const MSEdge * getNextEdgePtr() const
returns the next edge ptr if this person is walking and the pedestrian model allows it ...
Definition: MSPerson.cpp:398
T MAX2(T a, T b)
Definition: StdDefs.h:74
double getMaxSpeed() const
Get vehicle&#39;s maximum speed [m/s].
virtual void routeOutput(OutputDevice &os) const
Called on writing vehroute output.
Definition: MSPerson.cpp:218
const std::set< std::string > myLines
the lines to choose from
const std::string & getID() const
returns the id of the transportable
void removeWaiting(const MSEdge *const edge, SUMOVehicle *vehicle)
Removes a vehicle from the list of waiting vehicles to a given edge.
std::vector< const MSEdge * > ConstMSEdgeVector
Definition: MSEdge.h:79
virtual bool proceed(MSNet *net, SUMOTime time)=0
MSTransportablePlan::iterator myStep
the iterator over the route
virtual void beginEventOutput(const MSTransportable &p, SUMOTime t, OutputDevice &os) const
Called for writing the events output.
Definition: MSPerson.cpp:230
virtual void tripInfoOutput(OutputDevice &os) const
Called on writing tripinfo output.
Definition: MSPerson.cpp:319
The simulated network and simulation perfomer.
Definition: MSNet.h:89
The car-following model and parameter.
Definition: MSVehicleType.h:73
virtual void erase(MSTransportable *transportable)
removes a single transportable
virtual bool hasDeparted() const =0
Returns whether this vehicle has departed.
void addTransportable(MSTransportable *p)
adds a transportable to this stop
SUMOTime getDesiredDepart() const
Returns the desired departure time.
double getAngle(SUMOTime now) const
returns the angle of the transportable
Definition: MSPerson.cpp:111
ConstMSEdgeVector getEdges() const
the edges of the current stage
Definition: MSPerson.cpp:129
const MSEdge * getEdge() const
Returns the current edge.
Definition: MSPerson.cpp:83
virtual PedestrianState * add(MSPerson *person, MSPerson::MSPersonStage_Walking *stage, SUMOTime now)=0
register the given person as a pedestrian
const MSEdge & getDestination() const
returns the destination edge
static double interpretEdgePos(double pos, double maximumValue, SumoXMLAttr attr, const std::string &id)
Interprets negative edge positions and fits them onto a given edge.
SUMOTime myWaitingSince
The time since which this person is waiting for a ride.
virtual void proceed(MSNet *net, MSTransportable *person, SUMOTime now, Stage *previous)
proceeds to the next step
Definition: MSPerson.cpp:134
const MSEdge * getFromEdge() const
ConstMSEdgeVector myRoute
The route of the person.
Definition: MSPerson.h:195
const std::string & getID() const
Returns the id.
Definition: Named.h:66
A road/street connecting two junctions.
Definition: MSEdge.h:81
std::vector< MSTransportable::Stage * > MSTransportablePlan
the structure holding the plan of a transportable
virtual void routeOutput(OutputDevice &os) const
Called on writing vehroute output.
Definition: MSPerson.cpp:420
virtual Position getPosition(const MSPerson::MSPersonStage_Walking &stage, SUMOTime now) const =0
return the network coordinate of the person
MSVehicleType * myVType
This transportable&#39;s type. (mainly used for drawing related information Note sure if it is really nec...
virtual void removePerson(MSTransportable *p) const
Definition: MSEdge.h:600
const MSLane & getLane() const
Returns the lane this stop is located at.
void abort(MSTransportable *)
abort this stage (TraCI)
Definition: MSPerson.cpp:159
SUMOTime myArrived
the time at which this stage ended
bool moveToNextEdge(MSPerson *person, SUMOTime currentTime, MSEdge *nextInternal=0)
move forward and return whether the person arrived
Definition: MSPerson.cpp:244
the edges of a route
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:56
PedestrianState * getPedestrianState() const
Definition: MSPerson.h:174
SUMOTime myDeparted
the time at which this stage started
virtual void addPerson(MSTransportable *p) const
Definition: MSEdge.h:596
Representation of a vehicle.
Definition: SUMOVehicle.h:67
MSStoppingPlace *const myDestinationStop
the stop to reach by getting transported (if any)
static MSPModel * getModel()
Definition: MSPModel.cpp:66
virtual MSTransportableControl & getPersonControl()
Returns the person control.
Definition: MSNet.cpp:767
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
StageType getCurrentStageType() const
the current stage type of the transportable
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:305
virtual SUMOTime getWaitingTime(const MSPerson::MSPersonStage_Walking &stage, SUMOTime now) const =0
return the time the person spent standing
virtual double getEdgePos(const MSPerson::MSPersonStage_Walking &stage, SUMOTime now) const =0
return the offset from the start of the current edge measured in its natural direction ...
#define STEPS2TIME(x)
Definition: SUMOTime.h:65
static void addPedestrianData(double walkLength, SUMOTime walkDuration)
record tripinfo data for pedestrians
virtual double getSpeed(const MSPerson::MSPersonStage_Walking &stage) const =0
return the current speed of the person
DepartDefinition departProcedure
Information how the vehicle shall choose the depart time.
SUMOVehicle * getWaitingVehicle(const MSEdge *const edge, const std::set< std::string > &lines, const double position, const std::string ridingID)
virtual void routeOutput(OutputDevice &os) const
Called on writing vehroute output.
Definition: MSPerson.cpp:331
#define POSITION_EPS
Definition: config.h:176
ConstMSEdgeVector::iterator myRouteStep
Definition: MSPerson.h:198
SUMOVehicle * myVehicle
The taken vehicle.
void setSpeed(double speed)
sets the walking speed (ignored in other stages)
Definition: MSPerson.cpp:165
virtual const MSEdge * getNextEdge(const MSPerson::MSPersonStage_Walking &stage) const =0
return the list of internal edges if the pedestrian is on an intersection
Position getWaitPosition() const
Returns the next free waiting place for pedestrians / containers.
SUMOTime getWaitingTime(SUMOTime now) const
the time this transportable spent waiting
Definition: MSPerson.cpp:117
void unregisterOneWaitingForPerson()
decreases the count of vehicles waiting for a person to allow recogniztion of person related deadlock...
double getMaxSpeed() const
accessors to be used by MSPModel
Definition: MSPerson.h:149
static std::string emptyString
An empty string.
Definition: StringUtils.h:85
double computeAverageSpeed() const
Definition: MSPerson.cpp:171
virtual void endEventOutput(const MSTransportable &p, SUMOTime t, OutputDevice &os) const
Called for writing the events output (end of an action)
Definition: MSPerson.cpp:237
Structure representing possible vehicle parameter.
virtual const MSEdge * getEdge() const =0
Returns the current edge.
virtual void tripInfoOutput(OutputDevice &os) const
Called on writing tripinfo output.
Definition: MSPerson.cpp:410
MSInsertionControl & getInsertionControl()
Returns the insertion control.
Definition: MSNet.h:358
double getSpeed() const
the speed of the transportable
Definition: MSPerson.cpp:123
SUMOTime myWalkingTime
the time the person is walking
Definition: MSPerson.h:192
double getSpeedFactor() const
the current speed factor of the transportable (where applicable)
Definition: MSPerson.cpp:434
~MSPersonStage_Walking()
destructor
Definition: MSPerson.cpp:78
const std::string & getNextEdge() const
return the list of internal edges if this person is walking and the pedestrian model allows it ...
Definition: MSPerson.cpp:379
std::string getStageDescription() const
returns the stage description as a string
Definition: MSPerson.cpp:313
PedestrianState * myPedestrianState
state that is to be manipulated by MSPModel
Definition: MSPerson.h:208
bool proceed(MSNet *net, SUMOTime time)
Definition: MSPerson.cpp:351
virtual void addPerson(MSTransportable *person)=0
Adds a person to this vehicle.
virtual const SUMOVehicleParameter & getParameter() const =0
Returns the vehicle&#39;s parameter (including departure definition)
const MSEdge * getFromEdge() const
Definition: MSPerson.cpp:93
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
bool closeTag()
Closes the most recently opened tag.
MSTransportablePlan * myPlan
the plan of the transportable
long long int SUMOTime
Definition: TraCIDefs.h:52
void add(SUMOVehicle *veh)
Adds a single vehicle for departure.
double walkDistance() const
compute total walking distance
Definition: MSPerson.cpp:177
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=gPrecision)
Definition: ToString.h:237
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
void lf()
writes a line feed if applicable
Definition: OutputDevice.h:239
virtual void tripInfoOutput(OutputDevice &os) const
Called on writing tripinfo output.
Definition: MSPerson.cpp:207
virtual double getEdgePos(SUMOTime now) const =0
static const Position INVALID
used to indicate that a position is valid
Definition: Position.h:278
double myArrivalPos
the position at which we want to arrive