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