SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 /****************************************************************************/