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-2015 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 
54 
56  for (std::map<std::string, MSPerson*>::iterator i = myPersons.begin(); i != myPersons.end(); ++i) {
57  delete(*i).second;
58  }
59  myPersons.clear();
60  myWaiting4Vehicle.clear();
61 }
62 
63 
64 bool
65 MSPersonControl::add(const std::string& id, MSPerson* person) {
66  if (myPersons.find(id) == myPersons.end()) {
67  myPersons[id] = person;
68  return true;
69  }
70  return false;
71 }
72 
73 
74 MSPerson*
75 MSPersonControl::get(const std::string& id) const {
76  std::map<std::string, MSPerson*>::const_iterator i = myPersons.find(id);
77  if (i == myPersons.end()) {
78  return 0;
79  }
80  return (*i).second;
81 }
82 
83 
84 void
86  const std::string& id = person->getID();
87  if (OptionsCont::getOptions().isSet("tripinfo-output")) {
88  OutputDevice& od = OutputDevice::getDeviceByOption("tripinfo-output");
89  od.openTag("personinfo").writeAttr("id", id).writeAttr("depart", time2string(person->getDesiredDepart()));
90  person->tripInfoOutput(od);
91  od.closeTag();
92  }
93  if (OptionsCont::getOptions().isSet("vehroute-output")) {
94  OutputDevice& od = OutputDevice::getDeviceByOption("vehroute-output");
95  od.openTag("person").writeAttr("id", id).writeAttr("depart", time2string(person->getDesiredDepart())).writeAttr("arrival", time2string(MSNet::getInstance()->getCurrentTimeStep()));
96  person->routeOutput(od);
97  od.closeTag();
98  od << "\n";
99  }
100  const std::map<std::string, MSPerson*>::iterator i = myPersons.find(id);
101  if (i != myPersons.end()) {
102  delete i->second;
103  myPersons.erase(i);
104  }
105 }
106 
107 
108 void
110  const SUMOTime step = time % DELTA_T == 0 ? time : (time / DELTA_T + 1) * DELTA_T;
111  if (myWaiting4Departure.find(step) == myWaiting4Departure.end()) {
113  }
114  myWaiting4Departure[step].push_back(person);
115 }
116 
117 
118 void
120  const SUMOTime step = time % DELTA_T == 0 ? time : (time / DELTA_T + 1) * DELTA_T;
121  if (myWaitingUntil.find(step) == myWaitingUntil.end()) {
122  myWaitingUntil[step] = PersonVector();
123  }
124  myWaitingUntil[step].push_back(person);
125 }
126 
127 
128 void
130  while (myWaiting4Departure.find(time) != myWaiting4Departure.end()) {
131  const PersonVector& persons = myWaiting4Departure[time];
132  // we cannot use an iterator here because there might be additions to the vector while proceeding
133  for (size_t i = 0; i < persons.size(); ++i) {
134  if (!persons[i]->proceed(net, time)) {
135  erase(persons[i]);
136  }
137  }
138  myWaiting4Departure.erase(time);
139  }
140  while (myWaitingUntil.find(time) != myWaitingUntil.end()) {
141  const PersonVector& persons = myWaitingUntil[time];
142  // we cannot use an iterator here because there might be additions to the vector while proceeding
143  for (size_t i = 0; i < persons.size(); ++i) {
144  if (!persons[i]->proceed(net, time)) {
145  erase(persons[i]);
146  }
147  }
148  myWaitingUntil.erase(time);
149  }
150 }
151 
152 
153 void
154 MSPersonControl::addWaiting(const MSEdge* const edge, MSPerson* person) {
155  if (myWaiting4Vehicle.find(edge) == myWaiting4Vehicle.end()) {
156  myWaiting4Vehicle[edge] = std::vector<MSPerson*>();
157  }
158  myWaiting4Vehicle[edge].push_back(person);
159 }
160 
161 
162 bool
163 MSPersonControl::isWaiting4Vehicle(const MSEdge* const edge, MSPerson* /* p */) const {
164  return myWaiting4Vehicle.find(edge) != myWaiting4Vehicle.end();
165 }
166 
167 
168 bool
170  bool ret = false;
171  if (myWaiting4Vehicle.find(edge) != myWaiting4Vehicle.end()) {
172  PersonVector& waitPersons = myWaiting4Vehicle[edge];
173  const std::string& line = vehicle->getParameter().line == "" ? vehicle->getParameter().id : vehicle->getParameter().line;
175  for (PersonVector::iterator i = waitPersons.begin(); i != waitPersons.end();) {
176  if ((*i)->isWaitingFor(line) && vehicle->getVehicleType().getPersonCapacity() > vehicle->getPersonNumber() && stop->timeToBoardNextPerson <= currentTime && stop->startPos <= (*i)->getEdgePos() && (*i)->getEdgePos() <= stop->endPos) {
177  edge->removePerson(*i);
178  vehicle->addPerson(*i);
179  //if the time a person needs to enter the vehicle extends the duration of the stop of the vehicle extend
180  //the duration by setting it to the boarding duration of the person
181  const SUMOTime boardingDuration = vehicle->getVehicleType().getBoardingDuration();
182  if (boardingDuration >= stop->duration) {
183  stop->duration = boardingDuration;
184  }
185  //update the time point at which the next person can board the vehicle
186  stop->timeToBoardNextPerson = currentTime + boardingDuration;
187 
188  static_cast<MSPerson::MSPersonStage_Driving*>((*i)->getCurrentStage())->setVehicle(vehicle);
189  i = waitPersons.erase(i);
190  ret = true;
191  } else {
192  ++i;
193  }
194  }
195  if (waitPersons.size() == 0) {
196  myWaiting4Vehicle.erase(myWaiting4Vehicle.find(edge));
197  }
198  }
199  return ret;
200 }
201 
202 bool
204  return !myPersons.empty();
205 }
206 
207 
208 bool
210  return !myWaiting4Departure.empty() || !myWaitingUntil.empty() || !myWalking.empty();
211 }
212 
213 
214 void
216  myWalking[p->getID()] = p;
217 }
218 
219 
220 void
222  std::map<std::string, MSPerson*>::iterator i = myWalking.find(p->getID());
223  if (i != myWalking.end()) {
224  myWalking.erase(i);
225  }
226 }
227 
228 
229 void
231  for (std::map<const MSEdge*, PersonVector>::const_iterator i = myWaiting4Vehicle.begin(); i != myWaiting4Vehicle.end(); ++i) {
232  const MSEdge* edge = (*i).first;
233  const PersonVector& pv = (*i).second;
234  for (PersonVector::const_iterator j = pv.begin(); j != pv.end(); ++j) {
235  MSPerson* p = (*j);
236  edge->removePerson(p);
237  WRITE_WARNING("Person " + p->getID() + " aborted waiting for a ride that will never come.");
238  erase(p);
239  }
240  }
241 }
242 
243 
244 MSPerson*
246  return new MSPerson(pars, vtype, plan);
247 }
248 
249 /****************************************************************************/
SUMOTime getBoardingDuration() const
Get this vehicle type's boarding duration.
const std::string & getID() const
returns the person id
Definition: MSPerson.cpp:552
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:257
virtual MSPerson * buildPerson(const SUMOVehicleParameter *pars, const MSVehicleType *vtype, MSPerson::MSPersonPlan *plan) const
Builds a new person.
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:80
SUMOTime getDesiredDepart() const
Returns the desired departure time.
Definition: MSPerson.cpp:585
SUMOTime timeToBoardNextPerson
The time at which the vehicle is able to board another person.
Definition: MSVehicle.h:590
std::map< std::string, MSPerson * > myWalking
all persons by id
std::map< SUMOTime, PersonVector > myWaiting4Departure
Persons waiting for departure.
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:61
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:159
unsigned int getPersonNumber() const
Returns the number of persons.
Definition: MSVehicle.cpp:2364
Definition of vehicle stop (position and duration)
Definition: MSVehicle.h:560
MSPersonControl()
Constructor.
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:235
#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:67
bool hasPersons() const
checks whether any person waits to finish her plan
std::map< std::string, MSPerson * > myPersons
all persons by id
A road/street connecting two junctions.
Definition: MSEdge.h:81
void routeOutput(OutputDevice &os) const
Called on writing vehroute output.
Definition: MSPerson.cpp:605
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 addWaiting(const MSEdge *edge, MSPerson *person)
adds a person to the list of persons waiting for a vehicle on the specified edge
void checkWaitingPersons(MSNet *net, const SUMOTime time)
checks whether any persons waiting or walking time is over
SUMOTime duration
The stopping duration.
Definition: MSVehicle.h:574
virtual void removePerson(MSPerson *p) const
Definition: MSEdge.h:560
void setWaitEnd(SUMOTime time, MSPerson *person)
sets the arrival time for a waiting or walking person
unsigned int getPersonCapacity() const
Get this vehicle type's person capacity.
void unsetWalking(MSPerson *p)
std::string line
The vehicle's line (mainly for public transport)
std::map< const MSEdge *, PersonVector > myWaiting4Vehicle
the lists of waiting persons
bool hasNonWaiting() const
checks whether any person is still engaged in walking / stopping
Structure representing possible vehicle parameter.
void tripInfoOutput(OutputDevice &os) const
Called on writing tripinfo output.
Definition: MSPerson.cpp:597
static OutputDevice & getDeviceByOption(const std::string &name)
Returns the device described by the option.
void setWalking(MSPerson *p)
std::map< SUMOTime, PersonVector > myWaitingUntil
the lists of walking / stopping persons
const MSVehicleType & getVehicleType() const
Returns the vehicle's type definition.
Definition: MSBaseVehicle.h:90
MSPerson * get(const std::string &id) const
Returns the named person, if existing.
void setDeparture(SUMOTime time, MSPerson *person)
sets the arrival time for a waiting or walking person
int SUMOTime
Definition: SUMOTime.h:43
const SUMOVehicleParameter & getParameter() const
Returns the vehicle's parameter (including departure definition)
SUMOReal endPos
The stopping position end.
Definition: MSVehicle.h:572
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
bool closeTag()
Closes the most recently opened tag.
#define DELTA_T
Definition: SUMOTime.h:50
std::vector< MSPerson * > PersonVector
Definition of a list of persons.
virtual void erase(MSPerson *person)
removes a single person
void addPerson(MSPerson *person)
Adds a passenger.
Definition: MSVehicle.cpp:2322
SUMOReal startPos
The stopping position start.
Definition: MSVehicle.h:570
std::vector< MSPersonStage * > MSPersonPlan
the structure holding the plan of a person
Definition: MSPerson.h:514
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's id.