SUMO - Simulation of Urban MObility
MSTransportableControl.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
20 // Stores all persons in the net and handles their waiting for cars.
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 <vector>
34 #include <algorithm>
35 #include "MSNet.h"
36 #include "MSEdge.h"
38 #include "MSContainer.h"
39 #include "MSVehicle.h"
40 #include "MSTransportableControl.h"
44 
45 
46 // ===========================================================================
47 // method definitions
48 // ===========================================================================
50  myLoadedNumber(0),
51  myRunningNumber(0),
52  myJammedNumber(0),
53  myWaitingForVehicleNumber(0),
54  myHaveNewWaiting(false) {
55 }
56 
57 
59  for (std::map<std::string, MSTransportable*>::iterator i = myTransportables.begin(); i != myTransportables.end(); ++i) {
60  delete(*i).second;
61  }
62  myTransportables.clear();
63  myWaiting4Vehicle.clear();
64 }
65 
66 
67 bool
69  const SUMOVehicleParameter& param = transportable->getParameter();
70  if (myTransportables.find(param.id) == myTransportables.end()) {
71  myTransportables[param.id] = transportable;
72  const SUMOTime step = param.depart % DELTA_T == 0 ? param.depart : (param.depart / DELTA_T + 1) * DELTA_T;
73  myWaiting4Departure[step].push_back(transportable);
75  return true;
76  }
77  return false;
78 }
79 
80 
82 MSTransportableControl::get(const std::string& id) const {
83  std::map<std::string, MSTransportable*>::const_iterator i = myTransportables.find(id);
84  if (i == myTransportables.end()) {
85  return 0;
86  }
87  return (*i).second;
88 }
89 
90 
91 void
93  if (OptionsCont::getOptions().isSet("tripinfo-output")) {
94  transportable->tripInfoOutput(OutputDevice::getDeviceByOption("tripinfo-output"), transportable);
95  } else if (OptionsCont::getOptions().getBool("duration-log.statistics")) {
96  // collecting statistics is a sideffect
98  transportable->tripInfoOutput(dev, transportable);
99  }
100  if (OptionsCont::getOptions().isSet("vehroute-output")) {
101  transportable->routeOutput(OutputDevice::getDeviceByOption("vehroute-output"));
102  }
103  const std::map<std::string, MSTransportable*>::iterator i = myTransportables.find(transportable->getID());
104  if (i != myTransportables.end()) {
105  myRunningNumber--;
106  delete i->second;
107  myTransportables.erase(i);
108  }
109 }
110 
111 
112 void
114  const SUMOTime step = time % DELTA_T == 0 ? time : (time / DELTA_T + 1) * DELTA_T;
115  // avoid double registration
116  const TransportableVector& transportables = myWaiting4Departure[step];
117  if (std::find(transportables.begin(), transportables.end(), transportable) == transportables.end()) {
118  myWaitingUntil[step].push_back(transportable);
119  }
120 }
121 
122 
123 void
125  myHaveNewWaiting = false;
126  while (myWaiting4Departure.find(time) != myWaiting4Departure.end()) {
127  const TransportableVector& transportables = myWaiting4Departure[time];
128  // we cannot use an iterator here because there might be additions to the vector while proceeding
129  for (int i = 0; i < (int)transportables.size(); ++i) {
130  if (transportables[i]->proceed(net, time)) {
131  myRunningNumber++;
132  } else {
133  erase(transportables[i]);
134  }
135  }
136  myWaiting4Departure.erase(time);
137  }
138  while (myWaitingUntil.find(time) != myWaitingUntil.end()) {
139  const TransportableVector& transportables = myWaitingUntil[time];
140  // we cannot use an iterator here because there might be additions to the vector while proceeding
141  for (int i = 0; i < (int)transportables.size(); ++i) {
142  if (!transportables[i]->proceed(net, time)) {
143  erase(transportables[i]);
144  }
145  }
146  myWaitingUntil.erase(time);
147  }
148 }
149 
150 
151 void
152 MSTransportableControl::addWaiting(const MSEdge* const edge, MSTransportable* transportable) {
153  myWaiting4Vehicle[edge].push_back(transportable);
155  myHaveNewWaiting = true;
156 }
157 
158 
159 bool
161  bool ret = false;
162  if (myWaiting4Vehicle.find(edge) != myWaiting4Vehicle.end()) {
164  const std::string& line = vehicle->getParameter().line == "" ? vehicle->getParameter().id : vehicle->getParameter().line;
166  for (TransportableVector::iterator i = wait.begin(); i != wait.end();) {
167  if ((*i)->isWaitingFor(line) && vehicle->getVehicleType().getPersonCapacity() > vehicle->getPersonNumber() && stop->timeToBoardNextPerson <= currentTime && stop->pars.startPos <= (*i)->getEdgePos() && (*i)->getEdgePos() <= stop->pars.endPos) {
168  edge->removePerson(*i);
169  vehicle->addPerson(*i);
170  //if the time a person needs to enter the vehicle extends the duration of the stop of the vehicle extend
171  //the duration by setting it to the boarding duration of the person
172  const SUMOTime boardingDuration = vehicle->getVehicleType().getBoardingDuration();
173  if (boardingDuration >= stop->duration) {
174  stop->duration = boardingDuration;
175  }
176  //update the time point at which the next person can board the vehicle
177  if (stop->timeToBoardNextPerson > currentTime - DELTA_T) {
178  stop->timeToBoardNextPerson += boardingDuration;
179  } else {
180  stop->timeToBoardNextPerson = currentTime + boardingDuration;
181  }
182 
183  static_cast<MSTransportable::Stage_Driving*>((*i)->getCurrentStage())->setVehicle(vehicle);
184  i = wait.erase(i);
186  ret = true;
187  } else {
188  ++i;
189  }
190  }
191  if (wait.size() == 0) {
192  myWaiting4Vehicle.erase(myWaiting4Vehicle.find(edge));
193  }
194  }
195  return ret;
196 }
197 
198 
199 bool
201  bool ret = false;
202  if (myWaiting4Vehicle.find(edge) != myWaiting4Vehicle.end()) {
203  TransportableVector& waitContainers = myWaiting4Vehicle[edge];
204  for (TransportableVector::iterator i = waitContainers.begin(); i != waitContainers.end();) {
205  const std::string& line = vehicle->getParameter().line == "" ? vehicle->getParameter().id : vehicle->getParameter().line;
207  if ((*i)->isWaitingFor(line) && vehicle->getVehicleType().getContainerCapacity() > vehicle->getContainerNumber()
208  && stop->timeToLoadNextContainer <= currentTime
209  && stop->pars.startPos <= (*i)->getEdgePos() && (*i)->getEdgePos() <= stop->pars.endPos) {
210  edge->removeContainer(*i);
211  vehicle->addContainer(*i);
212  //if the time a container needs to get loaded on the vehicle extends the duration of the stop of the vehicle extend
213  //the duration by setting it to the loading duration of the container
214  const SUMOTime loadingDuration = vehicle->getVehicleType().getLoadingDuration();
215  if (loadingDuration >= stop->duration) {
216  stop->duration = loadingDuration;
217  }
218  //update the time point at which the next container can be loaded on the vehicle
219  stop->timeToLoadNextContainer = currentTime + loadingDuration;
220 
221  static_cast<MSContainer::MSContainerStage_Driving*>((*i)->getCurrentStage())->setVehicle(vehicle);
222  i = waitContainers.erase(i);
224  ret = true;
225  } else {
226  ++i;
227  }
228  }
229  if (waitContainers.size() == 0) {
230  myWaiting4Vehicle.erase(myWaiting4Vehicle.find(edge));
231  }
232  }
233  return ret;
234 }
235 
236 
237 bool
239  return !myTransportables.empty();
240 }
241 
242 
243 bool
246 }
247 
248 
249 void
251  for (std::map<const MSEdge*, TransportableVector>::const_iterator i = myWaiting4Vehicle.begin(); i != myWaiting4Vehicle.end(); ++i) {
252  const MSEdge* edge = (*i).first;
253  const TransportableVector& pv = (*i).second;
254  for (TransportableVector::const_iterator j = pv.begin(); j != pv.end(); ++j) {
255  MSTransportable* p = (*j);
256  p->setDeparted(MSNet::getInstance()->getCurrentTimeStep());
257  std::string transportableType;
258  if (dynamic_cast<MSPerson*>(p) != 0) {
259  edge->removePerson(p);
260  transportableType = "Person";
261  } else {
262  transportableType = "Container";
263  edge->removeContainer(p);
264  }
266  const std::string waitDescription = stage == 0 ? "waiting" : stage->getWaitingDescription();
267  WRITE_WARNING(transportableType + " '" + p->getID() + "' aborted " + waitDescription + ".");
268  erase(p);
269  }
270  }
271 }
272 
273 
274 void
276  for (std::map<SUMOTime, TransportableVector>::iterator it = myWaiting4Departure.begin(); it != myWaiting4Departure.end(); ++it) {
277  TransportableVector& ts = it->second;
278  TransportableVector::iterator it2 = std::find(ts.begin(), ts.end(), t);
279  if (it2 != ts.end()) {
280  ts.erase(it2);
281  }
282  }
283  for (std::map<SUMOTime, TransportableVector>::iterator it = myWaiting4Departure.begin(); it != myWaiting4Departure.end(); ++it) {
284  TransportableVector& ts = it->second;
285  TransportableVector::iterator it2 = std::find(ts.begin(), ts.end(), t);
286  if (it2 != ts.end()) {
287  ts.erase(it2);
288  }
289  }
290 }
291 
292 
295  std::mt19937* rng) const {
296  const double speedFactor = vtype->computeChosenSpeedDeviation(rng);
297  return new MSPerson(pars, vtype, plan, speedFactor);
298 }
299 
300 
303  return new MSContainer(pars, vtype, plan);
304 }
305 
306 /****************************************************************************/
void abortWaiting(MSTransportable *t)
aborts waiting stage of transportable
SUMOTime getBoardingDuration() const
Get this vehicle type&#39;s boarding duration.
void addWaiting(const MSEdge *edge, MSTransportable *person)
adds a transportable to the list of transportables waiting for a vehicle on the specified edge ...
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:83
SUMOTime getLoadingDuration() const
Get this vehicle type&#39;s loading duration.
void addContainer(MSTransportable *container)
Adds a container.
Definition: MSVehicle.cpp:4201
const SUMOVehicleParameter & getParameter() const
const SUMOVehicleParameter::Stop pars
The stop parameter.
Definition: MSVehicle.h:917
SUMOTime duration
The stopping duration.
Definition: MSVehicle.h:919
std::map< SUMOTime, TransportableVector > myWaiting4Departure
Transportables waiting for departure.
void setWaitEnd(SUMOTime time, MSTransportable *transportable)
sets the arrival time for a waiting transportable
virtual ~MSTransportableControl()
Destructor.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
std::map< std::string, MSTransportable * > myTransportables
all currently created transportables by id
SUMOTime DELTA_T
Definition: SUMOTime.cpp:39
const std::string & getID() const
returns the id of the transportable
MSTransportable::Stage * getCurrentStage() const
Return the current stage.
SUMOTime timeToBoardNextPerson
The time at which the vehicle is able to board another person.
Definition: MSVehicle.h:931
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:253
virtual MSTransportable * buildPerson(const SUMOVehicleParameter *pars, MSVehicleType *vtype, MSTransportable::MSTransportablePlan *plan, std::mt19937 *rng) const
Builds a new person.
std::map< const MSEdge *, TransportableVector > myWaiting4Vehicle
the lists of waiting transportables
virtual void removeContainer(MSTransportable *container) const
Remove container from myContainers.
Definition: MSEdge.h:612
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:199
The simulated network and simulation perfomer.
Definition: MSNet.h:90
int myLoadedNumber
The number of build transportables.
The car-following model and parameter.
Definition: MSVehicleType.h:72
virtual void erase(MSTransportable *transportable)
removes a single transportable
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:64
void abortWaitingForVehicle()
aborts the plan for any transportable that is still waiting for a ride
int getPersonCapacity() const
Get this vehicle type&#39;s person capacity.
double computeChosenSpeedDeviation(std::mt19937 *rng, const double minDev=-1.) const
Computes and returns the speed deviation.
int getContainerNumber() const
Returns the number of containers.
Definition: MSVehicle.cpp:4253
virtual void tripInfoOutput(OutputDevice &os, MSTransportable *transportable) const =0
Called on writing tripinfo output.
A road/street connecting two junctions.
Definition: MSEdge.h:80
std::vector< MSTransportable::Stage * > MSTransportablePlan
the structure holding the plan of a transportable
bool hasNonWaiting() const
checks whether any transportable is still engaged in walking / stopping
virtual void removePerson(MSTransportable *p) const
Definition: MSEdge.h:599
void setDeparted(SUMOTime now)
logs depart time of the current stage
MSTransportable * get(const std::string &id) const
Returns the named transportable, if existing.
double startPos
The stopping position start.
bool myHaveNewWaiting
whether a new transportable waiting for a vehicle has been added in the last step ...
SUMOTime depart
The vehicle&#39;s departure time.
virtual void routeOutput(OutputDevice &os) const =0
Called on writing vehroute output.
bool loadAnyWaiting(MSEdge *edge, MSVehicle *vehicle, MSVehicle::Stop *stop)
load any applicable containers Loads any container that is waiting on that edge for the given vehicle...
virtual MSTransportable * buildContainer(const SUMOVehicleParameter *pars, MSVehicleType *vtype, MSTransportable::MSTransportablePlan *plan) const
Builds a new container.
double endPos
The stopping position end.
int getPersonNumber() const
Returns the number of persons.
Definition: MSVehicle.cpp:4247
std::string line
The vehicle&#39;s line (mainly for public transport)
int myWaitingForVehicleNumber
The number of transportables waiting for vehicles.
bool boardAnyWaiting(MSEdge *edge, MSVehicle *vehicle, MSVehicle::Stop *stop)
board any applicable persons Boards any people who wait on that edge for the given vehicle and remove...
bool hasTransportables() const
checks whether any transportable waits to finish her plan
Structure representing possible vehicle parameter.
static OutputDevice & getDeviceByOption(const std::string &name)
Returns the device described by the option.
std::string getWaitingDescription() const
Return where the person waits and for what.
bool add(MSTransportable *transportable)
Adds a single transportable, returns false if an id clash occured.
const MSVehicleType & getVehicleType() const
Returns the vehicle&#39;s type definition.
void checkWaiting(MSNet *net, const SUMOTime time)
checks whether any transportables waiting time is over
const SUMOVehicleParameter & getParameter() const
Returns the vehicle&#39;s parameter (including departure definition)
std::vector< MSTransportable * > TransportableVector
Definition of a list of transportables.
void addPerson(MSTransportable *person)
Adds a passenger.
Definition: MSVehicle.cpp:4186
long long int SUMOTime
Definition: TraCIDefs.h:51
int myRunningNumber
The number of transportables within the network (build and inserted but not removed) ...
int getContainerCapacity() const
Get this vehicle type&#39;s container capacity.
std::map< SUMOTime, TransportableVector > myWaitingUntil
the lists of walking / stopping transportables
SUMOTime timeToLoadNextContainer
The time at which the vehicle is able to load another container.
Definition: MSVehicle.h:933
An output device that encapsulates an ofstream.
std::string id
The vehicle&#39;s id.
Definition of vehicle stop (position and duration)
Definition: MSVehicle.h:901