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 // static members
52 // ===========================================================================
54 
55 
56 // ===========================================================================
57 // member method definitions
58 // ===========================================================================
60  myLoadedVehNo(0),
61  myRunningVehNo(0),
62  myEndedVehNo(0),
63  myDiscarded(0),
64  myCollisions(0),
65  myTeleports(0),
66  myTotalDepartureDelay(0),
67  myTotalTravelTime(0),
68  myDefaultVTypeMayBeDeleted(true),
69  myWaitingForPerson(0),
70  myScale(-1) {
71  SUMOVTypeParameter defType;
74  if (oc.isSet("incremental-dua-step")) {
75  myScale = oc.getInt("incremental-dua-step") / static_cast<SUMOReal>(oc.getInt("incremental-dua-base"));
76  }
77  if (oc.isSet("scale")) {
78  myScale = oc.getFloat("scale");
79  }
80 }
81 
82 
84  // delete vehicles
85  for (VehicleDictType::iterator i = myVehicleDict.begin(); i != myVehicleDict.end(); ++i) {
86  delete(*i).second;
87  }
88  myVehicleDict.clear();
89  // delete vehicle type distributions
90  for (VTypeDistDictType::iterator i = myVTypeDistDict.begin(); i != myVTypeDistDict.end(); ++i) {
91  delete(*i).second;
92  }
93  myVTypeDistDict.clear();
94  // delete vehicle types
95  for (VTypeDictType::iterator i = myVTypeDict.begin(); i != myVTypeDict.end(); ++i) {
96  delete(*i).second;
97  }
98  myVTypeDict.clear();
99 }
100 
101 
104  const MSRoute* route,
105  const MSVehicleType* type) {
106  myLoadedVehNo++;
107  MSVehicle* built = new MSVehicle(defs, route, type, type->computeChosenSpeedDeviation(myVehicleParamsRNG), myLoadedVehNo - 1);
109  return built;
110 }
111 
112 
113 void
115  assert(myRunningVehNo > 0);
116  for (std::vector<MSDevice*>::const_iterator i = veh->getDevices().begin(); i != veh->getDevices().end(); ++i) {
117  (*i)->generateOutput();
118  }
119  if (OptionsCont::getOptions().isSet("tripinfo-output")) {
120  OutputDevice::getDeviceByOption("tripinfo-output").closeTag(veh->getDevices().size() == 1);
121  }
122  myTotalTravelTime += STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep() - veh->getDeparture());
123  myRunningVehNo--;
125  deleteVehicle(veh);
126 }
127 
128 
129 void
131  if (getDepartedVehicleNo() == 0) {
132  od << -1.;
133  } else {
135  }
136 }
137 
138 
139 void
141  if (myEndedVehNo == 0) {
142  od << -1.;
143  } else {
145  }
146 }
147 
148 
149 void
151  ++myRunningVehNo;
154 }
155 
156 
157 void
158 MSVehicleControl::saveState(std::ostream& /*os*/) {
159 }
160 
161 
162 void
164 }
165 
166 
167 bool
168 MSVehicleControl::addVehicle(const std::string& id, SUMOVehicle* v) {
169  VehicleDictType::iterator it = myVehicleDict.find(id);
170  if (it == myVehicleDict.end()) {
171  // id not in myVehicleDict.
172  myVehicleDict[id] = v;
173  return true;
174  }
175  return false;
176 }
177 
178 
180 MSVehicleControl::getVehicle(const std::string& id) const {
181  VehicleDictType::const_iterator it = myVehicleDict.find(id);
182  if (it == myVehicleDict.end()) {
183  return 0;
184  }
185  return it->second;
186 }
187 
188 
189 void
191  myEndedVehNo++;
192  if (discard) {
193  myDiscarded++;
194  }
195  myVehicleDict.erase(veh->getID());
196  delete veh;
197 }
198 
199 
202  return myVehicleDict.begin();
203 }
204 
205 
208  return myVehicleDict.end();
209 }
210 
211 
212 bool
213 MSVehicleControl::checkVType(const std::string& id) {
214  if (id == DEFAULT_VTYPE_ID) {
216  delete myVTypeDict[id];
217  myVTypeDict.erase(myVTypeDict.find(id));
219  } else {
220  return false;
221  }
222  } else {
223  if (myVTypeDict.find(id) != myVTypeDict.end() || myVTypeDistDict.find(id) != myVTypeDistDict.end()) {
224  return false;
225  }
226  }
227  return true;
228 }
229 
230 bool
232  if (checkVType(vehType->getID())) {
233  myVTypeDict[vehType->getID()] = vehType;
234  return true;
235  }
236  return false;
237 }
238 
239 
240 bool
241 MSVehicleControl::addVTypeDistribution(const std::string& id, RandomDistributor<MSVehicleType*>* vehTypeDistribution) {
242  if (checkVType(id)) {
243  myVTypeDistDict[id] = vehTypeDistribution;
244  return true;
245  }
246  return false;
247 }
248 
249 
250 bool
251 MSVehicleControl::hasVTypeDistribution(const std::string& id) const {
252  return myVTypeDistDict.find(id) != myVTypeDistDict.end();
253 }
254 
255 
257 MSVehicleControl::getVType(const std::string& id) {
258  VTypeDictType::iterator it = myVTypeDict.find(id);
259  if (it == myVTypeDict.end()) {
260  VTypeDistDictType::iterator it2 = myVTypeDistDict.find(id);
261  if (it2 == myVTypeDistDict.end()) {
262  return 0;
263  }
264  return it2->second->get(&myVehicleParamsRNG);
265  }
266  if (id == DEFAULT_VTYPE_ID) {
268  }
269  return it->second;
270 }
271 
272 
273 void
274 MSVehicleControl::insertVTypeIDs(std::vector<std::string>& into) const {
275  into.reserve(into.size() + myVTypeDict.size() + myVTypeDistDict.size());
276  for (VTypeDictType::const_iterator i = myVTypeDict.begin(); i != myVTypeDict.end(); ++i) {
277  into.push_back((*i).first);
278  }
279  for (VTypeDistDictType::const_iterator i = myVTypeDistDict.begin(); i != myVTypeDistDict.end(); ++i) {
280  into.push_back((*i).first);
281  }
282 }
283 
284 
285 void
286 MSVehicleControl::addWaiting(const MSEdge* const edge, SUMOVehicle* vehicle) {
287  if (myWaiting.find(edge) == myWaiting.end()) {
288  myWaiting[edge] = std::vector<SUMOVehicle*>();
289  }
290  myWaiting[edge].push_back(vehicle);
291 }
292 
293 
294 void
295 MSVehicleControl::removeWaiting(const MSEdge* const edge, SUMOVehicle* vehicle) {
296  if (myWaiting.find(edge) != myWaiting.end()) {
297  std::vector<SUMOVehicle*>::iterator it = std::find(myWaiting[edge].begin(), myWaiting[edge].end(), vehicle);
298  if (it != myWaiting[edge].end()) {
299  myWaiting[edge].erase(it);
300  }
301  }
302 }
303 
304 
306 MSVehicleControl::getWaitingVehicle(const MSEdge* const edge, const std::set<std::string>& lines) {
307  if (myWaiting.find(edge) != myWaiting.end()) {
308  for (std::vector<SUMOVehicle*>::const_iterator it = myWaiting[edge].begin(); it != myWaiting[edge].end(); ++it) {
309  const std::string& line = (*it)->getParameter().line == "" ? (*it)->getParameter().id : (*it)->getParameter().line;
310  if (lines.count(line)) {
311  return (*it);
312  }
313  }
314  }
315  return 0;
316 }
317 
318 
319 void
321  for (VehicleDictType::iterator i = myVehicleDict.begin(); i != myVehicleDict.end(); ++i) {
322  WRITE_WARNING("Vehicle " + i->first + " aborted waiting for a person that will never come.");
323  }
324 }
325 
326 
327 bool
329  frac = frac < 0 ? myScale : frac;
330  if (frac < 0) {
331  return true;
332  }
333  const unsigned int resolution = 1000;
334  const unsigned int intFrac = (unsigned int)floor(frac * resolution + 0.5);
335  // the vehicle in question has already been loaded, hence the '-1'
336  // apply % twice to avoid integer overflow
337  return (((myLoadedVehNo - 1) % resolution) * intFrac) % resolution < intFrac;
338 }
339 
340 /****************************************************************************/
341