SUMO - Simulation of Urban MObility
MSPersonControl.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // Stores all persons in the net and handles their waiting for cars.
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 <vector>
35 #include <algorithm>
36 #include "MSNet.h"
37 #include "MSEdge.h"
39 #include "MSVehicle.h"
40 #include "MSPersonControl.h"
43 
44 #ifdef CHECK_MEMORY_LEAKS
45 #include <foreign/nvwa/debug_new.h>
46 #endif // CHECK_MEMORY_LEAKS
47 
48 
49 // ===========================================================================
50 // method definitions
51 // ===========================================================================
53  myLoadedPersonNumber(0),
54  myRunningPersonNumber(0),
55  myJammedPersonNumber(0) {
56 }
57 
58 
60  for (std::map<std::string, MSTransportable*>::iterator i = myPersons.begin(); i != myPersons.end(); ++i) {
61  delete(*i).second;
62  }
63  myPersons.clear();
64  myWaiting4Vehicle.clear();
65 }
66 
67 
68 bool
69 MSPersonControl::add(const std::string& id, MSPerson* person) {
70  if (myPersons.find(id) == myPersons.end()) {
71  myPersons[id] = person;
74  return true;
75  }
76  return false;
77 }
78 
79 
81 MSPersonControl::get(const std::string& id) const {
82  std::map<std::string, MSTransportable*>::const_iterator i = myPersons.find(id);
83  if (i == myPersons.end()) {
84  return 0;
85  }
86  return (*i).second;
87 }
88 
89 
90 void
92  const std::string& id = person->getID();
93  if (OptionsCont::getOptions().isSet("tripinfo-output")) {
94  OutputDevice& od = OutputDevice::getDeviceByOption("tripinfo-output");
95  od.openTag("personinfo").writeAttr("id", id).writeAttr("depart", time2string(person->getDesiredDepart()));
96  person->tripInfoOutput(od);
97  od.closeTag();
98  }
99  if (OptionsCont::getOptions().isSet("vehroute-output")) {
100  OutputDevice& od = OutputDevice::getDeviceByOption("vehroute-output");
101  od.openTag("person").writeAttr("id", id).writeAttr("depart", time2string(person->getDesiredDepart())).writeAttr("arrival", time2string(MSNet::getInstance()->getCurrentTimeStep()));
102  person->routeOutput(od);
103  od.closeTag();
104  od << "\n";
105  }
106  const std::map<std::string, MSTransportable*>::iterator i = myPersons.find(id);
107  if (i != myPersons.end()) {
109  delete i->second;
110  myPersons.erase(i);
111  }
112 }
113 
114 
115 void
117  const SUMOTime step = time % DELTA_T == 0 ? time : (time / DELTA_T + 1) * DELTA_T;
118  if (myWaiting4Departure.find(step) == myWaiting4Departure.end()) {
120  }
121  myWaiting4Departure[step].push_back(person);
122 }
123 
124 
125 void
127  const SUMOTime step = time % DELTA_T == 0 ? time : (time / DELTA_T + 1) * DELTA_T;
128  if (myWaitingUntil.find(step) == myWaitingUntil.end()) {
129  myWaitingUntil[step] = PersonVector();
130  }
131  myWaitingUntil[step].push_back(person);
132 }
133 
134 
135 void
137  while (myWaiting4Departure.find(time) != myWaiting4Departure.end()) {
138  const PersonVector& persons = myWaiting4Departure[time];
139  // we cannot use an iterator here because there might be additions to the vector while proceeding
140  for (size_t i = 0; i < persons.size(); ++i) {
141  if (!persons[i]->proceed(net, time)) {
142  erase(persons[i]);
143  }
144  }
145  myWaiting4Departure.erase(time);
146  }
147  while (myWaitingUntil.find(time) != myWaitingUntil.end()) {
148  const PersonVector& persons = myWaitingUntil[time];
149  // we cannot use an iterator here because there might be additions to the vector while proceeding
150  for (size_t i = 0; i < persons.size(); ++i) {
151  if (!persons[i]->proceed(net, time)) {
152  erase(persons[i]);
153  }
154  }
155  myWaitingUntil.erase(time);
156  }
157 }
158 
159 
160 void
162  if (myWaiting4Vehicle.find(edge) == myWaiting4Vehicle.end()) {
163  myWaiting4Vehicle[edge] = std::vector<MSTransportable*>();
164  }
165  myWaiting4Vehicle[edge].push_back(person);
166 }
167 
168 
169 bool
170 MSPersonControl::isWaiting4Vehicle(const MSEdge* const edge, MSPerson* /* p */) const {
171  return myWaiting4Vehicle.find(edge) != myWaiting4Vehicle.end();
172 }
173 
174 
175 bool
177  bool ret = false;
178  if (myWaiting4Vehicle.find(edge) != myWaiting4Vehicle.end()) {
179  PersonVector& waitPersons = myWaiting4Vehicle[edge];
180  const std::string& line = vehicle->getParameter().line == "" ? vehicle->getParameter().id : vehicle->getParameter().line;
182  for (PersonVector::iterator i = waitPersons.begin(); i != waitPersons.end();) {
183  if ((*i)->isWaitingFor(line) && vehicle->getVehicleType().getPersonCapacity() > vehicle->getPersonNumber() && stop->timeToBoardNextPerson <= currentTime && stop->startPos <= (*i)->getEdgePos() && (*i)->getEdgePos() <= stop->endPos) {
184  edge->removePerson(*i);
185  vehicle->addPerson(*i);
186  //if the time a person needs to enter the vehicle extends the duration of the stop of the vehicle extend
187  //the duration by setting it to the boarding duration of the person
188  const SUMOTime boardingDuration = vehicle->getVehicleType().getBoardingDuration();
189  if (boardingDuration >= stop->duration) {
190  stop->duration = boardingDuration;
191  }
192  //update the time point at which the next person can board the vehicle
193  if (stop->timeToBoardNextPerson > currentTime - DELTA_T) {
194  stop->timeToBoardNextPerson += boardingDuration;
195  } else {
196  stop->timeToBoardNextPerson = currentTime + boardingDuration;
197  }
198 
199  static_cast<MSPerson::MSPersonStage_Driving*>((*i)->getCurrentStage())->setVehicle(vehicle);
200  i = waitPersons.erase(i);
201  ret = true;
202  } else {
203  ++i;
204  }
205  }
206  if (waitPersons.size() == 0) {
207  myWaiting4Vehicle.erase(myWaiting4Vehicle.find(edge));
208  }
209  }
210  return ret;
211 }
212 
213 bool
215  return !myPersons.empty();
216 }
217 
218 
219 bool
221  return !myWaiting4Departure.empty() || !myWaitingUntil.empty() || !myWalking.empty();
222 }
223 
224 
225 void
227  myWalking[p->getID()] = p;
228 }
229 
230 
231 void
233  std::map<std::string, MSTransportable*>::iterator i = myWalking.find(p->getID());
234  if (i != myWalking.end()) {
235  myWalking.erase(i);
236  }
237 }
238 
239 
240 void
242  for (std::map<const MSEdge*, PersonVector>::const_iterator i = myWaiting4Vehicle.begin(); i != myWaiting4Vehicle.end(); ++i) {
243  const MSEdge* edge = (*i).first;
244  const PersonVector& pv = (*i).second;
245  for (PersonVector::const_iterator j = pv.begin(); j != pv.end(); ++j) {
246  MSTransportable* p = (*j);
247  edge->removePerson(p);
248  WRITE_WARNING("Person '" + p->getID() + "' aborted waiting for a ride that will never come.");
249  erase(p);
250  }
251  }
252 }
253 
254 
255 MSPerson*
257  return new MSPerson(pars, vtype, plan);
258 }
259 
260 /****************************************************************************/
SUMOTime getBoardingDuration() const
Get this vehicle type&#39;s boarding duration.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:257
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:80
long long int SUMOTime
Definition: SUMOTime.h:43
SUMOTime timeToBoardNextPerson
The time at which the vehicle is able to board another person.
Definition: MSVehicle.h:679
virtual void erase(MSTransportable *person)
removes a single person
void tripInfoOutput(OutputDevice &os) const
Called on writing tripinfo output.
virtual MSPerson * buildPerson(const SUMOVehicleParameter *pars, const MSVehicleType *vtype, MSTransportable::MSTransportablePlan *plan) const
Builds a new person.
std::map< SUMOTime, PersonVector > myWaiting4Departure
Persons waiting for departure.
unsigned int myRunningPersonNumber
The number of persons within the network (build and inserted but not removed)
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:160
unsigned int getPersonNumber() const
Returns the number of persons.
Definition: MSVehicle.cpp:2562
SUMOTime DELTA_T
Definition: SUMOTime.cpp:39
const std::string & getID() const
returns the id of the transportable
Definition of vehicle stop (position and duration)
Definition: MSVehicle.h:647
void setWalking(MSTransportable *p)
MSPersonControl()
Constructor.
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:255
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
The simulated network and simulation perfomer.
Definition: MSNet.h:94
The car-following model and parameter.
Definition: MSVehicleType.h:74
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:69
void setWaitEnd(SUMOTime time, MSTransportable *person)
sets the arrival time for a waiting or walking person
bool hasPersons() const
checks whether any person waits to finish her plan
SUMOTime getDesiredDepart() const
Returns the desired departure time.
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 removePerson(MSTransportable *p) const
Definition: MSEdge.h:589
bool add(const std::string &id, MSPerson *person)
Adds a single person, returns false if an id clash occured.
void abortWaiting()
aborts the plan for any person that is still waiting for a ride
void checkWaitingPersons(MSNet *net, const SUMOTime time)
checks whether any persons waiting or walking time is over
virtual void routeOutput(OutputDevice &os) const =0
Called on writing vehroute output.
std::map< std::string, MSTransportable * > myWalking
all persons by id
SUMOTime duration
The stopping duration.
Definition: MSVehicle.h:663
unsigned int getPersonCapacity() const
Get this vehicle type&#39;s person capacity.
MSTransportable * get(const std::string &id) const
Returns the named person, if existing.
std::string line
The vehicle&#39;s line (mainly for public transport)
std::map< const MSEdge *, PersonVector > myWaiting4Vehicle
the lists of waiting persons
std::map< std::string, MSTransportable * > myPersons
all persons by id
void unsetWalking(MSTransportable *p)
bool hasNonWaiting() const
checks whether any person is still engaged in walking / stopping
void addWaiting(const MSEdge *edge, MSTransportable *person)
adds a person to the list of persons waiting for a vehicle on the specified edge
unsigned int myLoadedPersonNumber
The number of build persons.
Structure representing possible vehicle parameter.
static OutputDevice & getDeviceByOption(const std::string &name)
Returns the device described by the option.
std::map< SUMOTime, PersonVector > myWaitingUntil
the lists of walking / stopping persons
const MSVehicleType & getVehicleType() const
Returns the vehicle&#39;s type definition.
Definition: MSBaseVehicle.h:93
void setDeparture(SUMOTime time, MSPerson *person)
sets the arrival time for a waiting or walking person
const SUMOVehicleParameter & getParameter() const
Returns the vehicle&#39;s parameter (including departure definition)
SUMOReal endPos
The stopping position end.
Definition: MSVehicle.h:661
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
bool closeTag()
Closes the most recently opened tag.
void addPerson(MSTransportable *person)
Adds a passenger.
Definition: MSVehicle.cpp:2500
SUMOReal startPos
The stopping position start.
Definition: MSVehicle.h:659
std::vector< MSTransportable * > PersonVector
Definition of a list of persons.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
virtual ~MSPersonControl()
Destructor.
bool boardAnyWaiting(MSEdge *edge, MSVehicle *vehicle, MSVehicle::Stop *stop)
board any applicable persons Boards any people who wait on that edge for the given vehicle and remove...
bool isWaiting4Vehicle(const MSEdge *const edge, MSPerson *p) const
returns whether the the given person is waiting for a vehicle on the given edge
std::string id
The vehicle&#39;s id.