SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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-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  myMaxSpeedFactor(1),
72  myMinDeceleration(SUMOVTypeParameter::getDefaultDecel(SVC_IGNORING)) {
79  myScale = oc.getFloat("scale");
80  myMaxRandomDepartOffset = string2time(oc.getString("random-depart-offset"));
81 }
82 
83 
85  // delete vehicles
86  for (VehicleDictType::iterator i = myVehicleDict.begin(); i != myVehicleDict.end(); ++i) {
87  delete(*i).second;
88  }
89  myVehicleDict.clear();
90  // delete vehicle type distributions
91  for (VTypeDistDictType::iterator i = myVTypeDistDict.begin(); i != myVTypeDistDict.end(); ++i) {
92  delete(*i).second;
93  }
94  myVTypeDistDict.clear();
95  // delete vehicle types
96  for (VTypeDictType::iterator i = myVTypeDict.begin(); i != myVTypeDict.end(); ++i) {
97  delete(*i).second;
98  }
99  myVTypeDict.clear();
100 }
101 
102 SUMOTime
104  if (myMaxRandomDepartOffset > 0) {
105  // round to the closest usable simulation step
106  return DELTA_T * int((MSRouteHandler::getParsingRNG()->rand((int)myMaxRandomDepartOffset) + 0.5 * DELTA_T) / DELTA_T);
107  } else {
108  return 0;
109  }
110 }
111 
114  const MSRoute* route,
115  const MSVehicleType* type,
116  const bool ignoreStopErrors, const bool fromRouteFile) {
117  myLoadedVehNo++;
118  if (fromRouteFile) {
120  }
121  MSVehicle* built = new MSVehicle(defs, route, type, type->computeChosenSpeedDeviation(fromRouteFile ? MSRouteHandler::getParsingRNG() : 0));
122  built->addStops(ignoreStopErrors);
124  return built;
125 }
126 
127 
128 void
130  assert(myRunningVehNo > 0);
131  myTotalTravelTime += STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep() - veh->getDeparture());
132  myRunningVehNo--;
134  for (std::vector<MSDevice*>::const_iterator i = veh->getDevices().begin(); i != veh->getDevices().end(); ++i) {
135  (*i)->generateOutput();
136  }
137  if (OptionsCont::getOptions().isSet("tripinfo-output")) {
138  // close tag after tripinfo (possibly including emissions from another device) have been written
139  OutputDevice::getDeviceByOption("tripinfo-output").closeTag();
140  }
141  deleteVehicle(veh);
142 }
143 
144 
145 void
147  ++myRunningVehNo;
151  myMinDeceleration = MIN2(myMinDeceleration, v.getVehicleType().getCarFollowModel().getMaxDecel());
152 }
153 
154 
155 void
156 MSVehicleControl::setState(int runningVehNo, int loadedVehNo, int endedVehNo, SUMOReal totalDepartureDelay, SUMOReal totalTravelTime) {
157  myRunningVehNo = runningVehNo;
158  myLoadedVehNo = loadedVehNo;
159  myEndedVehNo = endedVehNo;
160  myTotalDepartureDelay = totalDepartureDelay;
161  myTotalTravelTime = totalTravelTime;
162 }
163 
164 
165 void
167  out.openTag(SUMO_TAG_DELAY);
173  // save vehicle types
174  for (VTypeDictType::iterator it = myVTypeDict.begin(); it != myVTypeDict.end(); ++it) {
175  it->second->getParameter().write(out);
176  }
177  for (VTypeDistDictType::iterator it = myVTypeDistDict.begin(); it != myVTypeDistDict.end(); ++it) {
179  out.writeAttr(SUMO_ATTR_VTYPES, (*it).second->getVals());
180  out.writeAttr(SUMO_ATTR_PROBS, (*it).second->getProbs());
181  out.closeTag();
182  }
183  for (VehicleDictType::iterator it = myVehicleDict.begin(); it != myVehicleDict.end(); ++it) {
184  (*it).second->saveState(out);
185  }
186 }
187 
188 
189 bool
190 MSVehicleControl::addVehicle(const std::string& id, SUMOVehicle* v) {
191  VehicleDictType::iterator it = myVehicleDict.find(id);
192  if (it == myVehicleDict.end()) {
193  // id not in myVehicleDict.
194  myVehicleDict[id] = v;
195  return true;
196  }
197  return false;
198 }
199 
200 
202 MSVehicleControl::getVehicle(const std::string& id) const {
203  VehicleDictType::const_iterator it = myVehicleDict.find(id);
204  if (it == myVehicleDict.end()) {
205  return 0;
206  }
207  return it->second;
208 }
209 
210 
211 void
213  myEndedVehNo++;
214  if (discard) {
215  myDiscarded++;
216  }
217  if (veh != 0) {
218  myVehicleDict.erase(veh->getID());
219  }
220  delete veh;
221 }
222 
223 
224 bool
225 MSVehicleControl::checkVType(const std::string& id) {
226  if (id == DEFAULT_VTYPE_ID) {
228  delete myVTypeDict[id];
229  myVTypeDict.erase(myVTypeDict.find(id));
231  } else {
232  return false;
233  }
234  } else if (id == DEFAULT_PEDTYPE_ID) {
236  delete myVTypeDict[id];
237  myVTypeDict.erase(myVTypeDict.find(id));
239  } else {
240  return false;
241  }
242  } else {
243  if (myVTypeDict.find(id) != myVTypeDict.end() || myVTypeDistDict.find(id) != myVTypeDistDict.end()) {
244  return false;
245  }
246  }
247  return true;
248 }
249 
250 bool
252  if (checkVType(vehType->getID())) {
253  myVTypeDict[vehType->getID()] = vehType;
254  return true;
255  }
256  return false;
257 }
258 
259 
260 bool
261 MSVehicleControl::addVTypeDistribution(const std::string& id, RandomDistributor<MSVehicleType*>* vehTypeDistribution) {
262  if (checkVType(id)) {
263  myVTypeDistDict[id] = vehTypeDistribution;
264  return true;
265  }
266  return false;
267 }
268 
269 
270 bool
271 MSVehicleControl::hasVTypeDistribution(const std::string& id) const {
272  return myVTypeDistDict.find(id) != myVTypeDistDict.end();
273 }
274 
275 
277 MSVehicleControl::getVType(const std::string& id, MTRand* rng) {
278  VTypeDictType::iterator it = myVTypeDict.find(id);
279  if (it == myVTypeDict.end()) {
280  VTypeDistDictType::iterator it2 = myVTypeDistDict.find(id);
281  if (it2 == myVTypeDistDict.end()) {
282  return 0;
283  }
284  return it2->second->get(rng);
285  }
286  if (id == DEFAULT_VTYPE_ID) {
288  } else if (id == DEFAULT_PEDTYPE_ID) {
290  }
291  return it->second;
292 }
293 
294 
295 void
296 MSVehicleControl::insertVTypeIDs(std::vector<std::string>& into) const {
297  into.reserve(into.size() + myVTypeDict.size() + myVTypeDistDict.size());
298  for (VTypeDictType::const_iterator i = myVTypeDict.begin(); i != myVTypeDict.end(); ++i) {
299  into.push_back((*i).first);
300  }
301  for (VTypeDistDictType::const_iterator i = myVTypeDistDict.begin(); i != myVTypeDistDict.end(); ++i) {
302  into.push_back((*i).first);
303  }
304 }
305 
306 
307 void
308 MSVehicleControl::addWaiting(const MSEdge* const edge, SUMOVehicle* vehicle) {
309  if (myWaiting.find(edge) == myWaiting.end()) {
310  myWaiting[edge] = std::vector<SUMOVehicle*>();
311  }
312  myWaiting[edge].push_back(vehicle);
313 }
314 
315 
316 void
317 MSVehicleControl::removeWaiting(const MSEdge* const edge, SUMOVehicle* vehicle) {
318  if (myWaiting.find(edge) != myWaiting.end()) {
319  std::vector<SUMOVehicle*>::iterator it = std::find(myWaiting[edge].begin(), myWaiting[edge].end(), vehicle);
320  if (it != myWaiting[edge].end()) {
321  myWaiting[edge].erase(it);
322  }
323  }
324 }
325 
326 
328 MSVehicleControl::getWaitingVehicle(const MSEdge* const edge, const std::set<std::string>& lines, const SUMOReal position, const std::string ridingID) {
329  if (myWaiting.find(edge) != myWaiting.end()) {
330  // for every vehicle waiting vehicle at this edge
331  std::vector<SUMOVehicle*> waitingTooFarAway;
332  for (std::vector<SUMOVehicle*>::const_iterator it = myWaiting[edge].begin(); it != myWaiting[edge].end(); ++it) {
333  const std::string& line = (*it)->getParameter().line == "" ? (*it)->getParameter().id : (*it)->getParameter().line;
334  SUMOReal vehiclePosition = (*it)->getPositionOnLane();
335  // if the line of the vehicle is contained in the set of given lines and the vehicle is stopped and is positioned
336  // in the interval [position - t, position + t] for a tolerance t=10
337  if (lines.count(line)) {
338  if ((position - 10 <= vehiclePosition) && (vehiclePosition <= position + 10)) {
339  return (*it);
340  } else if ((*it)->isStoppedTriggered() ||
341  (*it)->getParameter().departProcedure == DEPART_TRIGGERED) {
342  // maybe we are within the range of the stop
343  MSVehicle* veh = static_cast<MSVehicle*>(*it);
344  if (veh->isStoppedInRange(position)) {
345  return (*it);
346  } else {
347  waitingTooFarAway.push_back(*it);
348  }
349  }
350  }
351  }
352  for (std::vector<SUMOVehicle*>::iterator it = waitingTooFarAway.begin(); it != waitingTooFarAway.end(); ++it) {
353  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.");
354  }
355  }
356  return 0;
357 }
358 
359 
360 void
362  for (VehicleDictType::iterator i = myVehicleDict.begin(); i != myVehicleDict.end(); ++i) {
363  WRITE_WARNING("Vehicle " + i->first + " aborted waiting for a person or a container that will never come.");
364  }
365 }
366 
367 
368 int
370  frac = frac < 0 ? myScale : frac;
371  if (frac < 0 || frac == 1.) {
372  return 1;
373  }
374  // the vehicle in question has already been loaded, hence the '-1'
375  const int loaded = frac > 1. ? (int)(myLoadedVehNo / frac) : myLoadedVehNo - 1;
376  const int base = (int)frac;
377  const int resolution = 1000;
378  const int intFrac = (int)floor((frac - base) * resolution + 0.5);
379  // apply % twice to avoid integer overflow
380  if (((loaded % resolution) * intFrac) % resolution < intFrac) {
381  return base + 1;
382  }
383  return base;
384 }
385 
386 int
389 }
390 
391 /****************************************************************************/
392 
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:82
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:973
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'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'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:1021
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'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's maximum deceleration [m/s^2].
Definition: MSCFModel.h:201
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'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'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'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'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:214
void vehicleDeparted(const SUMOVehicle &v)
Informs this control about a vehicle'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's ID.
vehicles ignoring classes
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle's type.