SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MSVehicleControl.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // The class responsible for building and deletion of vehicles
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
12 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include "MSVehicleControl.h"
34 #include "MSVehicle.h"
35 #include "MSLane.h"
36 #include "MSNet.h"
39 #include <utils/common/RGBColor.h>
44 
45 #ifdef CHECK_MEMORY_LEAKS
46 #include <foreign/nvwa/debug_new.h>
47 #endif // CHECK_MEMORY_LEAKS
48 
49 
50 // ===========================================================================
51 // member method definitions
52 // ===========================================================================
54  myLoadedVehNo(0),
55  myRunningVehNo(0),
56  myEndedVehNo(0),
57  myDiscarded(0),
58  myTotalDepartureDelay(0),
59  myTotalTravelTime(0),
60  myDefaultVTypeMayBeDeleted(true),
61  myWaitingForPerson(0) {
62  SUMOVTypeParameter defType;
64 }
65 
66 
68  // delete vehicles
69  for (VehicleDictType::iterator i = myVehicleDict.begin(); i != myVehicleDict.end(); ++i) {
70  delete(*i).second;
71  }
72  myVehicleDict.clear();
73  // delete vehicle type distributions
74  for (VTypeDistDictType::iterator i = myVTypeDistDict.begin(); i != myVTypeDistDict.end(); ++i) {
75  delete(*i).second;
76  }
77  myVTypeDistDict.clear();
78  // delete vehicle types
79  for (VTypeDictType::iterator i = myVTypeDict.begin(); i != myVTypeDict.end(); ++i) {
80  delete(*i).second;
81  }
82  myVTypeDict.clear();
83 }
84 
85 
88  const MSRoute* route,
89  const MSVehicleType* type) {
90  myLoadedVehNo++;
91  MSVehicle* built = new MSVehicle(defs, route, type, myLoadedVehNo - 1);
93  return built;
94 }
95 
96 
97 void
99  assert(myRunningVehNo > 0);
100  for (std::vector<MSDevice*>::const_iterator i = veh->getDevices().begin(); i != veh->getDevices().end(); ++i) {
101  (*i)->generateOutput();
102  }
103  if (OptionsCont::getOptions().isSet("tripinfo-output")) {
104  OutputDevice::getDeviceByOption("tripinfo-output").closeTag(veh->getDevices().size() == 1);
105  }
106  myTotalTravelTime += STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep() - veh->getDeparture());
107  myRunningVehNo--;
109  deleteVehicle(veh);
110 }
111 
112 
113 void
115  if (getDepartedVehicleNo() == 0) {
116  od << -1.;
117  } else {
119  }
120 }
121 
122 
123 void
125  if (myEndedVehNo == 0) {
126  od << -1.;
127  } else {
129  }
130 }
131 
132 
133 void
135  ++myRunningVehNo;
138 }
139 
140 
141 void
142 MSVehicleControl::saveState(std::ostream& /*os*/) {
143 }
144 
145 
146 void
148 }
149 
150 
151 bool
152 MSVehicleControl::addVehicle(const std::string& id, SUMOVehicle* v) {
153  VehicleDictType::iterator it = myVehicleDict.find(id);
154  if (it == myVehicleDict.end()) {
155  // id not in myVehicleDict.
156  myVehicleDict[id] = v;
157  return true;
158  }
159  return false;
160 }
161 
162 
164 MSVehicleControl::getVehicle(const std::string& id) const {
165  VehicleDictType::const_iterator it = myVehicleDict.find(id);
166  if (it == myVehicleDict.end()) {
167  return 0;
168  }
169  return it->second;
170 }
171 
172 
173 void
175  myEndedVehNo++;
176  if (discard) {
177  myDiscarded++;
178  }
179  myVehicleDict.erase(veh->getID());
180  delete veh;
181 }
182 
183 
186  return myVehicleDict.begin();
187 }
188 
189 
192  return myVehicleDict.end();
193 }
194 
195 
196 bool
197 MSVehicleControl::checkVType(const std::string& id) {
198  if (id == DEFAULT_VTYPE_ID) {
200  delete myVTypeDict[id];
201  myVTypeDict.erase(myVTypeDict.find(id));
203  } else {
204  return false;
205  }
206  } else {
207  if (myVTypeDict.find(id) != myVTypeDict.end() || myVTypeDistDict.find(id) != myVTypeDistDict.end()) {
208  return false;
209  }
210  }
211  return true;
212 }
213 
214 bool
216  if (checkVType(vehType->getID())) {
217  myVTypeDict[vehType->getID()] = vehType;
218  return true;
219  }
220  return false;
221 }
222 
223 
224 bool
225 MSVehicleControl::addVTypeDistribution(const std::string& id, RandomDistributor<MSVehicleType*> *vehTypeDistribution) {
226  if (checkVType(id)) {
227  myVTypeDistDict[id] = vehTypeDistribution;
228  return true;
229  }
230  return false;
231 }
232 
233 
234 bool
235 MSVehicleControl::hasVTypeDistribution(const std::string& id) const {
236  return myVTypeDistDict.find(id) != myVTypeDistDict.end();
237 }
238 
239 
241 MSVehicleControl::getVType(const std::string& id) {
242  VTypeDictType::iterator it = myVTypeDict.find(id);
243  if (it == myVTypeDict.end()) {
244  VTypeDistDictType::iterator it2 = myVTypeDistDict.find(id);
245  if (it2 == myVTypeDistDict.end()) {
246  return 0;
247  }
248  return it2->second->get();
249  }
250  if (id == DEFAULT_VTYPE_ID) {
252  }
253  return it->second;
254 }
255 
256 
257 void
258 MSVehicleControl::insertVTypeIDs(std::vector<std::string> &into) const {
259  into.reserve(into.size() + myVTypeDict.size() + myVTypeDistDict.size());
260  for (VTypeDictType::const_iterator i = myVTypeDict.begin(); i != myVTypeDict.end(); ++i) {
261  into.push_back((*i).first);
262  }
263  for (VTypeDistDictType::const_iterator i = myVTypeDistDict.begin(); i != myVTypeDistDict.end(); ++i) {
264  into.push_back((*i).first);
265  }
266 }
267 
268 
269 void
270 MSVehicleControl::addWaiting(const MSEdge* const edge, SUMOVehicle* vehicle) {
271  if (myWaiting.find(edge) == myWaiting.end()) {
272  myWaiting[edge] = std::vector<SUMOVehicle*>();
273  }
274  myWaiting[edge].push_back(vehicle);
275 }
276 
277 
278 void
279 MSVehicleControl::removeWaiting(const MSEdge* const edge, SUMOVehicle* vehicle) {
280  if (myWaiting.find(edge) != myWaiting.end()) {
281  std::vector<SUMOVehicle*>::iterator it = std::find(myWaiting[edge].begin(), myWaiting[edge].end(), vehicle);
282  if (it != myWaiting[edge].end()) {
283  myWaiting[edge].erase(it);
284  }
285  }
286 }
287 
288 
290 MSVehicleControl::getWaitingVehicle(const MSEdge* const edge, const std::set<std::string> &lines) {
291  if (myWaiting.find(edge) != myWaiting.end()) {
292  for (std::vector<SUMOVehicle*>::const_iterator it = myWaiting[edge].begin(); it != myWaiting[edge].end(); ++it) {
293  const std::string& line = (*it)->getParameter().line == "" ? (*it)->getParameter().id : (*it)->getParameter().line;
294  if (lines.count(line)) {
295  return (*it);
296  }
297  }
298  }
299  return 0;
300 }
301 
302 
303 void
305  for (VehicleDictType::iterator i = myVehicleDict.begin(); i != myVehicleDict.end(); ++i) {
306  WRITE_WARNING("Vehicle " + i->first + " aborted waiting for a person that will never come.");
307  }
308 }
309 
310 
311 bool
313  const unsigned int resolution = 1000;
314  const unsigned int intFrac = (unsigned int)floor(frac * resolution + 0.5);
315  // the vehicle in question has already been loaded, hence the '-1'
316  // apply % twice to avoid integer overflow
317  return (((myLoadedVehNo - 1) % resolution) * intFrac) % resolution < intFrac;
318 }
319 
320 /****************************************************************************/
321