SUMO - Simulation of Urban MObility
MSTransportableControl.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // Stores all persons in the net and handles their waiting for cars.
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2001-2016 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <vector>
35 #include <algorithm>
36 #include "MSNet.h"
37 #include "MSEdge.h"
39 #include "MSContainer.h"
40 #include "MSVehicle.h"
41 #include "MSTransportableControl.h"
44 
45 #ifdef CHECK_MEMORY_LEAKS
46 #include <foreign/nvwa/debug_new.h>
47 #endif // CHECK_MEMORY_LEAKS
48 
49 
50 // ===========================================================================
51 // method definitions
52 // ===========================================================================
54  myLoadedNumber(0),
55  myRunningNumber(0),
56  myJammedNumber(0),
57  myWaitingForVehicleNumber(0),
58  myHaveNewWaiting(false) {
59 }
60 
61 
63  for (std::map<std::string, MSTransportable*>::iterator i = myTransportables.begin(); i != myTransportables.end(); ++i) {
64  delete(*i).second;
65  }
66  myTransportables.clear();
67  myWaiting4Vehicle.clear();
68 }
69 
70 
71 bool
73  const SUMOVehicleParameter& param = transportable->getParameter();
74  if (myTransportables.find(param.id) == myTransportables.end()) {
75  myTransportables[param.id] = transportable;
76  const SUMOTime step = param.depart % DELTA_T == 0 ? param.depart : (param.depart / DELTA_T + 1) * DELTA_T;
77  myWaiting4Departure[step].push_back(transportable);
79  return true;
80  }
81  return false;
82 }
83 
84 
86 MSTransportableControl::get(const std::string& id) const {
87  std::map<std::string, MSTransportable*>::const_iterator i = myTransportables.find(id);
88  if (i == myTransportables.end()) {
89  return 0;
90  }
91  return (*i).second;
92 }
93 
94 
95 void
97  if (OptionsCont::getOptions().isSet("tripinfo-output")) {
98  transportable->tripInfoOutput(OutputDevice::getDeviceByOption("tripinfo-output"));
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  myWaitingUntil[step].push_back(transportable);
116 }
117 
118 
119 void
121  myHaveNewWaiting = false;
122  while (myWaiting4Departure.find(time) != myWaiting4Departure.end()) {
123  const TransportableVector& transportables = myWaiting4Departure[time];
124  // we cannot use an iterator here because there might be additions to the vector while proceeding
125  for (int i = 0; i < (int)transportables.size(); ++i) {
126  if (transportables[i]->proceed(net, time)) {
127  myRunningNumber++;
128  } else {
129  erase(transportables[i]);
130  }
131  }
132  myWaiting4Departure.erase(time);
133  }
134  while (myWaitingUntil.find(time) != myWaitingUntil.end()) {
135  const TransportableVector& transportables = myWaitingUntil[time];
136  // we cannot use an iterator here because there might be additions to the vector while proceeding
137  for (int i = 0; i < (int)transportables.size(); ++i) {
138  if (!transportables[i]->proceed(net, time)) {
139  erase(transportables[i]);
140  }
141  }
142  myWaitingUntil.erase(time);
143  }
144 }
145 
146 
147 void
148 MSTransportableControl::addWaiting(const MSEdge* const edge, MSTransportable* transportable) {
149  myWaiting4Vehicle[edge].push_back(transportable);
151  myHaveNewWaiting = true;
152 }
153 
154 
155 bool
157  bool ret = false;
158  if (myWaiting4Vehicle.find(edge) != myWaiting4Vehicle.end()) {
160  const std::string& line = vehicle->getParameter().line == "" ? vehicle->getParameter().id : vehicle->getParameter().line;
162  for (TransportableVector::iterator i = wait.begin(); i != wait.end();) {
163  if ((*i)->isWaitingFor(line) && vehicle->getVehicleType().getPersonCapacity() > vehicle->getPersonNumber() && stop->timeToBoardNextPerson <= currentTime && stop->startPos <= (*i)->getEdgePos() && (*i)->getEdgePos() <= stop->endPos) {
164  edge->removePerson(*i);
165  vehicle->addPerson(*i);
166  //if the time a person needs to enter the vehicle extends the duration of the stop of the vehicle extend
167  //the duration by setting it to the boarding duration of the person
168  const SUMOTime boardingDuration = vehicle->getVehicleType().getBoardingDuration();
169  if (boardingDuration >= stop->duration) {
170  stop->duration = boardingDuration;
171  }
172  //update the time point at which the next person can board the vehicle
173  if (stop->timeToBoardNextPerson > currentTime - DELTA_T) {
174  stop->timeToBoardNextPerson += boardingDuration;
175  } else {
176  stop->timeToBoardNextPerson = currentTime + boardingDuration;
177  }
178 
179  static_cast<MSTransportable::Stage_Driving*>((*i)->getCurrentStage())->setVehicle(vehicle);
180  i = wait.erase(i);
182  ret = true;
183  } else {
184  ++i;
185  }
186  }
187  if (wait.size() == 0) {
188  myWaiting4Vehicle.erase(myWaiting4Vehicle.find(edge));
189  }
190  }
191  return ret;
192 }
193 
194 
195 bool
197  bool ret = false;
198  if (myWaiting4Vehicle.find(edge) != myWaiting4Vehicle.end()) {
199  TransportableVector& waitContainers = myWaiting4Vehicle[edge];
200  for (TransportableVector::iterator i = waitContainers.begin(); i != waitContainers.end();) {
201  const std::string& line = vehicle->getParameter().line == "" ? vehicle->getParameter().id : vehicle->getParameter().line;
203  if ((*i)->isWaitingFor(line) && vehicle->getVehicleType().getContainerCapacity() > vehicle->getContainerNumber()
204  && stop->timeToLoadNextContainer <= currentTime
205  && stop->startPos <= (*i)->getEdgePos() && (*i)->getEdgePos() <= stop->endPos) {
206  edge->removeContainer(*i);
207  vehicle->addContainer(*i);
208  //if the time a container needs to get loaded on the vehicle extends the duration of the stop of the vehicle extend
209  //the duration by setting it to the loading duration of the container
210  const SUMOTime loadingDuration = vehicle->getVehicleType().getLoadingDuration();
211  if (loadingDuration >= stop->duration) {
212  stop->duration = loadingDuration;
213  }
214  //update the time point at which the next container can be loaded on the vehicle
215  stop->timeToLoadNextContainer = currentTime + loadingDuration;
216 
217  static_cast<MSContainer::MSContainerStage_Driving*>((*i)->getCurrentStage())->setVehicle(vehicle);
218  i = waitContainers.erase(i);
220  ret = true;
221  } else {
222  ++i;
223  }
224  }
225  if (waitContainers.size() == 0) {
226  myWaiting4Vehicle.erase(myWaiting4Vehicle.find(edge));
227  }
228  }
229  return ret;
230 }
231 
232 
233 bool
235  return !myTransportables.empty();
236 }
237 
238 
239 bool
242 }
243 
244 
245 void
247  for (std::map<const MSEdge*, TransportableVector>::const_iterator i = myWaiting4Vehicle.begin(); i != myWaiting4Vehicle.end(); ++i) {
248  const MSEdge* edge = (*i).first;
249  const TransportableVector& pv = (*i).second;
250  for (TransportableVector::const_iterator j = pv.begin(); j != pv.end(); ++j) {
251  MSTransportable* p = (*j);
252  if (dynamic_cast<MSPerson*>(p) != 0) {
253  edge->removePerson(p);
254  WRITE_WARNING("Person '" + p->getID() + "' aborted waiting for a ride that will never come.");
255  } else {
256  edge->removeContainer(p);
257  WRITE_WARNING("Container '" + p->getID() + "' aborted waiting for a transport that will never come.");
258  }
259  erase(p);
260  }
261  }
262 }
263 
264 
267  return new MSPerson(pars, vtype, plan);
268 }
269 
270 
273  return new MSContainer(pars, vtype, plan);
274 }
275 
276 /****************************************************************************/
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 ...
virtual MSTransportable * buildPerson(const SUMOVehicleParameter *pars, const MSVehicleType *vtype, MSTransportable::MSTransportablePlan *plan) const
Builds a new person.
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:81
long long int SUMOTime
Definition: SUMOTime.h:43
SUMOTime timeToBoardNextPerson
The time at which the vehicle is able to board another person.
Definition: MSVehicle.h:752
virtual void tripInfoOutput(OutputDevice &os) const =0
Called on writing tripinfo output.
SUMOTime getLoadingDuration() const
Get this vehicle type&#39;s loading duration.
void addContainer(MSTransportable *container)
Adds a container.
Definition: MSVehicle.cpp:2987
const SUMOVehicleParameter & getParameter() const
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:159
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
Definition of vehicle stop (position and duration)
Definition: MSVehicle.h:720
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:254
std::map< const MSEdge *, TransportableVector > myWaiting4Vehicle
the lists of waiting transportables
virtual void removeContainer(MSTransportable *container) const
Remove container from myContainers.
Definition: MSEdge.h:625
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
The simulated network and simulation perfomer.
Definition: MSNet.h:93
int myLoadedNumber
The number of build transportables.
The car-following model and parameter.
Definition: MSVehicleType.h:74
virtual void erase(MSTransportable *transportable)
removes a single transportable
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:69
int getPersonCapacity() const
Get this vehicle type&#39;s person capacity.
int getContainerNumber() const
Returns the number of containers.
Definition: MSVehicle.cpp:3033
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:612
MSTransportable * get(const std::string &id) const
Returns the named transportable, if existing.
SUMOTime timeToLoadNextContainer
The time at which the vehicle is able to load another container.
Definition: MSVehicle.h:754
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...
SUMOTime duration
The stopping duration.
Definition: MSVehicle.h:736
int getPersonNumber() const
Returns the number of persons.
Definition: MSVehicle.cpp:3027
std::string line
The vehicle&#39;s line (mainly for public transport)
virtual MSTransportable * buildContainer(const SUMOVehicleParameter *pars, const MSVehicleType *vtype, MSTransportable::MSTransportablePlan *plan) const
Builds a new container.
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.
void abortWaiting()
aborts the plan for any transportable that is still waiting for a ride
static OutputDevice & getDeviceByOption(const std::string &name)
Returns the device described by the option.
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.
Definition: MSBaseVehicle.h:97
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)
SUMOReal endPos
The stopping position end.
Definition: MSVehicle.h:734
std::vector< MSTransportable * > TransportableVector
Definition of a list of transportables.
void addPerson(MSTransportable *person)
Adds a passenger.
Definition: MSVehicle.cpp:2965
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
SUMOReal startPos
The stopping position start.
Definition: MSVehicle.h:732
std::string id
The vehicle&#39;s id.