SUMO - Simulation of Urban MObility
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-2016 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 "MSEdge.h"
37 #include "MSNet.h"
38 #include "MSRouteHandler.h"
41 #include <utils/common/RGBColor.h>
46 
47 #ifdef CHECK_MEMORY_LEAKS
48 #include <foreign/nvwa/debug_new.h>
49 #endif // CHECK_MEMORY_LEAKS
50 
51 
52 // ===========================================================================
53 // member method definitions
54 // ===========================================================================
56  myLoadedVehNo(0),
57  myRunningVehNo(0),
58  myEndedVehNo(0),
59  myDiscarded(0),
60  myCollisions(0),
61  myTeleportsJam(0),
62  myTeleportsYield(0),
63  myTeleportsWrongLane(0),
64  myEmergencyStops(0),
65  myTotalDepartureDelay(0),
66  myTotalTravelTime(0),
67  myDefaultVTypeMayBeDeleted(true),
68  myDefaultPedTypeMayBeDeleted(true),
69  myWaitingForPerson(0),
70  myWaitingForContainer(0),
71  myScale(-1),
72  myMaxSpeedFactor(1),
73  myMinDeceleration(SUMOVTypeParameter::getDefaultDecel(SVC_IGNORING)) {
80  if (oc.isSet("scale")) {
81  myScale = oc.getFloat("scale");
82  }
83  myMaxRandomDepartOffset = string2time(oc.getString("random-depart-offset"));
84 }
85 
86 
88  // delete vehicles
89  for (VehicleDictType::iterator i = myVehicleDict.begin(); i != myVehicleDict.end(); ++i) {
90  delete(*i).second;
91  }
92  myVehicleDict.clear();
93  // delete vehicle type distributions
94  for (VTypeDistDictType::iterator i = myVTypeDistDict.begin(); i != myVTypeDistDict.end(); ++i) {
95  delete(*i).second;
96  }
97  myVTypeDistDict.clear();
98  // delete vehicle types
99  for (VTypeDictType::iterator i = myVTypeDict.begin(); i != myVTypeDict.end(); ++i) {
100  delete(*i).second;
101  }
102  myVTypeDict.clear();
103 }
104 
105 SUMOTime
107  if (myMaxRandomDepartOffset > 0) {
108  // round to the closest usable simulation step
109  return DELTA_T * int((MSRouteHandler::getParsingRNG()->rand((int)myMaxRandomDepartOffset) + 0.5 * DELTA_T) / DELTA_T);
110  } else {
111  return 0;
112  }
113 }
114 
117  const MSRoute* route,
118  const MSVehicleType* type,
119  const bool ignoreStopErrors, const bool fromRouteFile) {
120  myLoadedVehNo++;
121  if (fromRouteFile) {
123  }
124  MSVehicle* built = new MSVehicle(defs, route, type, type->computeChosenSpeedDeviation(fromRouteFile ? MSRouteHandler::getParsingRNG() : 0));
125  built->addStops(ignoreStopErrors);
127  return built;
128 }
129 
130 
131 void
133  assert(myRunningVehNo > 0);
134  myTotalTravelTime += STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep() - veh->getDeparture());
135  myRunningVehNo--;
137  for (std::vector<MSDevice*>::const_iterator i = veh->getDevices().begin(); i != veh->getDevices().end(); ++i) {
138  (*i)->generateOutput();
139  }
140  if (OptionsCont::getOptions().isSet("tripinfo-output")) {
141  // close tag after tripinfo (possibly including emissions from another device) have been written
142  OutputDevice::getDeviceByOption("tripinfo-output").closeTag();
143  }
144  deleteVehicle(veh);
145 }
146 
147 
148 void
150  ++myRunningVehNo;
154  myMinDeceleration = MIN2(myMinDeceleration, v.getVehicleType().getCarFollowModel().getMaxDecel());
155 }
156 
157 
158 void
159 MSVehicleControl::setState(int runningVehNo, int loadedVehNo, int endedVehNo, SUMOReal totalDepartureDelay, SUMOReal totalTravelTime) {
160  myRunningVehNo = runningVehNo;
161  myLoadedVehNo = loadedVehNo;
162  myEndedVehNo = endedVehNo;
163  myTotalDepartureDelay = totalDepartureDelay;
164  myTotalTravelTime = totalTravelTime;
165 }
166 
167 
168 void
170  out.openTag(SUMO_TAG_DELAY);
176  // save vehicle types
177  for (VTypeDictType::iterator it = myVTypeDict.begin(); it != myVTypeDict.end(); ++it) {
178  it->second->getParameter().write(out);
179  }
180  for (VTypeDistDictType::iterator it = myVTypeDistDict.begin(); it != myVTypeDistDict.end(); ++it) {
182  out.writeAttr(SUMO_ATTR_VTYPES, (*it).second->getVals());
183  out.writeAttr(SUMO_ATTR_PROBS, (*it).second->getProbs());
184  out.closeTag();
185  }
186  for (VehicleDictType::iterator it = myVehicleDict.begin(); it != myVehicleDict.end(); ++it) {
187  (*it).second->saveState(out);
188  }
189 }
190 
191 
192 bool
193 MSVehicleControl::addVehicle(const std::string& id, SUMOVehicle* v) {
194  VehicleDictType::iterator it = myVehicleDict.find(id);
195  if (it == myVehicleDict.end()) {
196  // id not in myVehicleDict.
197  myVehicleDict[id] = v;
198  return true;
199  }
200  return false;
201 }
202 
203 
205 MSVehicleControl::getVehicle(const std::string& id) const {
206  VehicleDictType::const_iterator it = myVehicleDict.find(id);
207  if (it == myVehicleDict.end()) {
208  return 0;
209  }
210  return it->second;
211 }
212 
213 
214 void
216  myEndedVehNo++;
217  if (discard) {
218  myDiscarded++;
219  }
220  if (veh != 0) {
221  myVehicleDict.erase(veh->getID());
222  }
223  delete veh;
224 }
225 
226 
227 bool
228 MSVehicleControl::checkVType(const std::string& id) {
229  if (id == DEFAULT_VTYPE_ID) {
231  delete myVTypeDict[id];
232  myVTypeDict.erase(myVTypeDict.find(id));
234  } else {
235  return false;
236  }
237  } else if (id == DEFAULT_PEDTYPE_ID) {
239  delete myVTypeDict[id];
240  myVTypeDict.erase(myVTypeDict.find(id));
242  } else {
243  return false;
244  }
245  } else {
246  if (myVTypeDict.find(id) != myVTypeDict.end() || myVTypeDistDict.find(id) != myVTypeDistDict.end()) {
247  return false;
248  }
249  }
250  return true;
251 }
252 
253 bool
255  if (checkVType(vehType->getID())) {
256  myVTypeDict[vehType->getID()] = vehType;
257  return true;
258  }
259  return false;
260 }
261 
262 
263 bool
264 MSVehicleControl::addVTypeDistribution(const std::string& id, RandomDistributor<MSVehicleType*>* vehTypeDistribution) {
265  if (checkVType(id)) {
266  myVTypeDistDict[id] = vehTypeDistribution;
267  return true;
268  }
269  return false;
270 }
271 
272 
273 bool
274 MSVehicleControl::hasVTypeDistribution(const std::string& id) const {
275  return myVTypeDistDict.find(id) != myVTypeDistDict.end();
276 }
277 
278 
280 MSVehicleControl::getVType(const std::string& id, MTRand* rng) {
281  VTypeDictType::iterator it = myVTypeDict.find(id);
282  if (it == myVTypeDict.end()) {
283  VTypeDistDictType::iterator it2 = myVTypeDistDict.find(id);
284  if (it2 == myVTypeDistDict.end()) {
285  return 0;
286  }
287  return it2->second->get(rng);
288  }
289  if (id == DEFAULT_VTYPE_ID) {
291  } else if (id == DEFAULT_PEDTYPE_ID) {
293  }
294  return it->second;
295 }
296 
297 
298 void
299 MSVehicleControl::insertVTypeIDs(std::vector<std::string>& into) const {
300  into.reserve(into.size() + myVTypeDict.size() + myVTypeDistDict.size());
301  for (VTypeDictType::const_iterator i = myVTypeDict.begin(); i != myVTypeDict.end(); ++i) {
302  into.push_back((*i).first);
303  }
304  for (VTypeDistDictType::const_iterator i = myVTypeDistDict.begin(); i != myVTypeDistDict.end(); ++i) {
305  into.push_back((*i).first);
306  }
307 }
308 
309 
310 void
311 MSVehicleControl::addWaiting(const MSEdge* const edge, SUMOVehicle* vehicle) {
312  if (myWaiting.find(edge) == myWaiting.end()) {
313  myWaiting[edge] = std::vector<SUMOVehicle*>();
314  }
315  myWaiting[edge].push_back(vehicle);
316 }
317 
318 
319 void
320 MSVehicleControl::removeWaiting(const MSEdge* const edge, SUMOVehicle* vehicle) {
321  if (myWaiting.find(edge) != myWaiting.end()) {
322  std::vector<SUMOVehicle*>::iterator it = std::find(myWaiting[edge].begin(), myWaiting[edge].end(), vehicle);
323  if (it != myWaiting[edge].end()) {
324  myWaiting[edge].erase(it);
325  }
326  }
327 }
328 
329 
331 MSVehicleControl::getWaitingVehicle(const MSEdge* const edge, const std::set<std::string>& lines, const SUMOReal position, const std::string ridingID) {
332  if (myWaiting.find(edge) != myWaiting.end()) {
333  // for every vehicle waiting vehicle at this edge
334  std::vector<SUMOVehicle*> waitingTooFarAway;
335  for (std::vector<SUMOVehicle*>::const_iterator it = myWaiting[edge].begin(); it != myWaiting[edge].end(); ++it) {
336  const std::string& line = (*it)->getParameter().line == "" ? (*it)->getParameter().id : (*it)->getParameter().line;
337  SUMOReal vehiclePosition = (*it)->getPositionOnLane();
338  // if the line of the vehicle is contained in the set of given lines and the vehicle is stopped and is positioned
339  // in the interval [position - t, position + t] for a tolerance t=10
340  if (lines.count(line)) {
341  if ((position - 10 <= vehiclePosition) && (vehiclePosition <= position + 10)) {
342  return (*it);
343  } else if ((*it)->isStoppedTriggered() ||
344  (*it)->getParameter().departProcedure == DEPART_TRIGGERED) {
345  // maybe we are within the range of the stop
346  MSVehicle* veh = static_cast<MSVehicle*>(*it);
347  if (veh->isStoppedInRange(position)) {
348  return (*it);
349  } else {
350  waitingTooFarAway.push_back(*it);
351  }
352  }
353  }
354  }
355  for (std::vector<SUMOVehicle*>::iterator it = waitingTooFarAway.begin(); it != waitingTooFarAway.end(); ++it) {
356  WRITE_WARNING(ridingID + " at edge '" + edge->getID() + "' position " + toString(position) + " cannot use waiting vehicle '" + (*it)->getID() + "' at position " + toString((*it)->getPositionOnLane()) + " because it is too far away.");
357  }
358  }
359  return 0;
360 }
361 
362 
363 void
365  for (VehicleDictType::iterator i = myVehicleDict.begin(); i != myVehicleDict.end(); ++i) {
366  WRITE_WARNING("Vehicle " + i->first + " aborted waiting for a person or a container that will never come.");
367  }
368 }
369 
370 
371 int
373  frac = frac < 0 ? myScale : frac;
374  if (frac < 0 || frac == 1.) {
375  return 1;
376  }
377  // the vehicle in question has already been loaded, hence the '-1'
378  const int loaded = frac > 1. ? (int)(myLoadedVehNo / frac) : myLoadedVehNo - 1;
379  const int base = (int)frac;
380  const int resolution = 1000;
381  const int intFrac = (int)floor((frac - base) * resolution + 0.5);
382  // apply % twice to avoid integer overflow
383  if (((loaded % resolution) * intFrac) % resolution < intFrac) {
384  return base + 1;
385  }
386  return base;
387 }
388 
389 int
392 }
393 
394 /****************************************************************************/
395 
The departure is person triggered.
The vehicle has departed (was inserted into the network)
Definition: MSNet.h:541
bool myDefaultPedTypeMayBeDeleted
Whether no pedestrian type was loaded.
bool checkVType(const std::string &id)
Checks whether the vehicle type (distribution) may be added.
SUMOReal myMaxSpeedFactor
The maximum speed factor for all vehicles in the network.
int getTeleportCount() const
return the number of teleports (including collisions)
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)
Adds a vehicle to the list of waiting vehiclse to a given edge.
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:81
virtual void deleteVehicle(SUMOVehicle *v, bool discard=false)
Deletes the vehicle.
long long int SUMOTime
Definition: SUMOTime.h:43
SUMOReal myTotalDepartureDelay
The aggregated time vehicles had to wait for departure (in seconds)
int myTeleportsWrongLane
The number of teleports due to vehicles stuck on the wrong lane.
SUMOReal myMinDeceleration
The minimum deceleration capability for all vehicles in the network.
is a pedestrian
int myEndedVehNo
The number of removed vehicles.
int getQuota(SUMOReal frac=-1) const
Returns the number of instances of the current vehicle that shall be emitted considering that "frac" ...
static bool teleportOnCollision()
Definition: MSLane.h:962
SUMOReal myTotalTravelTime
The aggregated time vehicles needed to aacomplish their route (in seconds)
int myDiscarded
The number of vehicles which were discarded while loading.
Structure representing possible vehicle parameter.
std::map< const MSEdge *const, std::vector< SUMOVehicle * > > myWaiting
the lists of waiting vehicles to a given edge
static MTRand * getParsingRNG()
MSVehicleControl()
Constructor.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:159
T MAX2(T a, T b)
Definition: StdDefs.h:75
VehicleDictType myVehicleDict
Dictionary of vehicles.
SUMOTime DELTA_T
Definition: SUMOTime.cpp:39
SUMOReal computeChosenSpeedDeviation(MTRand *rng, const SUMOReal minDevFactor=0.2) const
Computes and returns the speed deviation.
SUMOVehicle * getWaitingVehicle(const MSEdge *const edge, const std::set< std::string > &lines, const SUMOReal position, const std::string ridingID)
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.
int myTeleportsJam
The number of teleports due to jam.
void removeWaiting(const MSEdge *const edge, SUMOVehicle *vehicle)
Removes a vehicle from the list of waiting vehicles to a given edge.
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.
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:69
const MSCFModel & getCarFollowModel() const
Returns the vehicle type&#39;s car following model definition (const version)
virtual SUMOReal getChosenSpeedFactor() const =0
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
const std::string & getID() const
Returns the id.
Definition: Named.h:66
A road/street connecting two junctions.
Definition: MSEdge.h:80
#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:547
SUMOTime computeRandomDepartOffset() const
compute (optional) random offset to the departure time
Representation of a vehicle.
Definition: SUMOVehicle.h:66
SUMOReal myScale
The scaling factor (especially for inc-dua)
bool isStoppedInRange(SUMOReal pos) const
return whether the given position is within range of the current stop
Definition: MSVehicle.cpp:1006
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
int myLoadedVehNo
The number of build vehicles.
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:46
T MIN2(T a, T b)
Definition: StdDefs.h:69
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.
void setState(int runningVehNo, int loadedVehNo, int endedVehNo, SUMOReal totalDepartureDelay, SUMOReal totalTravelTime)
Sets the current state variables as loaded from the stream.
SUMOReal getMaxDecel() const
Get the vehicle type&#39;s maximum deceleration [m/s^2].
Definition: MSCFModel.h:186
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:55
int myRunningVehNo
The number of vehicles within the network (build and inserted but not removed)
vehicle is a passenger car (a "normal" car)
std::string line
The vehicle&#39;s line (mainly for public transport)
void abortWaiting()
informes about all waiting vehicles (deletion in destructor)
The vehicle was built, but has not yet departed.
Definition: MSNet.h:539
bool addVType(MSVehicleType *vehType)
Adds a vehicle type.
int setParameter
Information for the router which parameter were set.
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->vehicle type)
virtual SUMOTime getDeparture() const =0
Returns this vehicle&#39;s real departure time.
int myCollisions
The number of collisions.
const std::string DEFAULT_PEDTYPE_ID
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:99
const std::string & getID() const
Returns the name of the vehicle type.
void informVehicleStateListener(const SUMOVehicle *const vehicle, VehicleState to)
Informs all added listeners about a vehicle&#39;s state change.
Definition: MSNet.cpp:773
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:213
void vehicleDeparted(const SUMOVehicle &v)
Informs this control about a vehicle&#39;s departure.
int myTeleportsYield
The number of teleports due to vehicles stuck on a minor road.
MSVehicleType * getVType(const std::string &id=DEFAULT_VTYPE_ID, MTRand *rng=0)
Returns the named vehicle type or a sample from the named distribution.
const int VTYPEPARS_VEHICLECLASS_SET
virtual const std::string & getID() const =0
Get the vehicle&#39;s ID.
vehicles ignoring classes
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle&#39;s type.