SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MSInsertionControl.cpp
Go to the documentation of this file.
1 /****************************************************************************/
11 // Inserts vehicles into the network when their departure time is reached
12 /****************************************************************************/
13 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
14 // Copyright (C) 2001-2014 DLR (http://www.dlr.de/) and contributors
15 /****************************************************************************/
16 //
17 // This file is part of SUMO.
18 // SUMO is free software: you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation, either version 3 of the License, or
21 // (at your option) any later version.
22 //
23 /****************************************************************************/
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <iostream>
36 #include <algorithm>
37 #include <cassert>
38 #include <iterator>
40 #include "MSGlobals.h"
41 #include "MSInsertionControl.h"
42 #include "MSVehicle.h"
43 #include "MSLane.h"
44 #include "MSEdge.h"
45 #include "MSRouteHandler.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  SUMOTime maxDepartDelay,
57  bool checkEdgesOnce)
58  : myVehicleControl(vc), myMaxDepartDelay(maxDepartDelay),
59  myCheckEdgesOnce(checkEdgesOnce) {}
60 
61 
63  for (std::vector<Flow>::iterator i = myFlows.begin(); i != myFlows.end(); ++i) {
64  delete(i->pars);
65  }
66 }
67 
68 
69 void
71  myAllVeh.add(veh);
72 }
73 
74 
75 void
77  Flow flow;
78  flow.pars = pars;
82  flow.index = 0;
83  if (!flow.isVolatile) {
85  if (dist != 0) {
86  const std::vector<const MSRoute*>& routes = dist->getVals();
87  const MSEdge* e = 0;
88  for (std::vector<const MSRoute*>::const_iterator i = routes.begin(); i != routes.end(); ++i) {
89  if (e == 0) {
90  e = (*i)->getEdges()[0];
91  } else {
92  if (e != (*i)->getEdges()[0]) {
93  flow.isVolatile = true;
94  break;
95  }
96  }
97  }
98  }
99  }
100  flow.vehicle = 0;
101  myFlows.push_back(flow);
102 }
103 
104 
105 unsigned int
107  // check whether any vehicles shall be emitted within this time step
108  const bool havePreChecked = MSDevice_Routing::isEnabled();
109  if (myPendingEmits.empty() || (havePreChecked && myEmitCandidates.empty())) {
110  return 0;
111  }
112  unsigned int numEmitted = 0;
113  // we use buffering for the refused emits to save time
114  // for this, we have two lists; one contains previously refused emits, the second
115  // will be used to append those vehicles that will not be able to depart in this
116  // time step
118 
119  // go through the list of previously refused vehicles, first
120  MSVehicleContainer::VehicleVector::const_iterator veh;
121  for (veh = myPendingEmits.begin(); veh != myPendingEmits.end(); veh++) {
122  if (havePreChecked && (myEmitCandidates.count(*veh) == 0)) {
123  refusedEmits.push_back(*veh);
124  } else {
125  numEmitted += tryInsert(time, *veh, refusedEmits);
126  }
127  }
128  myEmitCandidates.clear();
129  myPendingEmits = refusedEmits;
130  return numEmitted;
131 }
132 
133 
134 unsigned int
136  MSVehicleContainer::VehicleVector& refusedEmits) {
137  assert(veh->getParameter().depart < time + DELTA_T);
138  const MSEdge& edge = *veh->getEdge();
139  if ((!myCheckEdgesOnce || edge.getLastFailedInsertionTime() != time) && edge.insertVehicle(*veh, time)) {
140  // Successful emission.
141  checkFlowWait(veh);
142  veh->onDepart();
143  return 1;
144  }
145  if (myMaxDepartDelay >= 0 && time - veh->getParameter().depart > myMaxDepartDelay) {
146  // remove vehicles waiting too long for departure
147  checkFlowWait(veh);
148  myVehicleControl.deleteVehicle(veh, true);
149  } else if (edge.isVaporizing()) {
150  // remove vehicles if the edge shall be empty
151  checkFlowWait(veh);
152  myVehicleControl.deleteVehicle(veh, true);
153  } else if (myAbortedEmits.count(veh) > 0) {
154  // remove vehicles which shall not be inserted for some reason
155  myAbortedEmits.erase(veh);
156  checkFlowWait(veh);
157  myVehicleControl.deleteVehicle(veh, true);
158  } else {
159  // let the vehicle wait one step, we'll retry then
160  refusedEmits.push_back(veh);
161  }
162  edge.setLastFailedInsertionTime(time);
163  return 0;
164 }
165 
166 
167 void
169  for (std::vector<Flow>::iterator i = myFlows.begin(); i != myFlows.end(); ++i) {
170  if (i->vehicle == veh) {
171  i->vehicle = 0;
172  break;
173  }
174  }
175 }
176 
177 
178 void
179 MSInsertionControl::checkCandidates(SUMOTime time, const bool preCheck) {
180  while (myAllVeh.anyWaitingBefore(time + DELTA_T)) {
182  copy(top.begin(), top.end(), back_inserter(myPendingEmits));
183  myAllVeh.pop();
184  }
185  if (preCheck) {
186  MSVehicleContainer::VehicleVector::const_iterator veh;
187  for (veh = myPendingEmits.begin(); veh != myPendingEmits.end(); veh++) {
188  SUMOVehicle* const v = *veh;
189  const MSEdge* const edge = v->getEdge();
190  if ((!myCheckEdgesOnce || edge->getLastFailedInsertionTime() != time) && edge->insertVehicle(*v, time, true)) {
191  myEmitCandidates.insert(v);
192  } else {
193  MSDevice_Routing* dev = static_cast<MSDevice_Routing*>(v->getDevice(typeid(MSDevice_Routing)));
194  if (dev != 0) {
195  dev->skipRouting(time);
196  }
197  }
198  }
199  }
200 }
201 
202 
203 void
206  for (std::vector<Flow>::iterator i = myFlows.begin(); i != myFlows.end();) {
207  SUMOVehicleParameter* pars = i->pars;
208  if (!i->isVolatile && i->vehicle != 0 && pars->repetitionProbability < 0) {
209  ++i;
210  //std::cout << SIMTIME << " volatile=" << i->isVolatile << " veh=" << i->vehicle << "\n";
211  continue;
212  }
213  bool tryEmitByProb = pars->repetitionProbability > 0;
214  while ((pars->repetitionProbability < 0
215  && pars->repetitionsDone < pars->repetitionNumber
216  && pars->depart + pars->repetitionsDone * pars->repetitionOffset < time + DELTA_T)
217  || (tryEmitByProb
218  && pars->depart < time + DELTA_T
219  && pars->repetitionEnd > time
220  // only call rand if all other conditions are met
221  && RandHelper::rand() < (pars->repetitionProbability * TS))
222  ) {
223  tryEmitByProb = false; // only emit one per step
224  SUMOVehicleParameter* newPars = new SUMOVehicleParameter(*pars);
225  newPars->id = pars->id + "." + toString(i->index);
226  newPars->depart = static_cast<SUMOTime>(pars->depart + pars->repetitionsDone * pars->repetitionOffset);
227  pars->repetitionsDone++;
228  // try to build the vehicle
229  if (vehControl.getVehicle(newPars->id) == 0) {
230  const MSRoute* route = MSRoute::dictionary(pars->routeid);
231  const MSVehicleType* vtype = vehControl.getVType(pars->vtypeid, MSRouteHandler::getParsingRNG());
232  i->vehicle = vehControl.buildVehicle(newPars, route, vtype, false);
233  unsigned int quota = vehControl.getQuota();
234  if (quota > 0) {
235  vehControl.addVehicle(newPars->id, i->vehicle);
236  add(i->vehicle);
237  i->index++;
238  while (--quota > 0) {
239  SUMOVehicleParameter* quotaPars = new SUMOVehicleParameter(*pars);
240  quotaPars->id = pars->id + "." + toString(i->index);
241  quotaPars->depart = static_cast<SUMOTime>(pars->depart + pars->repetitionsDone * pars->repetitionOffset);
242  i->vehicle = vehControl.buildVehicle(quotaPars, route, vtype, false);
243  vehControl.addVehicle(quotaPars->id, i->vehicle);
244  add(i->vehicle);
245  i->index++;
246  }
247  } else {
248  vehControl.deleteVehicle(i->vehicle, true);
249  i->vehicle = 0;
250  }
251  } else {
252  // strange: another vehicle with the same id already exists
254  break;
255  }
256  throw ProcessError("Another vehicle with the id '" + newPars->id + "' exists.");
257  }
258  }
259  if (pars->repetitionsDone == pars->repetitionNumber || (pars->repetitionProbability > 0 && pars->repetitionEnd <= time)) {
260  i = myFlows.erase(i);
262  delete pars;
263  } else {
264  ++i;
265  }
266  }
268 }
269 
270 
271 unsigned int
273  return (unsigned int)myPendingEmits.size();
274 }
275 
276 
277 int
279  return (int)myFlows.size();
280 }
281 
282 
283 void
285  myAbortedEmits.insert(veh);
286 }
287 
288 void
290  //clear out the refused vehicle list, deleting the vehicles entirely
291  MSVehicleContainer::VehicleVector::iterator veh;
292  for (veh = myPendingEmits.begin(); veh != myPendingEmits.end();) {
293  if ((*veh)->getRoute().getID() == route || route == "") {
294  myVehicleControl.deleteVehicle(*veh, true);
295  veh = myPendingEmits.erase(veh);
296  } else {
297  ++veh;
298  }
299  }
300 }
301 
302 
303 /****************************************************************************/
304 
SUMOTime repetitionEnd
The time at which the flow ends (only needed when using repetitionProbability)
SUMOReal repetitionProbability
The probability for emitting a vehicle per second.
int getPendingFlowCount() const
Returns the number of flows that are still active.
void checkFlowWait(SUMOVehicle *veh)
Checks whether any flow is blocked due to this vehicle and clears the block.
virtual void deleteVehicle(SUMOVehicle *v, bool discard=false)
Deletes the vehicle.
void descheduleDeparture(SUMOVehicle *veh)
stops trying to emit the given vehicle
bool insertVehicle(SUMOVehicle &v, SUMOTime time, const bool checkOnly=false) const
Tries to insert the given vehicle into the network.
Definition: MSEdge.cpp:365
int repetitionNumber
The number of times the vehicle shall be repeatedly inserted.
std::set< SUMOVehicle * > myAbortedEmits
Set of vehicles which shall not be inserted anymore.
std::string vtypeid
The vehicle&#39;s type id.
virtual void onDepart()=0
Called when the vehicle is inserted into the network.
void checkCandidates(SUMOTime time, const bool preCheck)
Adds all vehicles that should have been emitted earlier to the refuse container.
DepartLaneDefinition departLaneProcedure
Information how the vehicle shall choose the lane to depart from.
virtual const MSEdge * getEdge() const =0
Returns the edge the vehicle is currently at.
static MTRand * getParsingRNG()
static bool gStateLoaded
Information whether a state has been loaded.
Definition: MSGlobals.h:86
static SUMOReal rand()
Returns a random real number in [0, 1)
Definition: RandHelper.h:62
A device that performs vehicle rerouting based on current edge speeds.
int repetitionsDone
The number of times the vehicle was already inserted.
SUMOVehicle * vehicle
The last created vehicle.
unsigned int index
the running index
MSVehicleContainer myAllVeh
All loaded vehicles sorted by their departure time.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:156
virtual bool addVehicle(const std::string &id, SUMOVehicle *v)
Tries to insert the vehicle into the internal vehicle container.
The lane is chosen randomly.
static RandomDistributor< const MSRoute * > * distDictionary(const std::string &id)
Returns the named route distribution.
Definition: MSRoute.cpp:150
void skipRouting(const SUMOTime currentTime)
Labels the current time step as &quot;unroutable&quot;.
#define TS
Definition: SUMOTime.h:52
SUMOReal repetitionOffset
The time offset between vehicle reinsertions.
The position is chosen randomly.
~MSInsertionControl()
Destructor.
The car-following model and parameter.
Definition: MSVehicleType.h:74
const std::vector< T > & getVals() const
Returns the members of the distribution.
std::vector< SUMOVehicle * > VehicleVector
definition of a list of vehicles which have the same departure time
A road/street connecting two junctions.
Definition: MSEdge.h:75
void pop()
Removes the uppermost vehicle vector.
unsigned int getWaitingVehicleNo() const
Returns the number of waiting vehicles.
std::string routeid
The vehicle&#39;s route id.
Representation of a vehicle.
Definition: SUMOVehicle.h:64
DepartPosDefinition departPosProcedure
Information how the vehicle shall choose the departure position.
bool myCheckEdgesOnce
Whether an edge on which a vehicle could not depart should be ignored in the same step...
MSVehicleContainer::VehicleVector myPendingEmits
Buffers for vehicles that could not be inserted.
unsigned int tryInsert(SUMOTime time, SUMOVehicle *veh, MSVehicleContainer::VehicleVector &refusedEmits)
Tries to emit the vehicle.
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:283
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.
SUMOTime myMaxDepartDelay
The maximum waiting time; vehicles waiting longer are deleted (-1: no deletion)
void clearPendingVehicles(std::string &route)
clears out all pending vehicles from a route, &quot;&quot; for all routes
std::set< SUMOVehicle * > myEmitCandidates
Buffer for vehicles that may be inserted in the current step.
void add(SUMOVehicle *veh)
Adds a single vehicle.
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:53
static bool isEnabled()
returns whether any routing actions take place
const VehicleVector & top()
Returns the uppermost vehicle vector.
bool isVolatile
whether it has route or vehicle type distribution
std::vector< Flow > myFlows
Container for periodical vehicle parameters.
Structure representing possible vehicle parameter.
virtual const SUMOVehicleParameter & getParameter() const =0
Returns the vehicle&#39;s parameter (including departure definition)
MSVehicleControl & myVehicleControl
The assigned vehicle control (needed for vehicle re-insertion and deletion)
MSInsertionControl(MSVehicleControl &vc, SUMOTime maxDepartDelay, bool checkEdgesOnce)
Constructor.
unsigned int emitVehicles(SUMOTime time)
Emits vehicles that want to depart at the given time.
int SUMOTime
Definition: SUMOTime.h:43
SUMOTime getLastFailedInsertionTime() const
Returns the last time a vehicle could not be inserted.
Definition: MSEdge.h:465
SUMOVehicle * getVehicle(const std::string &id) const
Returns the vehicle with the given id.
bool anyWaitingBefore(SUMOTime time) const
Returns the information whether any vehicles want to depart before the given time.
bool hasVTypeDistribution(const std::string &id) const
Asks for a vehicle type distribution.
virtual MSDevice * getDevice(const std::type_info &type) const =0
Returns a device of the given type if it exists or 0.
#define DELTA_T
Definition: SUMOTime.h:50
The class responsible for building and deletion of vehicles.
SUMOVehicleParameter * pars
The paramters.
MSVehicleType * getVType(const std::string &id=DEFAULT_VTYPE_ID, MTRand *rng=0)
Returns the named vehicle type or a sample from the named distribution.
void add(SUMOVehicle *veh)
Adds a single vehicle for departure.
static void checkDist(const std::string &id)
Checks the distribution whether it is permanent and deletes it if not.
Definition: MSRoute.cpp:173
unsigned int getQuota(SUMOReal frac=-1) const
Returns the number of instances of the current vehicle that shall be emitted considering that &quot;frac&quot; ...
void determineCandidates(SUMOTime time)
Checks for all vehicles whether they can be emitted.
std::string id
The vehicle&#39;s id.
static bool dictionary(const std::string &id, const MSRoute *route)
Adds a route to the dictionary.
Definition: MSRoute.cpp:116