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-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  myWaitingForPerson(0),
69  myWaitingForContainer(0),
70  myScale(-1),
71  myMaxSpeedFactor(1),
72  myMinDeceleration(SUMOVTypeParameter::getDefaultDecel(SVC_IGNORING)) {
79  if (oc.isSet("scale")) {
80  myScale = oc.getFloat("scale");
81  }
82  myMaxRandomDepartOffset = string2time(oc.getString("random-depart-offset"));
83 }
84 
85 
87  // delete vehicles
88  for (VehicleDictType::iterator i = myVehicleDict.begin(); i != myVehicleDict.end(); ++i) {
89  delete(*i).second;
90  }
91  myVehicleDict.clear();
92  // delete vehicle type distributions
93  for (VTypeDistDictType::iterator i = myVTypeDistDict.begin(); i != myVTypeDistDict.end(); ++i) {
94  delete(*i).second;
95  }
96  myVTypeDistDict.clear();
97  // delete vehicle types
98  for (VTypeDictType::iterator i = myVTypeDict.begin(); i != myVTypeDict.end(); ++i) {
99  delete(*i).second;
100  }
101  myVTypeDict.clear();
102 }
103 
104 SUMOTime
106  if (myMaxRandomDepartOffset > 0) {
107  // round to the closest usable simulation step
108  return DELTA_T * int((MSRouteHandler::getParsingRNG()->rand((int)myMaxRandomDepartOffset) + 0.5 * DELTA_T) / DELTA_T);
109  } else {
110  return 0;
111  }
112 }
113 
116  const MSRoute* route,
117  const MSVehicleType* type,
118  const bool ignoreStopErrors, const bool fromRouteFile) {
119  myLoadedVehNo++;
120  if (fromRouteFile) {
122  }
123  MSVehicle* built = new MSVehicle(defs, route, type, type->computeChosenSpeedDeviation(fromRouteFile ? MSRouteHandler::getParsingRNG() : 0));
124  built->addStops(ignoreStopErrors);
126  return built;
127 }
128 
129 
130 void
132  assert(myRunningVehNo > 0);
133  myTotalTravelTime += STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep() - veh->getDeparture());
134  myRunningVehNo--;
136  for (std::vector<MSDevice*>::const_iterator i = veh->getDevices().begin(); i != veh->getDevices().end(); ++i) {
137  (*i)->generateOutput();
138  }
139  if (OptionsCont::getOptions().isSet("tripinfo-output")) {
140  // close tag after tripinfo (possibly including emissions from another device) have been written
141  OutputDevice::getDeviceByOption("tripinfo-output").closeTag();
142  }
143  deleteVehicle(veh);
144 }
145 
146 
147 void
149  ++myRunningVehNo;
153  myMinDeceleration = MIN2(myMinDeceleration, v.getVehicleType().getCarFollowModel().getMaxDecel());
154 }
155 
156 
157 void
158 MSVehicleControl::setState(int runningVehNo, int endedVehNo, SUMOReal totalDepartureDelay, SUMOReal totalTravelTime) {
159  myRunningVehNo = runningVehNo;
160  myEndedVehNo = endedVehNo;
161  myTotalDepartureDelay = totalDepartureDelay;
162  myTotalTravelTime = totalTravelTime;
163 }
164 
165 
166 void
170  // save vehicle types
171  for (VTypeDictType::iterator it = myVTypeDict.begin(); it != myVTypeDict.end(); ++it) {
172  it->second->getParameter().write(out);
173  }
174  for (VTypeDistDictType::iterator it = myVTypeDistDict.begin(); it != myVTypeDistDict.end(); ++it) {
176  out.writeAttr(SUMO_ATTR_VTYPES, (*it).second->getVals());
177  out.writeAttr(SUMO_ATTR_PROBS, (*it).second->getProbs());
178  out.closeTag();
179  }
180  for (VehicleDictType::iterator it = myVehicleDict.begin(); it != myVehicleDict.end(); ++it) {
181  (*it).second->saveState(out);
182  }
183 }
184 
185 
186 bool
187 MSVehicleControl::addVehicle(const std::string& id, SUMOVehicle* v) {
188  VehicleDictType::iterator it = myVehicleDict.find(id);
189  if (it == myVehicleDict.end()) {
190  // id not in myVehicleDict.
191  myVehicleDict[id] = v;
192  return true;
193  }
194  return false;
195 }
196 
197 
199 MSVehicleControl::getVehicle(const std::string& id) const {
200  VehicleDictType::const_iterator it = myVehicleDict.find(id);
201  if (it == myVehicleDict.end()) {
202  return 0;
203  }
204  return it->second;
205 }
206 
207 
208 void
210  myEndedVehNo++;
211  if (discard) {
212  myDiscarded++;
213  }
214  if (veh != 0) {
215  myVehicleDict.erase(veh->getID());
216  }
217  delete veh;
218 }
219 
220 
221 bool
222 MSVehicleControl::checkVType(const std::string& id) {
223  if (id == DEFAULT_VTYPE_ID) {
225  delete myVTypeDict[id];
226  myVTypeDict.erase(myVTypeDict.find(id));
228  } else {
229  return false;
230  }
231  } else if (id == DEFAULT_PEDTYPE_ID) {
233  delete myVTypeDict[id];
234  myVTypeDict.erase(myVTypeDict.find(id));
236  } else {
237  return false;
238  }
239  } else {
240  if (myVTypeDict.find(id) != myVTypeDict.end() || myVTypeDistDict.find(id) != myVTypeDistDict.end()) {
241  return false;
242  }
243  }
244  return true;
245 }
246 
247 bool
249  if (checkVType(vehType->getID())) {
250  myVTypeDict[vehType->getID()] = vehType;
251  return true;
252  }
253  return false;
254 }
255 
256 
257 bool
258 MSVehicleControl::addVTypeDistribution(const std::string& id, RandomDistributor<MSVehicleType*>* vehTypeDistribution) {
259  if (checkVType(id)) {
260  myVTypeDistDict[id] = vehTypeDistribution;
261  return true;
262  }
263  return false;
264 }
265 
266 
267 bool
268 MSVehicleControl::hasVTypeDistribution(const std::string& id) const {
269  return myVTypeDistDict.find(id) != myVTypeDistDict.end();
270 }
271 
272 
274 MSVehicleControl::getVType(const std::string& id, MTRand* rng) {
275  VTypeDictType::iterator it = myVTypeDict.find(id);
276  if (it == myVTypeDict.end()) {
277  VTypeDistDictType::iterator it2 = myVTypeDistDict.find(id);
278  if (it2 == myVTypeDistDict.end()) {
279  return 0;
280  }
281  return it2->second->get(rng);
282  }
283  if (id == DEFAULT_VTYPE_ID) {
285  } else if (id == DEFAULT_PEDTYPE_ID) {
287  }
288  return it->second;
289 }
290 
291 
292 void
293 MSVehicleControl::insertVTypeIDs(std::vector<std::string>& into) const {
294  into.reserve(into.size() + myVTypeDict.size() + myVTypeDistDict.size());
295  for (VTypeDictType::const_iterator i = myVTypeDict.begin(); i != myVTypeDict.end(); ++i) {
296  into.push_back((*i).first);
297  }
298  for (VTypeDistDictType::const_iterator i = myVTypeDistDict.begin(); i != myVTypeDistDict.end(); ++i) {
299  into.push_back((*i).first);
300  }
301 }
302 
303 
304 void
305 MSVehicleControl::addWaiting(const MSEdge* const edge, SUMOVehicle* vehicle) {
306  if (myWaiting.find(edge) == myWaiting.end()) {
307  myWaiting[edge] = std::vector<SUMOVehicle*>();
308  }
309  myWaiting[edge].push_back(vehicle);
310 }
311 
312 
313 void
314 MSVehicleControl::removeWaiting(const MSEdge* const edge, SUMOVehicle* vehicle) {
315  if (myWaiting.find(edge) != myWaiting.end()) {
316  std::vector<SUMOVehicle*>::iterator it = std::find(myWaiting[edge].begin(), myWaiting[edge].end(), vehicle);
317  if (it != myWaiting[edge].end()) {
318  myWaiting[edge].erase(it);
319  }
320  }
321 }
322 
323 
325 MSVehicleControl::getWaitingVehicle(const MSEdge* const edge, const std::set<std::string>& lines, const SUMOReal position, const std::string ridingID) {
326  if (myWaiting.find(edge) != myWaiting.end()) {
327  // for every vehicle waiting vehicle at this edge
328  std::vector<SUMOVehicle*> waitingTooFarAway;
329  for (std::vector<SUMOVehicle*>::const_iterator it = myWaiting[edge].begin(); it != myWaiting[edge].end(); ++it) {
330  const std::string& line = (*it)->getParameter().line == "" ? (*it)->getParameter().id : (*it)->getParameter().line;
331  SUMOReal vehiclePosition = (*it)->getPositionOnLane();
332  // if the line of the vehicle is contained in the set of given lines and the vehicle is stopped and is positioned
333  // in the interval [position - t, position + t] for a tolerance t=10
334  if (lines.count(line)) {
335  if ((position - 10 <= vehiclePosition) && (vehiclePosition <= position + 10)) {
336  return (*it);
337  } else if ((*it)->isStoppedTriggered() ||
338  (*it)->getParameter().departProcedure == DEPART_TRIGGERED) {
339  // maybe we are within the range of the stop
340  MSVehicle* veh = static_cast<MSVehicle*>(*it);
341  if (veh->isStoppedInRange(position)) {
342  return (*it);
343  } else {
344  waitingTooFarAway.push_back(*it);
345  }
346  }
347  }
348  }
349  for (std::vector<SUMOVehicle*>::iterator it = waitingTooFarAway.begin(); it != waitingTooFarAway.end(); ++it) {
350  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.");
351  }
352  }
353  return 0;
354 }
355 
356 
357 void
359  for (VehicleDictType::iterator i = myVehicleDict.begin(); i != myVehicleDict.end(); ++i) {
360  WRITE_WARNING("Vehicle " + i->first + " aborted waiting for a person or a container that will never come.");
361  }
362 }
363 
364 
365 unsigned int
367  frac = frac < 0 ? myScale : frac;
368  if (frac < 0 || frac == 1.) {
369  return 1;
370  }
371  // the vehicle in question has already been loaded, hence the '-1'
372  const unsigned int loaded = frac > 1. ? (unsigned int)(myLoadedVehNo / frac) : myLoadedVehNo - 1;
373  const unsigned int base = (unsigned int)frac;
374  const unsigned int resolution = 1000;
375  const unsigned int intFrac = (unsigned int)floor((frac - base) * resolution + 0.5);
376  // apply % twice to avoid integer overflow
377  if (((loaded % resolution) * intFrac) % resolution < intFrac) {
378  return base + 1;
379  }
380  return base;
381 }
382 
383 /****************************************************************************/
384 
The departure is person triggered.
The vehicle has departed (was inserted into the network)
Definition: MSNet.h:487
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.
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:159
T MAX2(T a, T b)
Definition: StdDefs.h:74
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:67
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:60
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's devices.
The vehicle arrived at his destination (is deleted)
Definition: MSNet.h:493
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:825
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
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:48
T MIN2(T a, T b)
Definition: StdDefs.h:68
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'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'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:485
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.
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:108
const std::string & getID() const
Returns the name of the vehicle type.
int SUMOTime
Definition: SUMOTime.h:43
void informVehicleStateListener(const SUMOVehicle *const vehicle, VehicleState to)
Informs all added listeners about a vehicle's state change.
Definition: MSNet.cpp:732
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'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'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's type.