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