SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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-sim.org/
12 // Copyright (C) 2001-2013 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 "MSNet.h"
39 #include <utils/common/RGBColor.h>
44 
45 #ifdef CHECK_MEMORY_LEAKS
46 #include <foreign/nvwa/debug_new.h>
47 #endif // CHECK_MEMORY_LEAKS
48 
49 
50 // ===========================================================================
51 // static members
52 // ===========================================================================
54 
55 
56 // ===========================================================================
57 // member method definitions
58 // ===========================================================================
60  myLoadedVehNo(0),
61  myRunningVehNo(0),
62  myEndedVehNo(0),
63  myDiscarded(0),
64  myCollisions(0),
65  myTeleports(0),
66  myTotalDepartureDelay(0),
67  myTotalTravelTime(0),
68  myDefaultVTypeMayBeDeleted(true),
69  myWaitingForPerson(0),
70  myScale(-1) {
71  SUMOVTypeParameter defType;
74  if (oc.isSet("incremental-dua-step")) {
75  myScale = oc.getInt("incremental-dua-step") / static_cast<SUMOReal>(oc.getInt("incremental-dua-base"));
76  }
77  if (oc.isSet("scale")) {
78  myScale = oc.getFloat("scale");
79  }
80 }
81 
82 
84  // delete vehicles
85  for (VehicleDictType::iterator i = myVehicleDict.begin(); i != myVehicleDict.end(); ++i) {
86  delete(*i).second;
87  }
88  myVehicleDict.clear();
89  // delete vehicle type distributions
90  for (VTypeDistDictType::iterator i = myVTypeDistDict.begin(); i != myVTypeDistDict.end(); ++i) {
91  delete(*i).second;
92  }
93  myVTypeDistDict.clear();
94  // delete vehicle types
95  for (VTypeDictType::iterator i = myVTypeDict.begin(); i != myVTypeDict.end(); ++i) {
96  delete(*i).second;
97  }
98  myVTypeDict.clear();
99 }
100 
101 
104  const MSRoute* route,
105  const MSVehicleType* type) {
106  myLoadedVehNo++;
107  MSVehicle* built = new MSVehicle(defs, route, type, type->computeChosenSpeedDeviation(myVehicleParamsRNG), myLoadedVehNo - 1);
109  return built;
110 }
111 
112 
113 void
115  assert(myRunningVehNo > 0);
116  for (std::vector<MSDevice*>::const_iterator i = veh->getDevices().begin(); i != veh->getDevices().end(); ++i) {
117  (*i)->generateOutput();
118  }
119  if (OptionsCont::getOptions().isSet("tripinfo-output")) {
120  OutputDevice::getDeviceByOption("tripinfo-output").closeTag();
121  }
122  myTotalTravelTime += STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep() - veh->getDeparture());
123  myRunningVehNo--;
125  deleteVehicle(veh);
126 }
127 
128 
129 void
131  if (getDepartedVehicleNo() == 0) {
132  od << -1.;
133  } else {
135  }
136 }
137 
138 
139 void
141  if (myEndedVehNo == 0) {
142  od << -1.;
143  } else {
145  }
146 }
147 
148 
149 void
151  ++myRunningVehNo;
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  myVehicleDict.erase(veh->getID());
215  delete veh;
216 }
217 
218 
221  return myVehicleDict.begin();
222 }
223 
224 
227  return myVehicleDict.end();
228 }
229 
230 
231 bool
232 MSVehicleControl::checkVType(const std::string& id) {
233  if (id == DEFAULT_VTYPE_ID) {
235  delete myVTypeDict[id];
236  myVTypeDict.erase(myVTypeDict.find(id));
238  } else {
239  return false;
240  }
241  } else {
242  if (myVTypeDict.find(id) != myVTypeDict.end() || myVTypeDistDict.find(id) != myVTypeDistDict.end()) {
243  return false;
244  }
245  }
246  return true;
247 }
248 
249 bool
251  if (checkVType(vehType->getID())) {
252  myVTypeDict[vehType->getID()] = vehType;
253  return true;
254  }
255  return false;
256 }
257 
258 
259 bool
260 MSVehicleControl::addVTypeDistribution(const std::string& id, RandomDistributor<MSVehicleType*>* vehTypeDistribution) {
261  if (checkVType(id)) {
262  myVTypeDistDict[id] = vehTypeDistribution;
263  return true;
264  }
265  return false;
266 }
267 
268 
269 bool
270 MSVehicleControl::hasVTypeDistribution(const std::string& id) const {
271  return myVTypeDistDict.find(id) != myVTypeDistDict.end();
272 }
273 
274 
276 MSVehicleControl::getVType(const std::string& id) {
277  VTypeDictType::iterator it = myVTypeDict.find(id);
278  if (it == myVTypeDict.end()) {
279  VTypeDistDictType::iterator it2 = myVTypeDistDict.find(id);
280  if (it2 == myVTypeDistDict.end()) {
281  return 0;
282  }
283  return it2->second->get(&myVehicleParamsRNG);
284  }
285  if (id == DEFAULT_VTYPE_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) {
326  if (myWaiting.find(edge) != myWaiting.end()) {
327  for (std::vector<SUMOVehicle*>::const_iterator it = myWaiting[edge].begin(); it != myWaiting[edge].end(); ++it) {
328  const std::string& line = (*it)->getParameter().line == "" ? (*it)->getParameter().id : (*it)->getParameter().line;
329  if (lines.count(line)) {
330  return (*it);
331  }
332  }
333  }
334  return 0;
335 }
336 
337 
338 void
340  for (VehicleDictType::iterator i = myVehicleDict.begin(); i != myVehicleDict.end(); ++i) {
341  WRITE_WARNING("Vehicle " + i->first + " aborted waiting for a person that will never come.");
342  }
343 }
344 
345 
346 bool
348  frac = frac < 0 ? myScale : frac;
349  if (frac < 0) {
350  return true;
351  }
352  const unsigned int resolution = 1000;
353  const unsigned int intFrac = (unsigned int)floor(frac * resolution + 0.5);
354  // the vehicle in question has already been loaded, hence the '-1'
355  // apply % twice to avoid integer overflow
356  return (((myLoadedVehNo - 1) % resolution) * intFrac) % resolution < intFrac;
357 }
358 
359 /****************************************************************************/
360 
The vehicle has departed (was inserted into the network)
Definition: MSNet.h:409
bool checkVType(const std::string &id)
Checks whether the vehicle type (distribution) may be added.
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:254
void addWaiting(const MSEdge *const edge, SUMOVehicle *vehicle)
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:77
virtual void deleteVehicle(SUMOVehicle *v, bool discard=false)
Deletes the vehicle.
SUMOReal myTotalDepartureDelay
The aggregated time vehicles had to wait for departure (in seconds)
unsigned int getDepartedVehicleNo() const
Returns the number of inserted vehicles.
constVehIt loadedVehBegin() const
Returns the begin of the internal vehicle map.
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
SUMOReal computeChosenSpeedDeviation(MTRand &rng) const
Computes and returns the speed deviation.
MSVehicleType * getVType(const std::string &id=DEFAULT_VTYPE_ID)
Returns the named vehicle type or a sample from the named distribution.
MSVehicleControl()
Constructor.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:150
VehicleDictType myVehicleDict
Dictionary of vehicles.
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)
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.
constVehIt loadedVehEnd() const
Returns the end of the internal vehicle map.
const std::string DEFAULT_VTYPE_ID
void printMeanTravelTime(OutputDevice &od) const
Returns the mean travel time of vehicles The mean travel time of ended vehicles (-1 if no vehicle has...
void printMeanWaitingTime(OutputDevice &od) const
Prints the mean waiting time of vehicles. The mean time vehicles had to wait for being inserted (-1 i...
static MSVehicleType * build(SUMOVTypeParameter &from)
Builds the microsim vehicle type described by the given parameter.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:196
The car-following model and parameter.
Definition: MSVehicleType.h:74
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:67
virtual SUMOVehicle * buildVehicle(SUMOVehicleParameter *defs, const MSRoute *route, const MSVehicleType *type)
Builds a vehicle, increases the number of built vehicles.
SUMOVehicle * getWaitingVehicle(const MSEdge *const edge, const std::set< std::string > &lines)
A road/street connecting two junctions.
Definition: MSEdge.h:73
#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:415
void setState(int runningVehNo, int endedVehNo, SUMOReal totalDepartureDelay, SUMOReal totalTravelTime)
Sets the current state variables as loaded from the stream.
Representation of a vehicle.
Definition: SUMOVehicle.h:63
SUMOReal myScale
The scaling factor (especially for inc-dua)
SUMOTime depart
The vehicle&#39;s departure time.
#define STEPS2TIME(x)
Definition: SUMOTime.h:65
void saveState(OutputDevice &out)
Saves the current state into the given stream.
static MTRand myVehicleParamsRNG
A random number generator used to choose from vtype/route distributions and computing the speed facto...
unsigned int myEndedVehNo
The number of removed vehicles.
std::string line
The vehicle&#39;s line (mainly for public transport)
void abortWaiting()
removes any vehicles that are still waiting
The vehicle was built, but has not yet departed.
Definition: MSNet.h:407
bool addVType(MSVehicleType *vehType)
Adds a vehicle type.
void scheduleVehicleRemoval(SUMOVehicle *veh)
Removes a vehicle after it has ended.
Structure representing possible vehicle parameter.
VTypeDistDictType myVTypeDistDict
A distribution of vehicle types (probability-&gt;vehicle type)
virtual SUMOTime getDeparture() const =0
Returns this vehicle&#39;s real departure time.
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.
std::map< std::string, SUMOVehicle * >::const_iterator constVehIt
Definition of the internal vehicles map iterator.
void informVehicleStateListener(const SUMOVehicle *const vehicle, VehicleState to)
Informs all added listeners about a vehicle&#39;s state change.
Definition: MSNet.cpp:668
SUMOVehicle * getVehicle(const std::string &id) const
Returns the vehicle with the given id.
bool isInQuota(SUMOReal frac=-1) const
Returns the information whether the currently vehicle number shall be emitted considering that only f...
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:70
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:221
void vehicleDeparted(const SUMOVehicle &v)
Informs this control about a vehicle&#39;s departure.
unsigned int myLoadedVehNo
The number of build vehicles.
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)
virtual const std::string & getID() const =0
Get the vehicle&#39;s ID.
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.