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>
48 #include "MSPModel.h"
49 
50 // ===========================================================================
51 // method definitions
52 // ===========================================================================
53 /* -------------------------------------------------------------------------
54  * MSPerson::MSPersonStage_Walking - methods
55  * ----------------------------------------------------------------------- */
57  MSStoppingPlace* toStop,
58  SUMOTime walkingTime, double speed,
59  double departPos, double arrivalPos, double departPosLat) :
60  MSTransportable::Stage(*route.back(), toStop, SUMOVehicleParameter::interpretEdgePos(
61  arrivalPos, route.back()->getLength(), SUMO_ATTR_ARRIVALPOS, "person walking to " + route.back()->getID()), MOVING_WITHOUT_VEHICLE), myWalkingTime(walkingTime), myRoute(route),
62  myCurrentInternalEdge(0),
63  myDepartPos(departPos),
64  myDepartPosLat(departPosLat),
65  mySpeed(speed),
66  myPedestrianState(0) {
68  myDepartPos, myRoute.front()->getLength(), SUMO_ATTR_DEPARTPOS, "person walking from " + myRoute.front()->getID());
69  if (walkingTime > 0) {
71  }
72 }
73 
74 
76 }
77 
78 
79 const MSEdge*
81  if (myCurrentInternalEdge != 0) {
82  return myCurrentInternalEdge;
83  } else {
84  return *myRouteStep;
85  }
86 }
87 
88 
89 const MSEdge*
91  return myRoute.front();
92 }
93 
94 
95 double
97  return myPedestrianState->getEdgePos(*this, now);
98 }
99 
100 
101 Position
103  return myPedestrianState->getPosition(*this, now);
104 }
105 
106 
107 double
109  return myPedestrianState->getAngle(*this, now);
110 }
111 
112 
113 SUMOTime
115  return myPedestrianState->getWaitingTime(*this, now);
116 }
117 
118 
119 double
121  return myPedestrianState->getSpeed(*this);
122 }
123 
124 
127  return myRoute;
128 }
129 
130 void
132  previous->getEdge()->removePerson(person);
133  myRouteStep = myRoute.begin();
134  if (myWalkingTime == 0) {
135  if (!person->proceed(net, now)) {
137  }
138  return;
139  }
140  if (previous->getEdgePos(now) >= 0) {
141  myDepartPos = previous->getEdgePos(now);
142  if (myWalkingTime > 0) {
144  }
145  }
146  myPedestrianState = MSPModel::getModel()->add(dynamic_cast<MSPerson*>(person), this, now);
147  (*myRouteStep)->addPerson(person);
148 }
149 
150 void
153  myPedestrianState = 0;
154 }
155 
156 void
158  mySpeed = speed;
159 }
160 
161 
162 double
164  double length = 0;
165  for (ConstMSEdgeVector::const_iterator i = myRoute.begin(); i != myRoute.end(); ++i) {
166  length += (*i)->getLength();
167  }
168  length -= myDepartPos;
169  length -= myRoute.back()->getLength() - myArrivalPos;
170  return length / STEPS2TIME(myWalkingTime + 1); // avoid systematic rounding errors
171 }
172 
173 
174 void
176  os.openTag("walk").writeAttr("arrival", time2string(myArrived)).closeTag();
177 }
178 
179 
180 void
183  if (myWalkingTime > 0) {
185  } else if (mySpeed > 0) {
187  }
188  os.closeTag();
189 }
190 
191 
192 void
194  os.openTag("event").writeAttr("time", time2string(t)).writeAttr("type", "departure")
195  .writeAttr("agent", p.getID()).writeAttr("link", myRoute.front()->getID()).closeTag();
196 }
197 
198 
199 void
201  os.openTag("event").writeAttr("time", time2string(t)).writeAttr("type", "arrival")
202  .writeAttr("agent", p.getID()).writeAttr("link", myRoute.back()->getID()).closeTag();
203 }
204 
205 
206 bool
208  ((MSEdge*)getEdge())->removePerson(person);
209  //std::cout << SIMTIME << " moveToNextEdge person=" << person->getID() << "\n";
210  if (myRouteStep == myRoute.end() - 1) {
211  if (myDestinationStop != 0) {
213  }
214  if (!person->proceed(MSNet::getInstance(), currentTime)) {
216  }
217  //std::cout << " end walk. myRouteStep=" << (*myRouteStep)->getID() << "\n";
218  return true;
219  } else {
220  if (nextInternal == 0) {
221  ++myRouteStep;
223  } else {
224  myCurrentInternalEdge = nextInternal;
225  }
226  ((MSEdge*) getEdge())->addPerson(person);
227  return false;
228  }
229 }
230 
231 
232 
233 /* -------------------------------------------------------------------------
234  * MSPerson::MSPersonStage_Driving - methods
235  * ----------------------------------------------------------------------- */
237  MSStoppingPlace* toStop, const double arrivalPos, const std::vector<std::string>& lines)
238  : MSTransportable::Stage_Driving(destination, toStop, arrivalPos, lines) {}
239 
240 
242 
243 
244 void
246  if (previous->getDestinationStop() != 0) {
247  // the arrival stop may have an access point
248  myWaitingEdge = &previous->getDestinationStop()->getLane().getEdge();
250  } else {
251  myWaitingEdge = previous->getEdge();
253  }
254  myWaitingPos = previous->getEdgePos(now);
255  myWaitingSince = now;
256  SUMOVehicle* availableVehicle = net->getVehicleControl().getWaitingVehicle(myWaitingEdge, myLines, myWaitingPos, person->getID());
257  if (availableVehicle != 0 && availableVehicle->getParameter().departProcedure == DEPART_TRIGGERED && !availableVehicle->hasDeparted()) {
258  myVehicle = availableVehicle;
259  myWaitingEdge->removePerson(person);
260  myVehicle->addPerson(person);
264  } else {
265  net->getPersonControl().addWaiting(myWaitingEdge, person);
266  myWaitingEdge->addPerson(person);
267  }
268 }
269 
270 
271 std::string
273  return isWaiting4Vehicle() ? "waiting for " + joinToString(myLines, ",") : "driving";
274 }
275 
276 
277 void
279  os.openTag("ride").writeAttr("depart", time2string(myDeparted)).writeAttr("arrival", time2string(myArrived)).closeTag();
280 }
281 
282 
283 void
287 }
288 
289 
290 
291 /* -------------------------------------------------------------------------
292  * MSPerson - methods
293  * ----------------------------------------------------------------------- */
295  : MSTransportable(pars, vtype, plan) {
296 }
297 
298 
300 }
301 
302 
303 bool
305  MSTransportable::Stage* prior = *myStep;
306  prior->setArrived(time);
307  /*
308  if(myWriteEvents) {
309  (*myStep)->endEventOutput(*this, time, OutputDevice::getDeviceByOption("person-event-output"));
310  }
311  */
312  myStep++;
313  if (myStep != myPlan->end()) {
314  (*myStep)->proceed(net, this, time, prior);
315  /*
316  if(myWriteEvents) {
317  (*myStep)->beginEventOutput(*this, time, OutputDevice::getDeviceByOption("person-event-output"));
318  }
319  */
320  return true;
321  } else {
322  prior->getEdge()->removePerson(this);
323  return false;
324  }
325 }
326 
327 
328 const std::string&
330 // if (getCurrentStageType() == MOVING_WITHOUT_VEHICLE) {
331 // MSPersonStage_Walking* walkingStage = dynamic_cast<MSPersonStage_Walking*>(*myStep);
332 // assert(walkingStage != 0);
333 // const MSEdge* nextEdge = walkingStage->getPedestrianState()->getNextEdge(*walkingStage);
334 // if (nextEdge != 0) {
335 // return nextEdge->getID();
336 // }
337 // }
338 // return StringUtils::emptyString;
339  const MSEdge* nextEdge = getNextEdgePtr();
340  if (nextEdge != 0) {
341  return nextEdge->getID();
342  }
344 }
345 
346 
347 const MSEdge*
350  MSPersonStage_Walking* walkingStage = dynamic_cast<MSPersonStage_Walking*>(*myStep);
351  assert(walkingStage != 0);
352  return walkingStage->getPedestrianState()->getNextEdge(*walkingStage);
353 
354  }
355  return 0;
356 }
357 
358 
359 void
361  os.openTag("personinfo").writeAttr("id", getID()).writeAttr("depart", time2string(getDesiredDepart()));
362  for (MSTransportablePlan::const_iterator i = myPlan->begin(); i != myPlan->end(); ++i) {
363  (*i)->tripInfoOutput(os);
364  }
365  os.closeTag();
366 }
367 
368 
369 void
372  if (myStep == myPlan->end()) {
373  os.writeAttr("arrival", time2string(MSNet::getInstance()->getCurrentTimeStep()));
374  }
375  for (MSTransportablePlan::const_iterator i = myPlan->begin(); i != myPlan->end(); ++i) {
376  (*i)->routeOutput(os);
377  }
378  os.closeTag();
379  os.lf();
380 }
381 
382 /****************************************************************************/
383 
The departure is person triggered.
Position getWaitPosition() const
Returns the next free waiting place for pedestrians / containers.
virtual const MSEdge * getNextEdge(const MSPerson::MSPersonStage_Walking &stage) const =0
return the list of internal edges if the pedestrian is on an intersection
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:256
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:56
MSEdge & getEdge() const
Returns the lane&#39;s edge.
Definition: MSLane.h:582
virtual bool hasDeparted() const =0
Returns whether this vehicle has departed.
virtual void endEventOutput(const MSTransportable &p, SUMOTime t, OutputDevice &os) const
Called for writing the events output (end of an action)
Definition: MSPerson.cpp:200
virtual ~MSPerson()
destructor
Definition: MSPerson.cpp:299
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 SUMOTime getWaitingTime(const MSPerson::MSPersonStage_Walking &stage, SUMOTime now) const =0
return the time the person spent standing
virtual void proceed(MSNet *net, MSTransportable *person, SUMOTime now, Stage *previous)
proceeds to the next step
Definition: MSPerson.cpp:245
MSEdge * myCurrentInternalEdge
The current internal edge this person is on or 0.
Definition: MSPerson.h:198
MSPersonStage_Driving(const MSEdge &destination, MSStoppingPlace *toStop, const double arrivalPos, const std::vector< std::string > &lines)
constructor
Definition: MSPerson.cpp:236
virtual const MSEdge * getEdge() const =0
Returns the current edge.
virtual void tripInfoOutput(OutputDevice &os) const
Called on writing tripinfo output.
Definition: MSPerson.cpp:278
const MSEdge * getEdge() const
Returns the current edge.
Definition: MSPerson.cpp:80
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:60
Position getPosition(SUMOTime now) const
returns the position of the transportable
Definition: MSPerson.cpp:102
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:158
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:329
virtual void tripInfoOutput(OutputDevice &os) const
Called on writing tripinfo output.
Definition: MSPerson.cpp:360
const std::set< std::string > myLines
the lines to choose from
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:78
virtual bool proceed(MSNet *net, SUMOTime time)=0
const std::string & getID() const
Returns the id.
Definition: Named.h:66
MSTransportablePlan::iterator myStep
the iterator over the route
const MSEdge * getFromEdge() const
The simulated network and simulation perfomer.
Definition: MSNet.h:94
The car-following model and parameter.
Definition: MSVehicleType.h:74
virtual void erase(MSTransportable *transportable)
removes a single transportable
void addTransportable(MSTransportable *p)
adds a transportable to this stop
virtual Position getPosition(const MSPerson::MSPersonStage_Walking &stage, SUMOTime now) const =0
return the network coordinate of the person
PedestrianState * getPedestrianState() const
Definition: MSPerson.h:174
virtual void routeOutput(OutputDevice &os) const
Called on writing vehroute output.
Definition: MSPerson.cpp:370
virtual PedestrianState * add(MSPerson *person, MSPerson::MSPersonStage_Walking *stage, SUMOTime now)=0
register the given person as a pedestrian
virtual void beginEventOutput(const MSTransportable &p, SUMOTime t, OutputDevice &os) const
Called for writing the events output.
Definition: MSPerson.cpp:193
static double interpretEdgePos(double pos, double maximumValue, SumoXMLAttr attr, const std::string &id)
Interprets negative edge positions and fits them onto a given edge.
bool isWaiting4Vehicle() const
Whether the person waits for a vehicle.
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:131
ConstMSEdgeVector myRoute
The route of the person.
Definition: MSPerson.h:192
A road/street connecting two junctions.
Definition: MSEdge.h:80
std::vector< MSTransportable::Stage * > MSTransportablePlan
the structure holding the plan of a transportable
void abort(MSTransportable *)
abort this stage (TraCI)
Definition: MSPerson.cpp:151
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:207
the edges of a route
SUMOTime myDeparted
the time at which this stage started
Representation of a vehicle.
Definition: SUMOVehicle.h:67
MSStoppingPlace *const myDestinationStop
the stop to reach by getting transported (if any)
virtual void routeOutput(OutputDevice &os) const
Called on writing vehroute output.
Definition: MSPerson.cpp:181
virtual double getEdgePos(SUMOTime now) const =0
static MSPModel * getModel()
Definition: MSPModel.cpp:63
virtual MSTransportableControl & getPersonControl()
Returns the person control.
Definition: MSNet.cpp:730
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
SUMOTime getDesiredDepart() const
Returns the desired departure time.
ConstMSEdgeVector getEdges() const
the edges of the current stage
Definition: MSPerson.cpp:126
virtual void addPerson(MSTransportable *p) const
Definition: MSEdge.h:612
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:310
virtual void tripInfoOutput(OutputDevice &os) const
Called on writing tripinfo output.
Definition: MSPerson.cpp:175
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
double getAngle(SUMOTime now) const
returns the angle of the transportable
Definition: MSPerson.cpp:108
DepartDefinition departProcedure
Information how the vehicle shall choose the depart time.
std::string getStageDescription() const
returns the stage description as a string
Definition: MSPerson.cpp:272
virtual double getSpeed(const MSPerson::MSPersonStage_Walking &stage) const =0
return the current speed of the person
SUMOVehicle * getWaitingVehicle(const MSEdge *const edge, const std::set< std::string > &lines, const double position, const std::string ridingID)
const std::string & getID() const
returns the id of the transportable
virtual void removePerson(MSTransportable *p) const
Definition: MSEdge.h:616
ConstMSEdgeVector::iterator myRouteStep
Definition: MSPerson.h:195
SUMOVehicle * myVehicle
The taken vehicle.
void setSpeed(double speed)
sets the walking speed (ignored in other stages)
Definition: MSPerson.cpp:157
virtual void routeOutput(OutputDevice &os) const
Called on writing vehroute output.
Definition: MSPerson.cpp:284
void unregisterOneWaitingForPerson()
decreases the count of vehicles waiting for a person to allow recogniztion of person related deadlock...
static std::string emptyString
An empty string.
Definition: StringUtils.h:85
double computeAverageSpeed() const
Definition: MSPerson.cpp:163
Structure representing possible vehicle parameter.
SUMOTime getWaitingTime(SUMOTime now) const
the time this transportable spent waiting
Definition: MSPerson.cpp:114
const MSStoppingPlace * getDestinationStop() const
returns the destination stop (if any)
MSInsertionControl & getInsertionControl()
Returns the insertion control.
Definition: MSNet.h:363
virtual const SUMOVehicleParameter & getParameter() const =0
Returns the vehicle&#39;s parameter (including departure definition)
SUMOTime myWalkingTime
the time the person is walking
Definition: MSPerson.h:189
double getSpeed() const
the speed of the transportable
Definition: MSPerson.cpp:120
~MSPersonStage_Walking()
destructor
Definition: MSPerson.cpp:75
PedestrianState * myPedestrianState
state that is to be manipulated by MSPModel
Definition: MSPerson.h:205
bool proceed(MSNet *net, SUMOTime time)
Definition: MSPerson.cpp:304
virtual void addPerson(MSTransportable *person)=0
Adds a person to this vehicle.
const MSEdge & getDestination() const
returns the destination edge
const MSEdge * getNextEdgePtr() const
returns the next edge ptr if this person is walking and the pedestrian model allows it ...
Definition: MSPerson.cpp:348
MSPerson(const SUMOVehicleParameter *pars, const MSVehicleType *vtype, MSTransportable::MSTransportablePlan *plan)
constructor
Definition: MSPerson.cpp:294
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
const MSLane & getLane() const
Returns the lane this stop is located at.
void add(SUMOVehicle *veh)
Adds a single vehicle for departure.
const MSEdge * getFromEdge() const
Definition: MSPerson.cpp:90
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=gPrecision)
Definition: ToString.h:228
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
double getEdgePos(SUMOTime now) const
Definition: MSPerson.cpp:96
void lf()
writes a line feed if applicable
Definition: OutputDevice.h:234
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
StageType getCurrentStageType() const
the current stage type of the transportable