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-sim.org/
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));
109  return built;
110 }
111 
112 
113 void
115  assert(myRunningVehNo > 0);
116  myTotalTravelTime += STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep() - veh->getDeparture());
117  myRunningVehNo--;
119  for (std::vector<MSDevice*>::const_iterator i = veh->getDevices().begin(); i != veh->getDevices().end(); ++i) {
120  (*i)->generateOutput();
121  }
122  if (OptionsCont::getOptions().isSet("tripinfo-output")) {
123  OutputDevice::getDeviceByOption("tripinfo-output").closeTag();
124  }
125  deleteVehicle(veh);
126 }
127 
128 
129 void
131  ++myRunningVehNo;
134 }
135 
136 
137 void
138 MSVehicleControl::setState(int runningVehNo, int endedVehNo, SUMOReal totalDepartureDelay, SUMOReal totalTravelTime) {
139  myRunningVehNo = runningVehNo;
140  myEndedVehNo = endedVehNo;
141  myTotalDepartureDelay = totalDepartureDelay;
142  myTotalTravelTime = totalTravelTime;
143 }
144 
145 
146 void
150  // save vehicle types
151  for (VTypeDictType::iterator it = myVTypeDict.begin(); it != myVTypeDict.end(); ++it) {
152  it->second->getParameter().write(out);
153  }
154  for (VTypeDistDictType::iterator it = myVTypeDistDict.begin(); it != myVTypeDistDict.end(); ++it) {
156  out.writeAttr(SUMO_ATTR_VTYPES, (*it).second->getVals());
157  out.writeAttr(SUMO_ATTR_PROBS, (*it).second->getProbs());
158  out.closeTag();
159  }
160  for (VehicleDictType::iterator it = myVehicleDict.begin(); it != myVehicleDict.end(); ++it) {
161  (*it).second->saveState(out);
162  }
163 }
164 
165 
166 bool
167 MSVehicleControl::addVehicle(const std::string& id, SUMOVehicle* v) {
168  VehicleDictType::iterator it = myVehicleDict.find(id);
169  if (it == myVehicleDict.end()) {
170  // id not in myVehicleDict.
171  myVehicleDict[id] = v;
172  return true;
173  }
174  return false;
175 }
176 
177 
179 MSVehicleControl::getVehicle(const std::string& id) const {
180  VehicleDictType::const_iterator it = myVehicleDict.find(id);
181  if (it == myVehicleDict.end()) {
182  return 0;
183  }
184  return it->second;
185 }
186 
187 
188 void
190  myEndedVehNo++;
191  if (discard) {
192  myDiscarded++;
193  }
194  myVehicleDict.erase(veh->getID());
195  delete veh;
196 }
197 
198 
201  return myVehicleDict.begin();
202 }
203 
204 
207  return myVehicleDict.end();
208 }
209 
210 
211 bool
212 MSVehicleControl::checkVType(const std::string& id) {
213  if (id == DEFAULT_VTYPE_ID) {
215  delete myVTypeDict[id];
216  myVTypeDict.erase(myVTypeDict.find(id));
218  } else {
219  return false;
220  }
221  } else {
222  if (myVTypeDict.find(id) != myVTypeDict.end() || myVTypeDistDict.find(id) != myVTypeDistDict.end()) {
223  return false;
224  }
225  }
226  return true;
227 }
228 
229 bool
231  if (checkVType(vehType->getID())) {
232  myVTypeDict[vehType->getID()] = vehType;
233  return true;
234  }
235  return false;
236 }
237 
238 
239 bool
240 MSVehicleControl::addVTypeDistribution(const std::string& id, RandomDistributor<MSVehicleType*>* vehTypeDistribution) {
241  if (checkVType(id)) {
242  myVTypeDistDict[id] = vehTypeDistribution;
243  return true;
244  }
245  return false;
246 }
247 
248 
249 bool
250 MSVehicleControl::hasVTypeDistribution(const std::string& id) const {
251  return myVTypeDistDict.find(id) != myVTypeDistDict.end();
252 }
253 
254 
256 MSVehicleControl::getVType(const std::string& id) {
257  VTypeDictType::iterator it = myVTypeDict.find(id);
258  if (it == myVTypeDict.end()) {
259  VTypeDistDictType::iterator it2 = myVTypeDistDict.find(id);
260  if (it2 == myVTypeDistDict.end()) {
261  return 0;
262  }
263  return it2->second->get(&myVehicleParamsRNG);
264  }
265  if (id == DEFAULT_VTYPE_ID) {
267  }
268  return it->second;
269 }
270 
271 
272 void
273 MSVehicleControl::insertVTypeIDs(std::vector<std::string>& into) const {
274  into.reserve(into.size() + myVTypeDict.size() + myVTypeDistDict.size());
275  for (VTypeDictType::const_iterator i = myVTypeDict.begin(); i != myVTypeDict.end(); ++i) {
276  into.push_back((*i).first);
277  }
278  for (VTypeDistDictType::const_iterator i = myVTypeDistDict.begin(); i != myVTypeDistDict.end(); ++i) {
279  into.push_back((*i).first);
280  }
281 }
282 
283 
284 void
285 MSVehicleControl::addWaiting(const MSEdge* const edge, SUMOVehicle* vehicle) {
286  if (myWaiting.find(edge) == myWaiting.end()) {
287  myWaiting[edge] = std::vector<SUMOVehicle*>();
288  }
289  myWaiting[edge].push_back(vehicle);
290 }
291 
292 
293 void
294 MSVehicleControl::removeWaiting(const MSEdge* const edge, SUMOVehicle* vehicle) {
295  if (myWaiting.find(edge) != myWaiting.end()) {
296  std::vector<SUMOVehicle*>::iterator it = std::find(myWaiting[edge].begin(), myWaiting[edge].end(), vehicle);
297  if (it != myWaiting[edge].end()) {
298  myWaiting[edge].erase(it);
299  }
300  }
301 }
302 
303 
305 MSVehicleControl::getWaitingVehicle(const MSEdge* const edge, const std::set<std::string>& lines) {
306  if (myWaiting.find(edge) != myWaiting.end()) {
307  for (std::vector<SUMOVehicle*>::const_iterator it = myWaiting[edge].begin(); it != myWaiting[edge].end(); ++it) {
308  const std::string& line = (*it)->getParameter().line == "" ? (*it)->getParameter().id : (*it)->getParameter().line;
309  if (lines.count(line)) {
310  return (*it);
311  }
312  }
313  }
314  return 0;
315 }
316 
317 
318 void
320  for (VehicleDictType::iterator i = myVehicleDict.begin(); i != myVehicleDict.end(); ++i) {
321  WRITE_WARNING("Vehicle " + i->first + " aborted waiting for a person that will never come.");
322  }
323 }
324 
325 
326 bool
328  frac = frac < 0 ? myScale : frac;
329  if (frac < 0) {
330  return true;
331  }
332  const unsigned int resolution = 1000;
333  const unsigned int intFrac = (unsigned int)floor(frac * resolution + 0.5);
334  // the vehicle in question has already been loaded, hence the '-1'
335  // apply % twice to avoid integer overflow
336  return (((myLoadedVehNo - 1) % resolution) * intFrac) % resolution < intFrac;
337 }
338 
339 /****************************************************************************/
340