SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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-2016 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) :
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  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 
100  return myPedestrianState->getEdgePos(*this, now);
101 }
102 
103 
104 Position
106  return myPedestrianState->getPosition(*this, now);
107 }
108 
109 
110 SUMOReal
112  return myPedestrianState->getAngle(*this, now);
113 }
114 
115 
116 SUMOTime
118  return myPedestrianState->getWaitingTime(*this, now);
119 }
120 
121 
122 SUMOReal
124  return myPedestrianState->getSpeed(*this);
125 }
126 
127 
128 void
130  previous->getEdge()->removePerson(person);
131  myRouteStep = myRoute.begin();
132  if (myWalkingTime == 0) {
133  if (!person->proceed(net, now)) {
135  }
136  return;
137  }
138  if (previous->getEdgePos(now) >= 0) {
139  myDepartPos = previous->getEdgePos(now);
140  if (myWalkingTime > 0) {
141  mySpeed = computeAverageSpeed();
142  }
143  }
144  myPedestrianState = MSPModel::getModel()->add(dynamic_cast<MSPerson*>(person), this, now);
145  (*myRouteStep)->addPerson(person);
146 }
147 
148 
149 SUMOReal
151  SUMOReal length = 0;
152  for (ConstMSEdgeVector::const_iterator i = myRoute.begin(); i != myRoute.end(); ++i) {
153  length += (*i)->getLength();
154  }
155  length -= myDepartPos;
156  length -= myRoute.back()->getLength() - myArrivalPos;
157  return length / STEPS2TIME(myWalkingTime + 1); // avoid systematic rounding errors
158 }
159 
160 
161 void
163  os.openTag("walk").writeAttr("arrival", time2string(myArrived)).closeTag();
164 }
165 
166 
167 void
169  os.openTag("walk").writeAttr(SUMO_ATTR_EDGES, myRoute);
170  if (myWalkingTime > 0) {
171  os.writeAttr(SUMO_ATTR_DURATION, time2string(myWalkingTime));
172  } else if (mySpeed > 0) {
173  os.writeAttr(SUMO_ATTR_SPEED, mySpeed);
174  }
175  os.closeTag();
176 }
177 
178 
179 void
181  os.openTag("event").writeAttr("time", time2string(t)).writeAttr("type", "departure")
182  .writeAttr("agent", p.getID()).writeAttr("link", myRoute.front()->getID()).closeTag();
183 }
184 
185 
186 void
188  os.openTag("event").writeAttr("time", time2string(t)).writeAttr("type", "arrival")
189  .writeAttr("agent", p.getID()).writeAttr("link", myRoute.back()->getID()).closeTag();
190 }
191 
192 
193 bool
195  ((MSEdge*)getEdge())->removePerson(person);
196  //std::cout << SIMTIME << " moveToNextEdge person=" << person->getID() << "\n";
197  if (myRouteStep == myRoute.end() - 1) {
198  if (myDestinationStop != 0) {
199  myDestinationStop->addTransportable(person);
200  }
201  if (!person->proceed(MSNet::getInstance(), currentTime)) {
203  }
204  //std::cout << " end walk. myRouteStep=" << (*myRouteStep)->getID() << "\n";
205  return true;
206  } else {
207  if (nextInternal == 0) {
208  ++myRouteStep;
209  myCurrentInternalEdge = 0;
210  } else {
211  myCurrentInternalEdge = nextInternal;
212  }
213  ((MSEdge*) getEdge())->addPerson(person);
214  return false;
215  }
216 }
217 
218 
219 
220 /* -------------------------------------------------------------------------
221  * MSPerson::MSPersonStage_Driving - methods
222  * ----------------------------------------------------------------------- */
224  MSStoppingPlace* toStop, const SUMOReal arrivalPos, const std::vector<std::string>& lines)
225  : MSTransportable::Stage_Driving(destination, toStop, arrivalPos, lines) {}
226 
227 
229 
230 
231 void
233  if (previous->getDestinationStop() != 0) {
234  // the arrival stop may have an access point
235  myWaitingEdge = &previous->getDestinationStop()->getLane().getEdge();
236  myStopWaitPos = previous->getDestinationStop()->getWaitPosition();
237  } else {
238  myWaitingEdge = previous->getEdge();
239  myStopWaitPos = Position::INVALID;
240  }
241  myWaitingPos = previous->getEdgePos(now);
242  myWaitingSince = now;
243  SUMOVehicle* availableVehicle = net->getVehicleControl().getWaitingVehicle(myWaitingEdge, myLines, myWaitingPos, person->getID());
244  if (availableVehicle != 0 && availableVehicle->getParameter().departProcedure == DEPART_TRIGGERED && !availableVehicle->hasDeparted()) {
245  myVehicle = availableVehicle;
246  myWaitingEdge->removePerson(person);
247  myVehicle->addPerson(person);
248  net->getInsertionControl().add(myVehicle);
249  net->getVehicleControl().removeWaiting(myWaitingEdge, myVehicle);
251  } else {
252  net->getPersonControl().addWaiting(myWaitingEdge, person);
253  myWaitingEdge->addPerson(person);
254  }
255 }
256 
257 
258 std::string
260  return isWaiting4Vehicle() ? "waiting for " + joinToString(myLines, ",") : "driving";
261 }
262 
263 
264 void
266  os.openTag("ride").writeAttr("depart", time2string(myDeparted)).writeAttr("arrival", time2string(myArrived)).closeTag();
267 }
268 
269 
270 void
273  os.writeAttr(SUMO_ATTR_LINES, myLines).closeTag();
274 }
275 
276 
277 
278 /* -------------------------------------------------------------------------
279  * MSPerson - methods
280  * ----------------------------------------------------------------------- */
282  : MSTransportable(pars, vtype, plan) {
283 }
284 
285 
287 }
288 
289 
290 bool
292  MSTransportable::Stage* prior = *myStep;
293  prior->setArrived(time);
294  /*
295  if(myWriteEvents) {
296  (*myStep)->endEventOutput(*this, time, OutputDevice::getDeviceByOption("person-event-output"));
297  }
298  */
299  myStep++;
300  if (myStep != myPlan->end()) {
301  (*myStep)->proceed(net, this, time, prior);
302  /*
303  if(myWriteEvents) {
304  (*myStep)->beginEventOutput(*this, time, OutputDevice::getDeviceByOption("person-event-output"));
305  }
306  */
307  return true;
308  } else {
309  prior->getEdge()->removePerson(this);
310  return false;
311  }
312 }
313 
314 
315 const std::string&
317 // if (getCurrentStageType() == MOVING_WITHOUT_VEHICLE) {
318 // MSPersonStage_Walking* walkingStage = dynamic_cast<MSPersonStage_Walking*>(*myStep);
319 // assert(walkingStage != 0);
320 // const MSEdge* nextEdge = walkingStage->getPedestrianState()->getNextEdge(*walkingStage);
321 // if (nextEdge != 0) {
322 // return nextEdge->getID();
323 // }
324 // }
325 // return StringUtils::emptyString;
326  const MSEdge* nextEdge = getNextEdgePtr();
327  if (nextEdge != 0) {
328  return nextEdge->getID();
329  }
331 }
332 
333 
334 const MSEdge*
337  MSPersonStage_Walking* walkingStage = dynamic_cast<MSPersonStage_Walking*>(*myStep);
338  assert(walkingStage != 0);
339  return walkingStage->getPedestrianState()->getNextEdge(*walkingStage);
340 
341  }
342  return 0;
343 }
344 
345 
346 void
348  os.openTag("personinfo").writeAttr("id", getID()).writeAttr("depart", time2string(getDesiredDepart()));
349  for (MSTransportablePlan::const_iterator i = myPlan->begin(); i != myPlan->end(); ++i) {
350  (*i)->tripInfoOutput(os);
351  }
352  os.closeTag();
353 }
354 
355 
356 void
359  if (myStep == myPlan->end()) {
360  os.writeAttr("arrival", time2string(MSNet::getInstance()->getCurrentTimeStep()));
361  }
362  for (MSTransportablePlan::const_iterator i = myPlan->begin(); i != myPlan->end(); ++i) {
363  (*i)->routeOutput(os);
364  }
365  os.closeTag();
366  os.lf();
367 }
368 
369 /****************************************************************************/
370 
The departure is person triggered.
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:257
void setArrived(SUMOTime now)
logs end of the step
MSEdge & getEdge() const
Returns the lane's edge.
Definition: MSLane.h:571
long long int SUMOTime
Definition: SUMOTime.h:43
virtual bool hasDeparted() const =0
Returns whether this vehicle has departed.
virtual ~MSPerson()
destructor
Definition: MSPerson.cpp:286
A lane area vehicles can halt at.
bool isWaiting4Vehicle() const
Whether the transportable waits for a vehicle.
virtual void proceed(MSNet *net, MSTransportable *person, SUMOTime now, Stage *previous)
proceeds to the next step
Definition: MSPerson.cpp:232
virtual const MSEdge * getEdge() const =0
Returns the current edge.
const MSStoppingPlace * getDestinationStop() const
returns the destination stop (if any)
virtual SUMOReal getEdgePos(SUMOTime now) const =0
Position getPosition(SUMOTime now) const
returns the position of the transportable
Definition: MSPerson.cpp:105
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:59
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:335
virtual void routeOutput(OutputDevice &os) const
Called on writing vehroute output.
Definition: MSPerson.cpp:168
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
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:180
virtual void tripInfoOutput(OutputDevice &os) const
Called on writing tripinfo output.
Definition: MSPerson.cpp:265
The simulated network and simulation perfomer.
Definition: MSNet.h:93
The car-following model and parameter.
Definition: MSVehicleType.h:74
SUMOReal getEdgePos(SUMOTime now) const
Definition: MSPerson.cpp:99
virtual void erase(MSTransportable *transportable)
removes a single transportable
SUMOTime getDesiredDepart() const
Returns the desired departure time.
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
static SUMOReal interpretEdgePos(SUMOReal pos, SUMOReal maximumValue, SumoXMLAttr attr, const std::string &id)
Interprets negative edge positions and fits them onto a given edge.
virtual void proceed(MSNet *net, MSTransportable *person, SUMOTime now, Stage *previous)
proceeds to the next step
Definition: MSPerson.cpp:129
ConstMSEdgeVector myRoute
The route of the person.
Definition: MSPerson.h:177
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:357
virtual void removePerson(MSTransportable *p) const
Definition: MSEdge.h:618
const MSLane & getLane() const
Returns the lane this stop is located at.
bool moveToNextEdge(MSPerson *person, SUMOTime currentTime, MSEdge *nextInternal=0)
move forward and return whether the person arrived
Definition: MSPerson.cpp:194
the edges of a route
const MSEdge * getEdge() const
Returns the current edge.
PedestrianState * getPedestrianState() const
Definition: MSPerson.h:159
Representation of a vehicle.
Definition: SUMOVehicle.h:66
static MSPModel * getModel()
Definition: MSPModel.cpp:63
virtual MSTransportableControl & getPersonControl()
Returns the person control.
Definition: MSNet.cpp:699
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
SUMOReal computeAverageSpeed() const
Definition: MSPerson.cpp:150
StageType getCurrentStageType() const
the current stage type of the transportable
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:307
#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:271
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...
const MSEdge & getDestination() const
Returns the current destination.
static std::string emptyString
An empty string.
Definition: StringUtils.h:84
SUMOReal getAngle(SUMOTime now) const
returns the angle of the transportable
Definition: MSPerson.cpp:111
const MSEdge * getFromEdge() const
Returns the departure edge.
virtual void endEventOutput(const MSTransportable &p, SUMOTime t, OutputDevice &os) const
Called for writing the events output (end of an action)
Definition: MSPerson.cpp:187
MSPersonStage_Driving(const MSEdge &destination, MSStoppingPlace *toStop, const SUMOReal arrivalPos, const std::vector< std::string > &lines)
constructor
Definition: MSPerson.cpp:223
Structure representing possible vehicle parameter.
virtual void tripInfoOutput(OutputDevice &os) const
Called on writing tripinfo output.
Definition: MSPerson.cpp:347
MSInsertionControl & getInsertionControl()
Returns the insertion control.
Definition: MSNet.h:360
virtual const SUMOVehicleParameter & getParameter() const =0
Returns the vehicle's parameter (including departure definition)
~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:316
std::string getStageDescription() const
returns the stage description as a string
Definition: MSPerson.cpp:259
bool proceed(MSNet *net, SUMOTime time)
Definition: MSPerson.cpp:291
virtual void addPerson(MSTransportable *person)=0
Adds a person to this vehicle.
const MSEdge * getFromEdge() const
Definition: MSPerson.cpp:93
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:188
MSPerson(const SUMOVehicleParameter *pars, const MSVehicleType *vtype, MSTransportable::MSTransportablePlan *plan)
constructor
Definition: MSPerson.cpp:281
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:214
MSTransportablePlan * myPlan
the plan of the transportable
MSPersonStage_Walking(const ConstMSEdgeVector &route, MSStoppingPlace *toStop, SUMOTime walkingTime, SUMOReal speed, SUMOReal departPos, SUMOReal arrivalPos)
constructor
Definition: MSPerson.cpp:60
SUMOReal getSpeed() const
the speed of the transportable
Definition: MSPerson.cpp:123
void add(SUMOVehicle *veh)
Adds a single vehicle for departure.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
void lf()
writes a line feed if applicable
Definition: OutputDevice.h:235
virtual void tripInfoOutput(OutputDevice &os) const
Called on writing tripinfo output.
Definition: MSPerson.cpp:162
static const Position INVALID
Definition: Position.h:261