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-2017 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 
48 // ===========================================================================
49 // member method definitions
50 // ===========================================================================
52  myLoadedVehNo(0),
53  myRunningVehNo(0),
54  myEndedVehNo(0),
55  myDiscarded(0),
56  myCollisions(0),
57  myTeleportsJam(0),
58  myTeleportsYield(0),
59  myTeleportsWrongLane(0),
60  myEmergencyStops(0),
61  myTotalDepartureDelay(0),
62  myTotalTravelTime(0),
63  myDefaultVTypeMayBeDeleted(true),
64  myDefaultPedTypeMayBeDeleted(true),
65  myWaitingForPerson(0),
66  myWaitingForContainer(0),
67  myMaxSpeedFactor(1),
68  myMinDeceleration(SUMOVTypeParameter::getDefaultDecel(SVC_IGNORING)) {
75  myScale = oc.getFloat("scale");
76  myMaxRandomDepartOffset = string2time(oc.getString("random-depart-offset"));
77 }
78 
79 
81  // delete vehicles
82  for (VehicleDictType::iterator i = myVehicleDict.begin(); i != myVehicleDict.end(); ++i) {
83  delete(*i).second;
84  }
85  myVehicleDict.clear();
86  // delete vehicle type distributions
87  for (VTypeDistDictType::iterator i = myVTypeDistDict.begin(); i != myVTypeDistDict.end(); ++i) {
88  delete(*i).second;
89  }
90  myVTypeDistDict.clear();
91  // delete vehicle types
92  for (VTypeDictType::iterator i = myVTypeDict.begin(); i != myVTypeDict.end(); ++i) {
93  //delete(*i).second;
94  }
95  myVTypeDict.clear();
96 }
97 
100  if (myMaxRandomDepartOffset > 0) {
101  // round to the closest usable simulation step
102  return DELTA_T * int((MSRouteHandler::getParsingRNG()->rand((int)myMaxRandomDepartOffset) + 0.5 * DELTA_T) / DELTA_T);
103  } else {
104  return 0;
105  }
106 }
107 
110  const MSRoute* route,
111  const MSVehicleType* type,
112  const bool ignoreStopErrors, const bool fromRouteFile) {
113  myLoadedVehNo++;
114  if (fromRouteFile) {
116  }
117  MSVehicle* built = new MSVehicle(defs, route, type, type->computeChosenSpeedDeviation(fromRouteFile ? MSRouteHandler::getParsingRNG() : 0));
118  built->addStops(ignoreStopErrors);
120  return built;
121 }
122 
123 
124 void
126  assert(myRunningVehNo > 0);
127  myTotalTravelTime += STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep() - veh->getDeparture());
128  myRunningVehNo--;
130  for (std::vector<MSDevice*>::const_iterator i = veh->getDevices().begin(); i != veh->getDevices().end(); ++i) {
131  (*i)->generateOutput();
132  }
133  if (OptionsCont::getOptions().isSet("tripinfo-output")) {
134  // close tag after tripinfo (possibly including emissions from another device) have been written
135  OutputDevice::getDeviceByOption("tripinfo-output").closeTag();
136  }
137  deleteVehicle(veh);
138 }
139 
140 
141 void
143  ++myRunningVehNo;
147  myMinDeceleration = MIN2(myMinDeceleration, v.getVehicleType().getCarFollowModel().getMaxDecel());
148 }
149 
150 
151 void
152 MSVehicleControl::setState(int runningVehNo, int loadedVehNo, int endedVehNo, double totalDepartureDelay, double totalTravelTime) {
153  myRunningVehNo = runningVehNo;
154  myLoadedVehNo = loadedVehNo;
155  myEndedVehNo = endedVehNo;
156  myTotalDepartureDelay = totalDepartureDelay;
157  myTotalTravelTime = totalTravelTime;
158 }
159 
160 
161 void
163  out.openTag(SUMO_TAG_DELAY);
169  // save vehicle types
170  for (VTypeDictType::iterator it = myVTypeDict.begin(); it != myVTypeDict.end(); ++it) {
171  it->second->getParameter().write(out);
172  }
173  for (VTypeDistDictType::iterator it = myVTypeDistDict.begin(); it != myVTypeDistDict.end(); ++it) {
175  out.writeAttr(SUMO_ATTR_VTYPES, (*it).second->getVals());
176  out.writeAttr(SUMO_ATTR_PROBS, (*it).second->getProbs());
177  out.closeTag();
178  }
179  for (VehicleDictType::iterator it = myVehicleDict.begin(); it != myVehicleDict.end(); ++it) {
180  (*it).second->saveState(out);
181  }
182 }
183 
184 
185 bool
186 MSVehicleControl::addVehicle(const std::string& id, SUMOVehicle* v) {
187  VehicleDictType::iterator it = myVehicleDict.find(id);
188  if (it == myVehicleDict.end()) {
189  // id not in myVehicleDict.
190  myVehicleDict[id] = v;
191  return true;
192  }
193  return false;
194 }
195 
196 
198 MSVehicleControl::getVehicle(const std::string& id) const {
199  VehicleDictType::const_iterator it = myVehicleDict.find(id);
200  if (it == myVehicleDict.end()) {
201  return 0;
202  }
203  return it->second;
204 }
205 
206 
207 void
209  myEndedVehNo++;
210  if (discard) {
211  myDiscarded++;
212  }
213  if (veh != 0) {
214  myVehicleDict.erase(veh->getID());
215  }
216  delete veh;
217 }
218 
219 
220 bool
221 MSVehicleControl::checkVType(const std::string& id) {
222  if (id == DEFAULT_VTYPE_ID) {
224  delete myVTypeDict[id];
225  myVTypeDict.erase(myVTypeDict.find(id));
227  } else {
228  return false;
229  }
230  } else if (id == DEFAULT_PEDTYPE_ID) {
232  delete myVTypeDict[id];
233  myVTypeDict.erase(myVTypeDict.find(id));
235  } else {
236  return false;
237  }
238  } else {
239  if (myVTypeDict.find(id) != myVTypeDict.end() || myVTypeDistDict.find(id) != myVTypeDistDict.end()) {
240  return false;
241  }
242  }
243  return true;
244 }
245 
246 bool
248  if (checkVType(vehType->getID())) {
249  myVTypeDict[vehType->getID()] = vehType;
250  return true;
251  }
252  return false;
253 }
254 
255 
256 void
258  assert(vehType != 0);
259  assert(myVTypeDict.find(vehType->getID()) != myVTypeDict.end());
260  myVTypeDict.erase(vehType->getID());
261  delete vehType;
262 }
263 
264 
265 bool
266 MSVehicleControl::addVTypeDistribution(const std::string& id, RandomDistributor<MSVehicleType*>* vehTypeDistribution) {
267  if (checkVType(id)) {
268  myVTypeDistDict[id] = vehTypeDistribution;
269  return true;
270  }
271  return false;
272 }
273 
274 
275 bool
276 MSVehicleControl::hasVTypeDistribution(const std::string& id) const {
277  return myVTypeDistDict.find(id) != myVTypeDistDict.end();
278 }
279 
280 
282 MSVehicleControl::getVType(const std::string& id, MTRand* rng) {
283  VTypeDictType::iterator it = myVTypeDict.find(id);
284  if (it == myVTypeDict.end()) {
285  VTypeDistDictType::iterator it2 = myVTypeDistDict.find(id);
286  if (it2 == myVTypeDistDict.end()) {
287  return 0;
288  }
289  return it2->second->get(rng);
290  }
291  if (id == DEFAULT_VTYPE_ID) {
293  } else if (id == DEFAULT_PEDTYPE_ID) {
295  }
296  return it->second;
297 }
298 
299 
300 void
301 MSVehicleControl::insertVTypeIDs(std::vector<std::string>& into) const {
302  into.reserve(into.size() + myVTypeDict.size() + myVTypeDistDict.size());
303  for (VTypeDictType::const_iterator i = myVTypeDict.begin(); i != myVTypeDict.end(); ++i) {
304  into.push_back((*i).first);
305  }
306  for (VTypeDistDictType::const_iterator i = myVTypeDistDict.begin(); i != myVTypeDistDict.end(); ++i) {
307  into.push_back((*i).first);
308  }
309 }
310 
311 
312 void
313 MSVehicleControl::addWaiting(const MSEdge* const edge, SUMOVehicle* vehicle) {
314  if (myWaiting.find(edge) == myWaiting.end()) {
315  myWaiting[edge] = std::vector<SUMOVehicle*>();
316  }
317  myWaiting[edge].push_back(vehicle);
318 }
319 
320 
321 void
322 MSVehicleControl::removeWaiting(const MSEdge* const edge, SUMOVehicle* vehicle) {
323  if (myWaiting.find(edge) != myWaiting.end()) {
324  std::vector<SUMOVehicle*>::iterator it = std::find(myWaiting[edge].begin(), myWaiting[edge].end(), vehicle);
325  if (it != myWaiting[edge].end()) {
326  myWaiting[edge].erase(it);
327  }
328  }
329 }
330 
331 
333 MSVehicleControl::getWaitingVehicle(const MSEdge* const edge, const std::set<std::string>& lines, const double position, const std::string ridingID) {
334  if (myWaiting.find(edge) != myWaiting.end()) {
335  // for every vehicle waiting vehicle at this edge
336  std::vector<SUMOVehicle*> waitingTooFarAway;
337  for (std::vector<SUMOVehicle*>::const_iterator it = myWaiting[edge].begin(); it != myWaiting[edge].end(); ++it) {
338  const std::string& line = (*it)->getParameter().line == "" ? (*it)->getParameter().id : (*it)->getParameter().line;
339  double vehiclePosition = (*it)->getPositionOnLane();
340  // if the line of the vehicle is contained in the set of given lines and the vehicle is stopped and is positioned
341  // in the interval [position - t, position + t] for a tolerance t=10
342  if (lines.count(line)) {
343  if ((position - 10 <= vehiclePosition) && (vehiclePosition <= position + 10)) {
344  return (*it);
345  } else if ((*it)->isStoppedTriggered() ||
346  (*it)->getParameter().departProcedure == DEPART_TRIGGERED) {
347  // maybe we are within the range of the stop
348  MSVehicle* veh = static_cast<MSVehicle*>(*it);
349  if (veh->isStoppedInRange(position)) {
350  return (*it);
351  } else {
352  waitingTooFarAway.push_back(*it);
353  }
354  }
355  }
356  }
357  for (std::vector<SUMOVehicle*>::iterator it = waitingTooFarAway.begin(); it != waitingTooFarAway.end(); ++it) {
358  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.");
359  }
360  }
361  return 0;
362 }
363 
364 
365 void
367  for (VehicleDictType::iterator i = myVehicleDict.begin(); i != myVehicleDict.end(); ++i) {
368  WRITE_WARNING("Vehicle " + i->first + " aborted waiting for a person or a container that will never come.");
369  }
370 }
371 
372 
373 int
374 MSVehicleControl::getQuota(double frac) const {
375  frac = frac < 0 ? myScale : frac;
376  if (frac < 0 || frac == 1.) {
377  return 1;
378  }
379  // the vehicle in question has already been loaded, hence the '-1'
380  const int loaded = frac > 1. ? (int)(myLoadedVehNo / frac) : myLoadedVehNo - 1;
381  const int base = (int)frac;
382  const int resolution = 1000;
383  const int intFrac = (int)floor((frac - base) * resolution + 0.5);
384  // apply % twice to avoid integer overflow
385  if (((loaded % resolution) * intFrac) % resolution < intFrac) {
386  return base + 1;
387  }
388  return base;
389 }
390 
391 int
394 }
395 
396 /****************************************************************************/
397 
The departure is person triggered.
The vehicle has departed (was inserted into the network)
Definition: MSNet.h:580
bool myDefaultPedTypeMayBeDeleted
Whether no pedestrian type was loaded.
bool checkVType(const std::string &id)
Checks whether the vehicle type (distribution) may be added.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:256
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:83
virtual void deleteVehicle(SUMOVehicle *v, bool discard=false)
Deletes the vehicle.
SUMOTime computeRandomDepartOffset() const
compute (optional) random offset to the departure time
int myTeleportsWrongLane
The number of teleports due to vehicles stuck on the wrong lane.
is a pedestrian
int myEndedVehNo
The number of removed vehicles.
static bool teleportOnCollision()
Definition: MSLane.h:1011
Represents a generic random distribution.
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
bool hasVTypeDistribution(const std::string &id) const
Asks for a vehicle type distribution.
static MTRand * getParsingRNG()
SUMOVehicle * getVehicle(const std::string &id) const
Returns the vehicle with the given id.
int getQuota(double frac=-1) const
Returns the number of instances of the current vehicle that shall be emitted considering that "frac" ...
MSVehicleControl()
Constructor.
weights: time range begin
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:158
T MAX2(T a, T b)
Definition: StdDefs.h:70
VehicleDictType myVehicleDict
Dictionary of vehicles.
SUMOTime DELTA_T
Definition: SUMOTime.cpp:40
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.
const std::string & getID() const
Returns the id.
Definition: Named.h:66
bool myDefaultVTypeMayBeDeleted
Whether no vehicle type was loaded.
double myMaxSpeedFactor
The maximum speed factor for all vehicles in the network.
const std::string DEFAULT_VTYPE_ID
void setState(int runningVehNo, int loadedVehNo, int endedVehNo, double totalDepartureDelay, double totalTravelTime)
Sets the current state variables as loaded from the stream.
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
bool isStoppedInRange(double pos) const
return whether the given position is within range of the current stop
Definition: MSVehicle.cpp:1139
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:65
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.
const MSCFModel & getCarFollowModel() const
Returns the vehicle type&#39;s car following model definition (const version)
The vehicle arrived at his destination (is deleted)
Definition: MSNet.h:586
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:56
Representation of a vehicle.
Definition: SUMOVehicle.h:67
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
void removeVType(const MSVehicleType *vehType)
double myTotalTravelTime
The aggregated time vehicles needed to aacomplish their route (in seconds)
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:47
T MIN2(T a, T b)
Definition: StdDefs.h:64
SUMOVehicle * getWaitingVehicle(const MSEdge *const edge, const std::set< std::string > &lines, const double position, const std::string ridingID)
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.
double getMaxDecel() const
Get the vehicle type&#39;s maximum deceleration [m/s^2].
Definition: MSCFModel.h:201
int myRunningVehNo
The number of vehicles within the network (build and inserted but not removed)
virtual double getChosenSpeedFactor() const =0
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
vehicle is a passenger car (a "normal" car)
void insertVTypeIDs(std::vector< std::string > &into) const
Inserts ids of all known vehicle types and vehicle type distributions to the given vector...
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:578
bool addVType(MSVehicleType *vehType)
Adds a vehicle type.
double myTotalDepartureDelay
The aggregated time vehicles had to wait for departure (in seconds)
int setParameter
Information for the router which parameter were set.
trigger: the time of the step
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)
weights: time range end
virtual ~MSVehicleControl()
Destructor.
A storage for options typed value containers)
Definition: OptionsCont.h:99
bool addVTypeDistribution(const std::string &id, RandomDistributor< MSVehicleType *> *vehTypeDistribution)
Adds a vehicle type distribution.
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:811
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
bool closeTag()
Closes the most recently opened tag.
long long int SUMOTime
Definition: TraCIDefs.h:52
double computeChosenSpeedDeviation(MTRand *rng, const double minDev=-1.) const
Computes and returns the speed deviation.
void vehicleDeparted(const SUMOVehicle &v)
Informs this control about a vehicle&#39;s departure.
double myScale
The scaling factor (especially for inc-dua)
double myMinDeceleration
The minimum deceleration capability for all vehicles in the network.
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
distribution of a vehicle type
int getTeleportCount() const
return the number of teleports (including collisions)
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.
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle&#39;s type.