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 "MSPerson.h"
38 #include "MSVehicle.h"
39 #include "MSPersonControl.h"
42 
43 #ifdef CHECK_MEMORY_LEAKS
44 #include <foreign/nvwa/debug_new.h>
45 #endif // CHECK_MEMORY_LEAKS
46 
47 
48 // ===========================================================================
49 // method definitions
50 // ===========================================================================
52 
53 
55  for (std::map<std::string, MSPerson*>::iterator i = myPersons.begin(); i != myPersons.end(); ++i) {
56  delete(*i).second;
57  }
58  myPersons.clear();
59  myWaiting.clear();
60 }
61 
62 
63 bool
64 MSPersonControl::add(const std::string& id, MSPerson* person) {
65  if (myPersons.find(id) == myPersons.end()) {
66  myPersons[id] = person;
67  return true;
68  }
69  return false;
70 }
71 
72 
73 void
75  const std::string& id = person->getID();
76  if (OptionsCont::getOptions().isSet("tripinfo-output")) {
77  OutputDevice& od = OutputDevice::getDeviceByOption("tripinfo-output");
78  od.openTag("personinfo") << " id=\"" << id << "\" ";
79  od << "depart=\"" << time2string(person->getDesiredDepart()) << "\">\n";
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") << " id=\"" << id
86  << "\" depart=\"" << time2string(person->getDesiredDepart())
87  << "\" arrival=\"" << time2string(MSNet::getInstance()->getCurrentTimeStep())
88  << "\">\n";
89  od.closeTag();
90  od << "\n";
91  }
92  const std::map<std::string, MSPerson*>::iterator i = myPersons.find(id);
93  if (i != myPersons.end()) {
94  delete i->second;
95  myPersons.erase(i);
96  }
97 }
98 
99 void
101  const SUMOTime step = time % DELTA_T == 0 ? time : (time / DELTA_T + 1) * DELTA_T;
102  if (myArrivals.find(step) == myArrivals.end()) {
103  myArrivals[step] = PersonVector();
104  }
105  myArrivals[step].push_back(person);
106 }
107 
108 
109 void
111  while (myArrivals.find(time) != myArrivals.end()) {
112  const PersonVector& persons = myArrivals[time];
113  // we cannot use an iterator here because there might be additions to the vector while proceeding
114  for (size_t i = 0; i < persons.size(); ++i) {
115  persons[i]->proceed(net, time);
116  }
117  myArrivals.erase(time);
118  }
119 }
120 
121 
122 void
123 MSPersonControl::addWaiting(const MSEdge* const edge, MSPerson* person) {
124  if (myWaiting.find(edge) == myWaiting.end()) {
125  myWaiting[edge] = std::vector<MSPerson*>();
126  }
127  myWaiting[edge].push_back(person);
128 }
129 
130 
131 bool
132 MSPersonControl::boardAnyWaiting(const MSEdge* const edge, MSVehicle* vehicle) {
133  bool ret = false;
134  if (myWaiting.find(edge) != myWaiting.end()) {
135  PersonVector& waitPersons = myWaiting[edge];
136  for (PersonVector::iterator i = waitPersons.begin(); i != waitPersons.end();) {
137  const std::string& line = vehicle->getParameter().line == "" ? vehicle->getParameter().id : vehicle->getParameter().line;
138  if ((*i)->isWaitingFor(line)) {
139  vehicle->addPerson(*i);
140  i = waitPersons.erase(i);
141  ret = true;
142  } else {
143  ++i;
144  }
145  }
146  }
147  return ret;
148 }
149 
150 
151 bool
153  return !myPersons.empty();
154 }
155 
156 
157 bool
159  return !myArrivals.empty();
160 }
161 
162 
163 void
165  while (!myPersons.empty()) {
166  std::map<std::string, MSPerson*>::iterator i = myPersons.begin();
167  WRITE_WARNING("Person " + i->first + " aborted waiting for a ride that will never come.");
168  erase(i->second);
169  }
170 }
171 
172 /****************************************************************************/