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-2015 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 endedVehNo, SUMOReal totalDepartureDelay, SUMOReal totalTravelTime) {
160  myRunningVehNo = runningVehNo;
161  myEndedVehNo = endedVehNo;
162  myTotalDepartureDelay = totalDepartureDelay;
163  myTotalTravelTime = totalTravelTime;
164 }
165 
166 
167 void
171  // save vehicle types
172  for (VTypeDictType::iterator it = myVTypeDict.begin(); it != myVTypeDict.end(); ++it) {
173  it->second->getParameter().write(out);
174  }
175  for (VTypeDistDictType::iterator it = myVTypeDistDict.begin(); it != myVTypeDistDict.end(); ++it) {
177  out.writeAttr(SUMO_ATTR_VTYPES, (*it).second->getVals());
178  out.writeAttr(SUMO_ATTR_PROBS, (*it).second->getProbs());
179  out.closeTag();
180  }
181  for (VehicleDictType::iterator it = myVehicleDict.begin(); it != myVehicleDict.end(); ++it) {
182  (*it).second->saveState(out);
183  }
184 }
185 
186 
187 bool
188 MSVehicleControl::addVehicle(const std::string& id, SUMOVehicle* v) {
189  VehicleDictType::iterator it = myVehicleDict.find(id);
190  if (it == myVehicleDict.end()) {
191  // id not in myVehicleDict.
192  myVehicleDict[id] = v;
193  return true;
194  }
195  return false;
196 }
197 
198 
200 MSVehicleControl::getVehicle(const std::string& id) const {
201  VehicleDictType::const_iterator it = myVehicleDict.find(id);
202  if (it == myVehicleDict.end()) {
203  return 0;
204  }
205  return it->second;
206 }
207 
208 
209 void
211  myEndedVehNo++;
212  if (discard) {
213  myDiscarded++;
214  }
215  if (veh != 0) {
216  myVehicleDict.erase(veh->getID());
217  }
218  delete veh;
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 if (id == DEFAULT_PEDTYPE_ID) {
234  delete myVTypeDict[id];
235  myVTypeDict.erase(myVTypeDict.find(id));
237  } else {
238  return false;
239  }
240  } else {
241  if (myVTypeDict.find(id) != myVTypeDict.end() || myVTypeDistDict.find(id) != myVTypeDistDict.end()) {
242  return false;
243  }
244  }
245  return true;
246 }
247 
248 bool
250  if (checkVType(vehType->getID())) {
251  myVTypeDict[vehType->getID()] = vehType;
252  return true;
253  }
254  return false;
255 }
256 
257 
258 bool
259 MSVehicleControl::addVTypeDistribution(const std::string& id, RandomDistributor<MSVehicleType*>* vehTypeDistribution) {
260  if (checkVType(id)) {
261  myVTypeDistDict[id] = vehTypeDistribution;
262  return true;
263  }
264  return false;
265 }
266 
267 
268 bool
269 MSVehicleControl::hasVTypeDistribution(const std::string& id) const {
270  return myVTypeDistDict.find(id) != myVTypeDistDict.end();
271 }
272 
273 
275 MSVehicleControl::getVType(const std::string& id, MTRand* rng) {
276  VTypeDictType::iterator it = myVTypeDict.find(id);
277  if (it == myVTypeDict.end()) {
278  VTypeDistDictType::iterator it2 = myVTypeDistDict.find(id);
279  if (it2 == myVTypeDistDict.end()) {
280  return 0;
281  }
282  return it2->second->get(rng);
283  }
284  if (id == DEFAULT_VTYPE_ID) {
286  } else if (id == DEFAULT_PEDTYPE_ID) {
288  }
289  return it->second;
290 }
291 
292 
293 void
294 MSVehicleControl::insertVTypeIDs(std::vector<std::string>& into) const {
295  into.reserve(into.size() + myVTypeDict.size() + myVTypeDistDict.size());
296  for (VTypeDictType::const_iterator i = myVTypeDict.begin(); i != myVTypeDict.end(); ++i) {
297  into.push_back((*i).first);
298  }
299  for (VTypeDistDictType::const_iterator i = myVTypeDistDict.begin(); i != myVTypeDistDict.end(); ++i) {
300  into.push_back((*i).first);
301  }
302 }
303 
304 
305 void
306 MSVehicleControl::addWaiting(const MSEdge* const edge, SUMOVehicle* vehicle) {
307  if (myWaiting.find(edge) == myWaiting.end()) {
308  myWaiting[edge] = std::vector<SUMOVehicle*>();
309  }
310  myWaiting[edge].push_back(vehicle);
311 }
312 
313 
314 void
315 MSVehicleControl::removeWaiting(const MSEdge* const edge, SUMOVehicle* vehicle) {
316  if (myWaiting.find(edge) != myWaiting.end()) {
317  std::vector<SUMOVehicle*>::iterator it = std::find(myWaiting[edge].begin(), myWaiting[edge].end(), vehicle);
318  if (it != myWaiting[edge].end()) {
319  myWaiting[edge].erase(it);
320  }
321  }
322 }
323 
324 
326 MSVehicleControl::getWaitingVehicle(const MSEdge* const edge, const std::set<std::string>& lines, const SUMOReal position, const std::string ridingID) {
327  if (myWaiting.find(edge) != myWaiting.end()) {
328  // for every vehicle waiting vehicle at this edge
329  std::vector<SUMOVehicle*> waitingTooFarAway;
330  for (std::vector<SUMOVehicle*>::const_iterator it = myWaiting[edge].begin(); it != myWaiting[edge].end(); ++it) {
331  const std::string& line = (*it)->getParameter().line == "" ? (*it)->getParameter().id : (*it)->getParameter().line;
332  SUMOReal vehiclePosition = (*it)->getPositionOnLane();
333  // if the line of the vehicle is contained in the set of given lines and the vehicle is stopped and is positioned
334  // in the interval [position - t, position + t] for a tolerance t=10
335  if (lines.count(line)) {
336  if ((position - 10 <= vehiclePosition) && (vehiclePosition <= position + 10)) {
337  return (*it);
338  } else if ((*it)->isStoppedTriggered() ||
339  (*it)->getParameter().departProcedure == DEPART_TRIGGERED) {
340  // maybe we are within the range of the stop
341  MSVehicle* veh = static_cast<MSVehicle*>(*it);
342  if (veh->isStoppedInRange(position)) {
343  return (*it);
344  } else {
345  waitingTooFarAway.push_back(*it);
346  }
347  }
348  }
349  }
350  for (std::vector<SUMOVehicle*>::iterator it = waitingTooFarAway.begin(); it != waitingTooFarAway.end(); ++it) {
351  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.");
352  }
353  }
354  return 0;
355 }
356 
357 
358 void
360  for (VehicleDictType::iterator i = myVehicleDict.begin(); i != myVehicleDict.end(); ++i) {
361  WRITE_WARNING("Vehicle " + i->first + " aborted waiting for a person or a container that will never come.");
362  }
363 }
364 
365 
366 unsigned int
368  frac = frac < 0 ? myScale : frac;
369  if (frac < 0 || frac == 1.) {
370  return 1;
371  }
372  // the vehicle in question has already been loaded, hence the '-1'
373  const unsigned int loaded = frac > 1. ? (unsigned int)(myLoadedVehNo / frac) : myLoadedVehNo - 1;
374  const unsigned int base = (unsigned int)frac;
375  const unsigned int resolution = 1000;
376  const unsigned int intFrac = (unsigned int)floor((frac - base) * resolution + 0.5);
377  // apply % twice to avoid integer overflow
378  if (((loaded % resolution) * intFrac) % resolution < intFrac) {
379  return base + 1;
380  }
381  return base;
382 }
383 
384 /****************************************************************************/
385 
The departure is person triggered.
The vehicle has departed (was inserted into the network)
Definition: MSNet.h:530
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.
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:80
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)
SUMOReal myMinDeceleration
The minimum deceleration capability for all vehicles in the network.
is a pedestrian
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 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:162
T MAX2(T a, T b)
Definition: StdDefs.h:79
VehicleDictType myVehicleDict
Dictionary of vehicles.
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.
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:65
A road/street connecting two junctions.
Definition: MSEdge.h:81
#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:536
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:65
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:845
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
T MIN2(T a, T b)
Definition: StdDefs.h:73
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.
SUMOReal getMaxDecel() const
Get the vehicle type&#39;s maximum deceleration [m/s^2].
Definition: MSCFModel.h:184
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:53
unsigned int myEndedVehNo
The number of removed vehicles.
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:528
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.
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:108
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:770
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
#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.
const int VTYPEPARS_VEHICLECLASS_SET
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 "frac" ...
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.
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle&#39;s type.