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.dlr.de/
12 // Copyright (C) 2001-2014 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"
37 #include "MSRouteHandler.h"
40 #include <utils/common/RGBColor.h>
45 
46 #ifdef CHECK_MEMORY_LEAKS
47 #include <foreign/nvwa/debug_new.h>
48 #endif // CHECK_MEMORY_LEAKS
49 
50 
51 // ===========================================================================
52 // member method definitions
53 // ===========================================================================
55  myLoadedVehNo(0),
56  myRunningVehNo(0),
57  myEndedVehNo(0),
58  myDiscarded(0),
59  myCollisions(0),
60  myTeleportsJam(0),
61  myTeleportsYield(0),
62  myTeleportsWrongLane(0),
63  myEmergencyStops(0),
64  myTotalDepartureDelay(0),
65  myTotalTravelTime(0),
66  myDefaultVTypeMayBeDeleted(true),
67  myWaitingForPerson(0),
68  myScale(-1) {
72  if (oc.isSet("scale")) {
73  myScale = oc.getFloat("scale");
74  }
75  myMaxRandomDepartOffset = string2time(oc.getString("random-depart-offset"));
76 }
77 
78 
80  // delete vehicles
81  for (VehicleDictType::iterator i = myVehicleDict.begin(); i != myVehicleDict.end(); ++i) {
82  delete(*i).second;
83  }
84  myVehicleDict.clear();
85  // delete vehicle type distributions
86  for (VTypeDistDictType::iterator i = myVTypeDistDict.begin(); i != myVTypeDistDict.end(); ++i) {
87  delete(*i).second;
88  }
89  myVTypeDistDict.clear();
90  // delete vehicle types
91  for (VTypeDictType::iterator i = myVTypeDict.begin(); i != myVTypeDict.end(); ++i) {
92  delete(*i).second;
93  }
94  myVTypeDict.clear();
95 }
96 
99  if (myMaxRandomDepartOffset > 0) {
100  // round to the closest usable simulation step
101  return DELTA_T * int((MSRouteHandler::getParsingRNG()->rand((int)myMaxRandomDepartOffset) + 0.5 * DELTA_T) / DELTA_T);
102  } else {
103  return 0;
104  }
105 }
106 
109  const MSRoute* route,
110  const MSVehicleType* type,
111  const bool ignoreStopErrors, const bool fromRouteFile) {
112  myLoadedVehNo++;
113  if (fromRouteFile) {
115  }
116  MSVehicle* built = new MSVehicle(defs, route, type, type->computeChosenSpeedDeviation(fromRouteFile ? MSRouteHandler::getParsingRNG() : 0));
117  built->addStops(ignoreStopErrors);
119  return built;
120 }
121 
122 
123 void
125  assert(myRunningVehNo > 0);
126  myTotalTravelTime += STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep() - veh->getDeparture());
127  myRunningVehNo--;
129  for (std::vector<MSDevice*>::const_iterator i = veh->getDevices().begin(); i != veh->getDevices().end(); ++i) {
130  (*i)->generateOutput();
131  }
132  if (OptionsCont::getOptions().isSet("tripinfo-output")) {
133  // close tag after tripinfo (possibly including emissions from another device) have been written
134  OutputDevice::getDeviceByOption("tripinfo-output").closeTag();
135  }
136  deleteVehicle(veh);
137 }
138 
139 
140 void
142  ++myRunningVehNo;
145 }
146 
147 
148 void
149 MSVehicleControl::setState(int runningVehNo, int endedVehNo, SUMOReal totalDepartureDelay, SUMOReal totalTravelTime) {
150  myRunningVehNo = runningVehNo;
151  myEndedVehNo = endedVehNo;
152  myTotalDepartureDelay = totalDepartureDelay;
153  myTotalTravelTime = totalTravelTime;
154 }
155 
156 
157 void
161  // save vehicle types
162  for (VTypeDictType::iterator it = myVTypeDict.begin(); it != myVTypeDict.end(); ++it) {
163  it->second->getParameter().write(out);
164  }
165  for (VTypeDistDictType::iterator it = myVTypeDistDict.begin(); it != myVTypeDistDict.end(); ++it) {
167  out.writeAttr(SUMO_ATTR_VTYPES, (*it).second->getVals());
168  out.writeAttr(SUMO_ATTR_PROBS, (*it).second->getProbs());
169  out.closeTag();
170  }
171  for (VehicleDictType::iterator it = myVehicleDict.begin(); it != myVehicleDict.end(); ++it) {
172  (*it).second->saveState(out);
173  }
174 }
175 
176 
177 bool
178 MSVehicleControl::addVehicle(const std::string& id, SUMOVehicle* v) {
179  VehicleDictType::iterator it = myVehicleDict.find(id);
180  if (it == myVehicleDict.end()) {
181  // id not in myVehicleDict.
182  myVehicleDict[id] = v;
183  return true;
184  }
185  return false;
186 }
187 
188 
190 MSVehicleControl::getVehicle(const std::string& id) const {
191  VehicleDictType::const_iterator it = myVehicleDict.find(id);
192  if (it == myVehicleDict.end()) {
193  return 0;
194  }
195  return it->second;
196 }
197 
198 
199 void
201  myEndedVehNo++;
202  if (discard) {
203  myDiscarded++;
204  }
205  myVehicleDict.erase(veh->getID());
206  delete veh;
207 }
208 
209 
212  return myVehicleDict.begin();
213 }
214 
215 
218  return myVehicleDict.end();
219 }
220 
221 
222 bool
223 MSVehicleControl::checkVType(const std::string& id) {
224  if (id == DEFAULT_VTYPE_ID) {
226  delete myVTypeDict[id];
227  myVTypeDict.erase(myVTypeDict.find(id));
229  } else {
230  return false;
231  }
232  } else {
233  if (myVTypeDict.find(id) != myVTypeDict.end() || myVTypeDistDict.find(id) != myVTypeDistDict.end()) {
234  return false;
235  }
236  }
237  return true;
238 }
239 
240 bool
242  if (checkVType(vehType->getID())) {
243  myVTypeDict[vehType->getID()] = vehType;
244  return true;
245  }
246  return false;
247 }
248 
249 
250 bool
251 MSVehicleControl::addVTypeDistribution(const std::string& id, RandomDistributor<MSVehicleType*>* vehTypeDistribution) {
252  if (checkVType(id)) {
253  myVTypeDistDict[id] = vehTypeDistribution;
254  return true;
255  }
256  return false;
257 }
258 
259 
260 bool
261 MSVehicleControl::hasVTypeDistribution(const std::string& id) const {
262  return myVTypeDistDict.find(id) != myVTypeDistDict.end();
263 }
264 
265 
267 MSVehicleControl::getVType(const std::string& id, MTRand* rng) {
268  VTypeDictType::iterator it = myVTypeDict.find(id);
269  if (it == myVTypeDict.end()) {
270  VTypeDistDictType::iterator it2 = myVTypeDistDict.find(id);
271  if (it2 == myVTypeDistDict.end()) {
272  return 0;
273  }
274  return it2->second->get(rng);
275  }
276  if (id == DEFAULT_VTYPE_ID) {
278  }
279  return it->second;
280 }
281 
282 
283 void
284 MSVehicleControl::insertVTypeIDs(std::vector<std::string>& into) const {
285  into.reserve(into.size() + myVTypeDict.size() + myVTypeDistDict.size());
286  for (VTypeDictType::const_iterator i = myVTypeDict.begin(); i != myVTypeDict.end(); ++i) {
287  into.push_back((*i).first);
288  }
289  for (VTypeDistDictType::const_iterator i = myVTypeDistDict.begin(); i != myVTypeDistDict.end(); ++i) {
290  into.push_back((*i).first);
291  }
292 }
293 
294 
295 void
296 MSVehicleControl::addWaiting(const MSEdge* const edge, SUMOVehicle* vehicle) {
297  if (myWaiting.find(edge) == myWaiting.end()) {
298  myWaiting[edge] = std::vector<SUMOVehicle*>();
299  }
300  myWaiting[edge].push_back(vehicle);
301 }
302 
303 
304 void
305 MSVehicleControl::removeWaiting(const MSEdge* const edge, SUMOVehicle* vehicle) {
306  if (myWaiting.find(edge) != myWaiting.end()) {
307  std::vector<SUMOVehicle*>::iterator it = std::find(myWaiting[edge].begin(), myWaiting[edge].end(), vehicle);
308  if (it != myWaiting[edge].end()) {
309  myWaiting[edge].erase(it);
310  }
311  }
312 }
313 
314 
316 MSVehicleControl::getWaitingVehicle(const MSEdge* const edge, const std::set<std::string>& lines) {
317  if (myWaiting.find(edge) != myWaiting.end()) {
318  for (std::vector<SUMOVehicle*>::const_iterator it = myWaiting[edge].begin(); it != myWaiting[edge].end(); ++it) {
319  const std::string& line = (*it)->getParameter().line == "" ? (*it)->getParameter().id : (*it)->getParameter().line;
320  if (lines.count(line)) {
321  return (*it);
322  }
323  }
324  }
325  return 0;
326 }
327 
328 
329 void
331  for (VehicleDictType::iterator i = myVehicleDict.begin(); i != myVehicleDict.end(); ++i) {
332  WRITE_WARNING("Vehicle " + i->first + " aborted waiting for a person that will never come.");
333  }
334 }
335 
336 
337 unsigned int
339  frac = frac < 0 ? myScale : frac;
340  if (frac < 0 || frac == 1.) {
341  return 1;
342  }
343  // the vehicle in question has already been loaded, hence the '-1'
344  const unsigned int loaded = frac > 1. ? (unsigned int)(myLoadedVehNo / frac) : myLoadedVehNo - 1;
345  const unsigned int base = (unsigned int)frac;
346  const unsigned int resolution = 1000;
347  const unsigned int intFrac = (unsigned int)floor((frac - base) * resolution + 0.5);
348  // apply % twice to avoid integer overflow
349  if (((loaded % resolution) * intFrac) % resolution < intFrac) {
350  return base + 1;
351  }
352  return base;
353 }
354 
355 /****************************************************************************/
356 
The vehicle has departed (was inserted into the network)
Definition: MSNet.h:441
bool checkVType(const std::string &id)
Checks whether the vehicle type (distribution) may be added.
bool addVTypeDistribution(const std::string &id, RandomDistributor< MSVehicleType * > *vehTypeDistribution)
Adds a vehicle type distribution.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:257
void addWaiting(const MSEdge *const edge, SUMOVehicle *vehicle)
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:78
virtual void deleteVehicle(SUMOVehicle *v, bool discard=false)
Deletes the vehicle.
SUMOReal myTotalDepartureDelay
The aggregated time vehicles had to wait for departure (in seconds)
constVehIt loadedVehBegin() const
Returns the begin of the internal vehicle map.
unsigned int myDiscarded
The number of vehicles which were discarded while loading.
SUMOReal myTotalTravelTime
The aggregated time vehicles needed to aacomplish their route (in seconds)
Structure representing possible vehicle parameter.
std::map< const MSEdge *const, std::vector< SUMOVehicle * > > myWaiting
the lists of waiting vehicles
static MTRand * getParsingRNG()
MSVehicleControl()
Constructor.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:156
VehicleDictType myVehicleDict
Dictionary of vehicles.
SUMOReal computeChosenSpeedDeviation(MTRand *rng, const SUMOReal minDevFactor=0.2) const
Computes and returns the speed deviation.
virtual bool addVehicle(const std::string &id, SUMOVehicle *v)
Tries to insert the vehicle into the internal vehicle container.
VTypeDictType myVTypeDict
Dictionary of vehicle types.
void removeWaiting(const MSEdge *const edge, SUMOVehicle *vehicle)
SUMOReal getFloat(const std::string &name) const
Returns the SUMOReal-value of the named option (only for Option_Float)
void insertVTypeIDs(std::vector< std::string > &into) const
Inserts ids of all known vehicle types and vehicle type distributions to the given vector...
bool myDefaultVTypeMayBeDeleted
Whether no vehicle type was loaded.
constVehIt loadedVehEnd() const
Returns the end of the internal vehicle map.
const std::string DEFAULT_VTYPE_ID
static MSVehicleType * build(SUMOVTypeParameter &from)
Builds the microsim vehicle type described by the given parameter.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
The car-following model and parameter.
Definition: MSVehicleType.h:74
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:67
SUMOVehicle * getWaitingVehicle(const MSEdge *const edge, const std::set< std::string > &lines)
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
A road/street connecting two junctions.
Definition: MSEdge.h:75
#define STEPFLOOR(x)
Definition: SUMOTime.h:67
virtual const std::vector< MSDevice * > & getDevices() const =0
Returns this vehicle&#39;s devices.
The vehicle arrived at his destination (is deleted)
Definition: MSNet.h:447
void setState(int runningVehNo, int endedVehNo, SUMOReal totalDepartureDelay, SUMOReal totalTravelTime)
Sets the current state variables as loaded from the stream.
SUMOTime computeRandomDepartOffset() const
compute (optional) random offset to the departure time
Representation of a vehicle.
Definition: SUMOVehicle.h:64
SUMOReal myScale
The scaling factor (especially for inc-dua)
virtual SUMOVehicle * buildVehicle(SUMOVehicleParameter *defs, const MSRoute *route, const MSVehicleType *type, const bool ignoreStopErrors, const bool fromRouteFile=true)
Builds a vehicle, increases the number of built vehicles.
SUMOTime depart
The vehicle&#39;s departure time.
#define STEPS2TIME(x)
Definition: SUMOTime.h:65
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:48
SUMOTime myMaxRandomDepartOffset
The maximum random offset to be added to vehicles departure times (non-negative)
void saveState(OutputDevice &out)
Saves the current state into the given stream.
unsigned int myEndedVehNo
The number of removed vehicles.
std::string line
The vehicle&#39;s line (mainly for public transport)
void abortWaiting()
removes any vehicles that are still waiting
The vehicle was built, but has not yet departed.
Definition: MSNet.h:439
bool addVType(MSVehicleType *vehType)
Adds a vehicle type.
void addStops(const bool ignoreStopErrors)
Adds stops to the built vehicle.
void scheduleVehicleRemoval(SUMOVehicle *veh)
Removes a vehicle after it has ended.
Structure representing possible vehicle parameter.
VTypeDistDictType myVTypeDistDict
A distribution of vehicle types (probability-&gt;vehicle type)
virtual SUMOTime getDeparture() const =0
Returns this vehicle&#39;s real departure time.
static OutputDevice & getDeviceByOption(const std::string &name)
Returns the device described by the option.
virtual const SUMOVehicleParameter & getParameter() const =0
Returns the vehicle&#39;s parameter (including departure definition)
virtual ~MSVehicleControl()
Destructor.
A storage for options typed value containers)
Definition: OptionsCont.h:108
const std::string & getID() const
Returns the name of the vehicle type.
int SUMOTime
Definition: SUMOTime.h:43
std::map< std::string, SUMOVehicle * >::const_iterator constVehIt
Definition of the internal vehicles map iterator.
void informVehicleStateListener(const SUMOVehicle *const vehicle, VehicleState to)
Informs all added listeners about a vehicle&#39;s state change.
Definition: MSNet.cpp:704
SUMOVehicle * getVehicle(const std::string &id) const
Returns the vehicle with the given id.
bool hasVTypeDistribution(const std::string &id) const
Asks for a vehicle type distribution.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:218
#define DELTA_T
Definition: SUMOTime.h:50
void vehicleDeparted(const SUMOVehicle &v)
Informs this control about a vehicle&#39;s departure.
unsigned int myLoadedVehNo
The number of build vehicles.
MSVehicleType * getVType(const std::string &id=DEFAULT_VTYPE_ID, MTRand *rng=0)
Returns the named vehicle type or a sample from the named distribution.
virtual const std::string & getID() const =0
Get the vehicle&#39;s ID.
vehicles ignoring classes
unsigned int getQuota(SUMOReal frac=-1) const
Returns the number of instances of the current vehicle that shall be emitted considering that &quot;frac&quot; ...
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
unsigned int myRunningVehNo
The number of vehicles within the network (build and inserted but not removed)
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.