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-2013 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") << " id=\"" << id << "\" ";
80  od << "depart=\"" << time2string(person->getDesiredDepart()) << "\"";
81  person->tripInfoOutput(od);
82  od.closeTag();
83  }
84  if (OptionsCont::getOptions().isSet("vehroute-output")) {
85  OutputDevice& od = OutputDevice::getDeviceByOption("vehroute-output");
86  od.openTag("person") << " id=\"" << id
87  << "\" depart=\"" << time2string(person->getDesiredDepart())
88  << "\" arrival=\"" << time2string(MSNet::getInstance()->getCurrentTimeStep())
89  << "\"";
90  person->routeOutput(od);
91  od.closeTag();
92  od << "\n";
93  }
94  const std::map<std::string, MSPerson*>::iterator i = myPersons.find(id);
95  if (i != myPersons.end()) {
96  delete i->second;
97  myPersons.erase(i);
98  }
99 }
100 
101 
102 void
104  const SUMOTime step = time % DELTA_T == 0 ? time : (time / DELTA_T + 1) * DELTA_T;
105  if (myWaiting4Departure.find(step) == myWaiting4Departure.end()) {
107  }
108  myWaiting4Departure[step].push_back(person);
109 }
110 
111 
112 void
114  const SUMOTime step = time % DELTA_T == 0 ? time : (time / DELTA_T + 1) * DELTA_T;
115  if (myWaitingUntil.find(step) == myWaitingUntil.end()) {
116  myWaitingUntil[step] = PersonVector();
117  }
118  myWaitingUntil[step].push_back(person);
119 }
120 
121 
122 void
124  while (myWaiting4Departure.find(time) != myWaiting4Departure.end()) {
125  const PersonVector& persons = myWaiting4Departure[time];
126  // we cannot use an iterator here because there might be additions to the vector while proceeding
127  for (size_t i = 0; i < persons.size(); ++i) {
128  if (!persons[i]->proceed(net, time)) {
129  erase(persons[i]);
130  }
131  }
132  myWaiting4Departure.erase(time);
133  }
134  while (myWaitingUntil.find(time) != myWaitingUntil.end()) {
135  const PersonVector& persons = myWaitingUntil[time];
136  // we cannot use an iterator here because there might be additions to the vector while proceeding
137  for (size_t i = 0; i < persons.size(); ++i) {
138  if (!persons[i]->proceed(net, time)) {
139  erase(persons[i]);
140  }
141  }
142  myWaitingUntil.erase(time);
143  }
144 }
145 
146 
147 void
148 MSPersonControl::addWaiting(const MSEdge* const edge, MSPerson* person) {
149  if (myWaiting4Vehicle.find(edge) == myWaiting4Vehicle.end()) {
150  myWaiting4Vehicle[edge] = std::vector<MSPerson*>();
151  }
152  myWaiting4Vehicle[edge].push_back(person);
153 }
154 
155 
156 bool
157 MSPersonControl::isWaiting4Vehicle(const MSEdge* const edge, MSPerson* /* p */) const {
158  return myWaiting4Vehicle.find(edge) != myWaiting4Vehicle.end();
159 }
160 
161 
162 bool
164  bool ret = false;
165  if (myWaiting4Vehicle.find(edge) != myWaiting4Vehicle.end()) {
166  PersonVector& waitPersons = myWaiting4Vehicle[edge];
167  for (PersonVector::iterator i = waitPersons.begin(); i != waitPersons.end();) {
168  const std::string& line = vehicle->getParameter().line == "" ? vehicle->getParameter().id : vehicle->getParameter().line;
169  if ((*i)->isWaitingFor(line)) {
170  edge->removePerson(*i);
171  vehicle->addPerson(*i);
172  static_cast<MSPerson::MSPersonStage_Driving*>((*i)->getCurrentStage())->setVehicle(vehicle);
173  i = waitPersons.erase(i);
174  ret = true;
175  } else {
176  ++i;
177  }
178  }
179  if (waitPersons.size() == 0) {
180  myWaiting4Vehicle.erase(myWaiting4Vehicle.find(edge));
181  }
182  }
183  return ret;
184 }
185 
186 
187 bool
189  return !myPersons.empty();
190 }
191 
192 
193 bool
195  return !myWaiting4Departure.empty() || !myWaitingUntil.empty() || !myWalking.empty();
196 }
197 
198 
199 void
201  myWalking[p->getID()] = p;
202 }
203 
204 
205 void
207  std::map<std::string, MSPerson*>::iterator i = myWalking.find(p->getID());
208  if (i != myWalking.end()) {
209  myWalking.erase(i);
210  }
211 }
212 
213 
214 void
216  for (std::map<const MSEdge*, PersonVector>::const_iterator i = myWaiting4Vehicle.begin(); i != myWaiting4Vehicle.end(); ++i) {
217  const MSEdge* edge = (*i).first;
218  const PersonVector& pv = (*i).second;
219  for (PersonVector::const_iterator j = pv.begin(); j != pv.end(); ++j) {
220  MSPerson* p = (*j);
221  edge->removePerson(p);
222  WRITE_WARNING("Person " + p->getID() + " aborted waiting for a ride that will never come.");
223  erase(p);
224  }
225  }
226 }
227 
228 
229 MSPerson*
231  return new MSPerson(pars, vtype, plan);
232 }
233 
234 /****************************************************************************/