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-2017 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"
45 
46 
47 // ===========================================================================
48 // method definitions
49 // ===========================================================================
51  myLoadedNumber(0),
52  myRunningNumber(0),
53  myJammedNumber(0),
54  myWaitingForVehicleNumber(0),
55  myHaveNewWaiting(false) {
56 }
57 
58 
60  for (std::map<std::string, MSTransportable*>::iterator i = myTransportables.begin(); i != myTransportables.end(); ++i) {
61  delete(*i).second;
62  }
63  myTransportables.clear();
64  myWaiting4Vehicle.clear();
65 }
66 
67 
68 bool
70  const SUMOVehicleParameter& param = transportable->getParameter();
71  if (myTransportables.find(param.id) == myTransportables.end()) {
72  myTransportables[param.id] = transportable;
73  const SUMOTime step = param.depart % DELTA_T == 0 ? param.depart : (param.depart / DELTA_T + 1) * DELTA_T;
74  myWaiting4Departure[step].push_back(transportable);
76  return true;
77  }
78  return false;
79 }
80 
81 
83 MSTransportableControl::get(const std::string& id) const {
84  std::map<std::string, MSTransportable*>::const_iterator i = myTransportables.find(id);
85  if (i == myTransportables.end()) {
86  return 0;
87  }
88  return (*i).second;
89 }
90 
91 
92 void
94  if (OptionsCont::getOptions().isSet("tripinfo-output")) {
95  transportable->tripInfoOutput(OutputDevice::getDeviceByOption("tripinfo-output"));
96  } else if (OptionsCont::getOptions().getBool("duration-log.statistics")) {
97  // collecting statistics is a sideffect
99  transportable->tripInfoOutput(dev);
100  }
101  if (OptionsCont::getOptions().isSet("vehroute-output")) {
102  transportable->routeOutput(OutputDevice::getDeviceByOption("vehroute-output"));
103  }
104  const std::map<std::string, MSTransportable*>::iterator i = myTransportables.find(transportable->getID());
105  if (i != myTransportables.end()) {
106  myRunningNumber--;
107  delete i->second;
108  myTransportables.erase(i);
109  }
110 }
111 
112 
113 void
115  const SUMOTime step = time % DELTA_T == 0 ? time : (time / DELTA_T + 1) * DELTA_T;
116  // avoid double registration
117  const TransportableVector& transportables = myWaiting4Departure[step];
118  if (std::find(transportables.begin(), transportables.end(), transportable) == transportables.end()) {
119  myWaitingUntil[step].push_back(transportable);
120  }
121 }
122 
123 
124 void
126  myHaveNewWaiting = false;
127  while (myWaiting4Departure.find(time) != myWaiting4Departure.end()) {
128  const TransportableVector& transportables = myWaiting4Departure[time];
129  // we cannot use an iterator here because there might be additions to the vector while proceeding
130  for (int i = 0; i < (int)transportables.size(); ++i) {
131  if (transportables[i]->proceed(net, time)) {
132  myRunningNumber++;
133  } else {
134  erase(transportables[i]);
135  }
136  }
137  myWaiting4Departure.erase(time);
138  }
139  while (myWaitingUntil.find(time) != myWaitingUntil.end()) {
140  const TransportableVector& transportables = myWaitingUntil[time];
141  // we cannot use an iterator here because there might be additions to the vector while proceeding
142  for (int i = 0; i < (int)transportables.size(); ++i) {
143  if (!transportables[i]->proceed(net, time)) {
144  erase(transportables[i]);
145  }
146  }
147  myWaitingUntil.erase(time);
148  }
149 }
150 
151 
152 void
153 MSTransportableControl::addWaiting(const MSEdge* const edge, MSTransportable* transportable) {
154  myWaiting4Vehicle[edge].push_back(transportable);
156  myHaveNewWaiting = true;
157 }
158 
159 
160 bool
162  bool ret = false;
163  if (myWaiting4Vehicle.find(edge) != myWaiting4Vehicle.end()) {
165  const std::string& line = vehicle->getParameter().line == "" ? vehicle->getParameter().id : vehicle->getParameter().line;
167  for (TransportableVector::iterator i = wait.begin(); i != wait.end();) {
168  if ((*i)->isWaitingFor(line) && vehicle->getVehicleType().getPersonCapacity() > vehicle->getPersonNumber() && stop->timeToBoardNextPerson <= currentTime && stop->startPos <= (*i)->getEdgePos() && (*i)->getEdgePos() <= stop->endPos) {
169  edge->removePerson(*i);
170  vehicle->addPerson(*i);
171  //if the time a person needs to enter the vehicle extends the duration of the stop of the vehicle extend
172  //the duration by setting it to the boarding duration of the person
173  const SUMOTime boardingDuration = vehicle->getVehicleType().getBoardingDuration();
174  if (boardingDuration >= stop->duration) {
175  stop->duration = boardingDuration;
176  }
177  //update the time point at which the next person can board the vehicle
178  if (stop->timeToBoardNextPerson > currentTime - DELTA_T) {
179  stop->timeToBoardNextPerson += boardingDuration;
180  } else {
181  stop->timeToBoardNextPerson = currentTime + boardingDuration;
182  }
183 
184  static_cast<MSTransportable::Stage_Driving*>((*i)->getCurrentStage())->setVehicle(vehicle);
185  i = wait.erase(i);
187  ret = true;
188  } else {
189  ++i;
190  }
191  }
192  if (wait.size() == 0) {
193  myWaiting4Vehicle.erase(myWaiting4Vehicle.find(edge));
194  }
195  }
196  return ret;
197 }
198 
199 
200 bool
202  bool ret = false;
203  if (myWaiting4Vehicle.find(edge) != myWaiting4Vehicle.end()) {
204  TransportableVector& waitContainers = myWaiting4Vehicle[edge];
205  for (TransportableVector::iterator i = waitContainers.begin(); i != waitContainers.end();) {
206  const std::string& line = vehicle->getParameter().line == "" ? vehicle->getParameter().id : vehicle->getParameter().line;
208  if ((*i)->isWaitingFor(line) && vehicle->getVehicleType().getContainerCapacity() > vehicle->getContainerNumber()
209  && stop->timeToLoadNextContainer <= currentTime
210  && stop->startPos <= (*i)->getEdgePos() && (*i)->getEdgePos() <= stop->endPos) {
211  edge->removeContainer(*i);
212  vehicle->addContainer(*i);
213  //if the time a container needs to get loaded on the vehicle extends the duration of the stop of the vehicle extend
214  //the duration by setting it to the loading duration of the container
215  const SUMOTime loadingDuration = vehicle->getVehicleType().getLoadingDuration();
216  if (loadingDuration >= stop->duration) {
217  stop->duration = loadingDuration;
218  }
219  //update the time point at which the next container can be loaded on the vehicle
220  stop->timeToLoadNextContainer = currentTime + loadingDuration;
221 
222  static_cast<MSContainer::MSContainerStage_Driving*>((*i)->getCurrentStage())->setVehicle(vehicle);
223  i = waitContainers.erase(i);
225  ret = true;
226  } else {
227  ++i;
228  }
229  }
230  if (waitContainers.size() == 0) {
231  myWaiting4Vehicle.erase(myWaiting4Vehicle.find(edge));
232  }
233  }
234  return ret;
235 }
236 
237 
238 bool
240  return !myTransportables.empty();
241 }
242 
243 
244 bool
247 }
248 
249 
250 void
252  for (std::map<const MSEdge*, TransportableVector>::const_iterator i = myWaiting4Vehicle.begin(); i != myWaiting4Vehicle.end(); ++i) {
253  const MSEdge* edge = (*i).first;
254  const TransportableVector& pv = (*i).second;
255  for (TransportableVector::const_iterator j = pv.begin(); j != pv.end(); ++j) {
256  MSTransportable* p = (*j);
257  if (dynamic_cast<MSPerson*>(p) != 0) {
258  edge->removePerson(p);
259  WRITE_WARNING("Person '" + p->getID() + "' aborted waiting for a ride that will never come.");
260  } else {
261  edge->removeContainer(p);
262  WRITE_WARNING("Container '" + p->getID() + "' aborted waiting for a transport that will never come.");
263  }
264  erase(p);
265  }
266  }
267 }
268 
269 
270 void
272  for (std::map<SUMOTime, TransportableVector>::iterator it = myWaiting4Departure.begin(); it != myWaiting4Departure.end(); ++it) {
273  TransportableVector& ts = it->second;
274  TransportableVector::iterator it2 = std::find(ts.begin(), ts.end(), t);
275  if (it2 != ts.end()) {
276  ts.erase(it2);
277  }
278  }
279  for (std::map<SUMOTime, TransportableVector>::iterator it = myWaiting4Departure.begin(); it != myWaiting4Departure.end(); ++it) {
280  TransportableVector& ts = it->second;
281  TransportableVector::iterator it2 = std::find(ts.begin(), ts.end(), t);
282  if (it2 != ts.end()) {
283  ts.erase(it2);
284  }
285  }
286 }
287 
288 
291  return new MSPerson(pars, vtype, plan);
292 }
293 
294 
297  return new MSContainer(pars, vtype, plan);
298 }
299 
300 /****************************************************************************/
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 timeToBoardNextPerson
The time at which the vehicle is able to board another person.
Definition: MSVehicle.h:854
SUMOTime getLoadingDuration() const
Get this vehicle type&#39;s loading duration.
void addContainer(MSTransportable *container)
Adds a container.
Definition: MSVehicle.cpp:3684
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:169
std::map< std::string, MSTransportable * > myTransportables
all currently created transportables by id
SUMOTime DELTA_T
Definition: SUMOTime.cpp:40
const std::string & getID() const
returns the id of the transportable
virtual MSTransportable * buildPerson(const SUMOVehicleParameter *pars, MSVehicleType *vtype, MSTransportable::MSTransportablePlan *plan) const
Builds a new person.
Definition of vehicle stop (position and duration)
Definition: MSVehicle.h:820
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:252
std::map< const MSEdge *, TransportableVector > myWaiting4Vehicle
the lists of waiting transportables
virtual void removeContainer(MSTransportable *container) const
Remove container from myContainers.
Definition: MSEdge.h:613
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
The simulated network and simulation perfomer.
Definition: MSNet.h:89
int myLoadedNumber
The number of build transportables.
The car-following model and parameter.
Definition: MSVehicleType.h:73
virtual void erase(MSTransportable *transportable)
removes a single transportable
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:65
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.
int getContainerNumber() const
Returns the number of containers.
Definition: MSVehicle.cpp:3740
A road/street connecting two junctions.
Definition: MSEdge.h:81
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:600
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:856
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.
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:838
virtual MSTransportable * buildContainer(const SUMOVehicleParameter *pars, MSVehicleType *vtype, MSTransportable::MSTransportablePlan *plan) const
Builds a new container.
int getPersonNumber() const
Returns the number of persons.
Definition: MSVehicle.cpp:3734
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.
virtual void tripInfoOutput(OutputDevice &os) const =0
Called on writing tripinfo output.
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.
void checkWaiting(MSNet *net, const SUMOTime time)
checks whether any transportables waiting time is over
virtual void routeOutput(OutputDevice &os) const =0
Called on writing vehroute output.
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:3662
long long int SUMOTime
Definition: TraCIDefs.h:52
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.
double endPos
The stopping position end.
Definition: MSVehicle.h:836
std::map< SUMOTime, TransportableVector > myWaitingUntil
the lists of walking / stopping transportables
An output device that encapsulates an ofstream.
double startPos
The stopping position start.
Definition: MSVehicle.h:834
std::string id
The vehicle&#39;s id.