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-sim.org/
13 // Copyright (C) 2001-2014 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"
38 #include "MSPerson.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 void
76  const std::string& id = person->getID();
77  if (OptionsCont::getOptions().isSet("tripinfo-output")) {
78  OutputDevice& od = OutputDevice::getDeviceByOption("tripinfo-output");
79  od.openTag("personinfo").writeAttr("id", id).writeAttr("depart", time2string(person->getDesiredDepart()));
80  person->tripInfoOutput(od);
81  od.closeTag();
82  }
83  if (OptionsCont::getOptions().isSet("vehroute-output")) {
84  OutputDevice& od = OutputDevice::getDeviceByOption("vehroute-output");
85  od.openTag("person").writeAttr("id", id).writeAttr("depart", time2string(person->getDesiredDepart())).writeAttr("arrival", time2string(MSNet::getInstance()->getCurrentTimeStep()));
86  person->routeOutput(od);
87  od.closeTag();
88  od << "\n";
89  }
90  const std::map<std::string, MSPerson*>::iterator i = myPersons.find(id);
91  if (i != myPersons.end()) {
92  delete i->second;
93  myPersons.erase(i);
94  }
95 }
96 
97 
98 void
100  const SUMOTime step = time % DELTA_T == 0 ? time : (time / DELTA_T + 1) * DELTA_T;
101  if (myWaiting4Departure.find(step) == myWaiting4Departure.end()) {
103  }
104  myWaiting4Departure[step].push_back(person);
105 }
106 
107 
108 void
110  const SUMOTime step = time % DELTA_T == 0 ? time : (time / DELTA_T + 1) * DELTA_T;
111  if (myWaitingUntil.find(step) == myWaitingUntil.end()) {
112  myWaitingUntil[step] = PersonVector();
113  }
114  myWaitingUntil[step].push_back(person);
115 }
116 
117 
118 void
120  while (myWaiting4Departure.find(time) != myWaiting4Departure.end()) {
121  const PersonVector& persons = myWaiting4Departure[time];
122  // we cannot use an iterator here because there might be additions to the vector while proceeding
123  for (size_t i = 0; i < persons.size(); ++i) {
124  if (!persons[i]->proceed(net, time)) {
125  erase(persons[i]);
126  }
127  }
128  myWaiting4Departure.erase(time);
129  }
130  while (myWaitingUntil.find(time) != myWaitingUntil.end()) {
131  const PersonVector& persons = myWaitingUntil[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  myWaitingUntil.erase(time);
139  }
140 }
141 
142 
143 void
144 MSPersonControl::addWaiting(const MSEdge* const edge, MSPerson* person) {
145  if (myWaiting4Vehicle.find(edge) == myWaiting4Vehicle.end()) {
146  myWaiting4Vehicle[edge] = std::vector<MSPerson*>();
147  }
148  myWaiting4Vehicle[edge].push_back(person);
149 }
150 
151 
152 bool
153 MSPersonControl::isWaiting4Vehicle(const MSEdge* const edge, MSPerson* /* p */) const {
154  return myWaiting4Vehicle.find(edge) != myWaiting4Vehicle.end();
155 }
156 
157 
158 bool
160  bool ret = false;
161  if (myWaiting4Vehicle.find(edge) != myWaiting4Vehicle.end()) {
162  PersonVector& waitPersons = myWaiting4Vehicle[edge];
163  for (PersonVector::iterator i = waitPersons.begin(); i != waitPersons.end();) {
164  const std::string& line = vehicle->getParameter().line == "" ? vehicle->getParameter().id : vehicle->getParameter().line;
165  if ((*i)->isWaitingFor(line)) {
166  edge->removePerson(*i);
167  vehicle->addPerson(*i);
168  static_cast<MSPerson::MSPersonStage_Driving*>((*i)->getCurrentStage())->setVehicle(vehicle);
169  i = waitPersons.erase(i);
170  ret = true;
171  } else {
172  ++i;
173  }
174  }
175  if (waitPersons.size() == 0) {
176  myWaiting4Vehicle.erase(myWaiting4Vehicle.find(edge));
177  }
178  }
179  return ret;
180 }
181 
182 
183 bool
185  return !myPersons.empty();
186 }
187 
188 
189 bool
191  return !myWaiting4Departure.empty() || !myWaitingUntil.empty() || !myWalking.empty();
192 }
193 
194 
195 void
197  myWalking[p->getID()] = p;
198 }
199 
200 
201 void
203  std::map<std::string, MSPerson*>::iterator i = myWalking.find(p->getID());
204  if (i != myWalking.end()) {
205  myWalking.erase(i);
206  }
207 }
208 
209 
210 void
212  for (std::map<const MSEdge*, PersonVector>::const_iterator i = myWaiting4Vehicle.begin(); i != myWaiting4Vehicle.end(); ++i) {
213  const MSEdge* edge = (*i).first;
214  const PersonVector& pv = (*i).second;
215  for (PersonVector::const_iterator j = pv.begin(); j != pv.end(); ++j) {
216  MSPerson* p = (*j);
217  edge->removePerson(p);
218  WRITE_WARNING("Person " + p->getID() + " aborted waiting for a ride that will never come.");
219  erase(p);
220  }
221  }
222 }
223 
224 
225 MSPerson*
227  return new MSPerson(pars, vtype, plan);
228 }
229 
230 /****************************************************************************/
const std::string & getID() const
returns the person id
Definition: MSPerson.cpp:551
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:77
SUMOTime getDesiredDepart() const
Returns the desired departure time.
Definition: MSPerson.cpp:584
bool boardAnyWaiting(MSEdge *edge, MSVehicle *vehicle)
board any applicable persons Boards any people who wait on that edge for the given vehicle and remove...
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
MSPersonControl()
constructor
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
The simulated network and simulation perfomer.
Definition: MSNet.h:91
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:74
void routeOutput(OutputDevice &os) const
Called on writing vehroute output.
Definition: MSPerson.cpp:604
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
virtual void removePerson(MSPerson *p) const
Definition: MSEdge.h:487
void setWaitEnd(SUMOTime time, MSPerson *person)
sets the arrival time for a waiting or walking person
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:596
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
void setDeparture(SUMOTime time, MSPerson *person)
sets the arrival time for a waiting or walking person
const SUMOVehicleParameter & getParameter() const
Returns the vehicle's parameter (including departure definition)
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
virtual void erase(MSPerson *person)
removes a single person
void addPerson(MSPerson *person)
Adds a passenger.
Definition: MSVehicle.cpp:2189
std::vector< MSPersonStage * > MSPersonPlan
the structure holding the plan of a person
Definition: MSPerson.h:515
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
virtual ~MSPersonControl()
destructor
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.