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-2013 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();
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::setState(int runningVehNo, int endedVehNo, SUMOReal totalDepartureDelay, SUMOReal totalTravelTime) {
159  myRunningVehNo = runningVehNo;
160  myEndedVehNo = endedVehNo;
161  myTotalDepartureDelay = totalDepartureDelay;
162  myTotalTravelTime = totalTravelTime;
163 }
164 
165 
166 void
168 }
169 
170 
171 bool
172 MSVehicleControl::addVehicle(const std::string& id, SUMOVehicle* v) {
173  VehicleDictType::iterator it = myVehicleDict.find(id);
174  if (it == myVehicleDict.end()) {
175  // id not in myVehicleDict.
176  myVehicleDict[id] = v;
177  return true;
178  }
179  return false;
180 }
181 
182 
184 MSVehicleControl::getVehicle(const std::string& id) const {
185  VehicleDictType::const_iterator it = myVehicleDict.find(id);
186  if (it == myVehicleDict.end()) {
187  return 0;
188  }
189  return it->second;
190 }
191 
192 
193 void
195  myEndedVehNo++;
196  if (discard) {
197  myDiscarded++;
198  }
199  myVehicleDict.erase(veh->getID());
200  delete veh;
201 }
202 
203 
206  return myVehicleDict.begin();
207 }
208 
209 
212  return myVehicleDict.end();
213 }
214 
215 
216 bool
217 MSVehicleControl::checkVType(const std::string& id) {
218  if (id == DEFAULT_VTYPE_ID) {
220  delete myVTypeDict[id];
221  myVTypeDict.erase(myVTypeDict.find(id));
223  } else {
224  return false;
225  }
226  } else {
227  if (myVTypeDict.find(id) != myVTypeDict.end() || myVTypeDistDict.find(id) != myVTypeDistDict.end()) {
228  return false;
229  }
230  }
231  return true;
232 }
233 
234 bool
236  if (checkVType(vehType->getID())) {
237  myVTypeDict[vehType->getID()] = vehType;
238  return true;
239  }
240  return false;
241 }
242 
243 
244 bool
245 MSVehicleControl::addVTypeDistribution(const std::string& id, RandomDistributor<MSVehicleType*>* vehTypeDistribution) {
246  if (checkVType(id)) {
247  myVTypeDistDict[id] = vehTypeDistribution;
248  return true;
249  }
250  return false;
251 }
252 
253 
254 bool
255 MSVehicleControl::hasVTypeDistribution(const std::string& id) const {
256  return myVTypeDistDict.find(id) != myVTypeDistDict.end();
257 }
258 
259 
261 MSVehicleControl::getVType(const std::string& id) {
262  VTypeDictType::iterator it = myVTypeDict.find(id);
263  if (it == myVTypeDict.end()) {
264  VTypeDistDictType::iterator it2 = myVTypeDistDict.find(id);
265  if (it2 == myVTypeDistDict.end()) {
266  return 0;
267  }
268  return it2->second->get(&myVehicleParamsRNG);
269  }
270  if (id == DEFAULT_VTYPE_ID) {
272  }
273  return it->second;
274 }
275 
276 
277 void
278 MSVehicleControl::insertVTypeIDs(std::vector<std::string>& into) const {
279  into.reserve(into.size() + myVTypeDict.size() + myVTypeDistDict.size());
280  for (VTypeDictType::const_iterator i = myVTypeDict.begin(); i != myVTypeDict.end(); ++i) {
281  into.push_back((*i).first);
282  }
283  for (VTypeDistDictType::const_iterator i = myVTypeDistDict.begin(); i != myVTypeDistDict.end(); ++i) {
284  into.push_back((*i).first);
285  }
286 }
287 
288 
289 void
290 MSVehicleControl::addWaiting(const MSEdge* const edge, SUMOVehicle* vehicle) {
291  if (myWaiting.find(edge) == myWaiting.end()) {
292  myWaiting[edge] = std::vector<SUMOVehicle*>();
293  }
294  myWaiting[edge].push_back(vehicle);
295 }
296 
297 
298 void
299 MSVehicleControl::removeWaiting(const MSEdge* const edge, SUMOVehicle* vehicle) {
300  if (myWaiting.find(edge) != myWaiting.end()) {
301  std::vector<SUMOVehicle*>::iterator it = std::find(myWaiting[edge].begin(), myWaiting[edge].end(), vehicle);
302  if (it != myWaiting[edge].end()) {
303  myWaiting[edge].erase(it);
304  }
305  }
306 }
307 
308 
310 MSVehicleControl::getWaitingVehicle(const MSEdge* const edge, const std::set<std::string>& lines) {
311  if (myWaiting.find(edge) != myWaiting.end()) {
312  for (std::vector<SUMOVehicle*>::const_iterator it = myWaiting[edge].begin(); it != myWaiting[edge].end(); ++it) {
313  const std::string& line = (*it)->getParameter().line == "" ? (*it)->getParameter().id : (*it)->getParameter().line;
314  if (lines.count(line)) {
315  return (*it);
316  }
317  }
318  }
319  return 0;
320 }
321 
322 
323 void
325  for (VehicleDictType::iterator i = myVehicleDict.begin(); i != myVehicleDict.end(); ++i) {
326  WRITE_WARNING("Vehicle " + i->first + " aborted waiting for a person that will never come.");
327  }
328 }
329 
330 
331 bool
333  frac = frac < 0 ? myScale : frac;
334  if (frac < 0) {
335  return true;
336  }
337  const unsigned int resolution = 1000;
338  const unsigned int intFrac = (unsigned int)floor(frac * resolution + 0.5);
339  // the vehicle in question has already been loaded, hence the '-1'
340  // apply % twice to avoid integer overflow
341  return (((myLoadedVehNo - 1) % resolution) * intFrac) % resolution < intFrac;
342 }
343 
344 /****************************************************************************/
345