Eclipse 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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 // Stores all persons in the net and handles their waiting for cars.
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <vector>
27 #include <algorithm>
28 #include "MSNet.h"
29 #include "MSEdge.h"
31 #include "MSContainer.h"
32 #include "MSVehicle.h"
33 #include "MSTransportableControl.h"
37 
38 
39 // ===========================================================================
40 // method definitions
41 // ===========================================================================
43  myLoadedNumber(0),
44  myRunningNumber(0),
45  myJammedNumber(0),
46  myWaitingForVehicleNumber(0),
47  myHaveNewWaiting(false) {
48 }
49 
50 
52  for (std::map<std::string, MSTransportable*>::iterator i = myTransportables.begin(); i != myTransportables.end(); ++i) {
53  delete (*i).second;
54  }
55  myTransportables.clear();
56  myWaiting4Vehicle.clear();
57 }
58 
59 
60 bool
62  const SUMOVehicleParameter& param = transportable->getParameter();
63  if (myTransportables.find(param.id) == myTransportables.end()) {
64  myTransportables[param.id] = transportable;
65  const SUMOTime step = param.depart % DELTA_T == 0 ? param.depart : (param.depart / DELTA_T + 1) * DELTA_T;
66  myWaiting4Departure[step].push_back(transportable);
68  return true;
69  }
70  return false;
71 }
72 
73 
75 MSTransportableControl::get(const std::string& id) const {
76  std::map<std::string, MSTransportable*>::const_iterator i = myTransportables.find(id);
77  if (i == myTransportables.end()) {
78  return nullptr;
79  }
80  return (*i).second;
81 }
82 
83 
84 void
86  if (OptionsCont::getOptions().isSet("tripinfo-output")) {
87  transportable->tripInfoOutput(OutputDevice::getDeviceByOption("tripinfo-output"));
88  } else if (OptionsCont::getOptions().getBool("duration-log.statistics")) {
89  // collecting statistics is a sideffect
91  transportable->tripInfoOutput(dev);
92  }
93  if (OptionsCont::getOptions().isSet("vehroute-output")) {
94  transportable->routeOutput(OutputDevice::getDeviceByOption("vehroute-output"), OptionsCont::getOptions().getBool("vehroute-output.route-length"));
95  }
96  const std::map<std::string, MSTransportable*>::iterator i = myTransportables.find(transportable->getID());
97  if (i != myTransportables.end()) {
99  delete i->second;
100  myTransportables.erase(i);
101  }
102 }
103 
104 
105 void
107  const SUMOTime step = time % DELTA_T == 0 ? time : (time / DELTA_T + 1) * DELTA_T;
108  // avoid double registration
109  const TransportableVector& transportables = myWaiting4Departure[step];
110  if (std::find(transportables.begin(), transportables.end(), transportable) == transportables.end()) {
111  myWaitingUntil[step].push_back(transportable);
112  }
113 }
114 
115 
116 void
118  myHaveNewWaiting = false;
119  while (myWaiting4Departure.find(time) != myWaiting4Departure.end()) {
120  const TransportableVector& transportables = myWaiting4Departure[time];
121  // we cannot use an iterator here because there might be additions to the vector while proceeding
122  for (int i = 0; i < (int)transportables.size(); ++i) {
123  if (transportables[i]->proceed(net, time)) {
124  myRunningNumber++;
125  } else {
126  erase(transportables[i]);
127  }
128  }
129  myWaiting4Departure.erase(time);
130  }
131  while (myWaitingUntil.find(time) != myWaitingUntil.end()) {
132  const TransportableVector& transportables = myWaitingUntil[time];
133  // we cannot use an iterator here because there might be additions to the vector while proceeding
134  for (int i = 0; i < (int)transportables.size(); ++i) {
135  if (!transportables[i]->proceed(net, time)) {
136  erase(transportables[i]);
137  }
138  }
139  myWaitingUntil.erase(time);
140  }
141 }
142 
143 
144 void
145 MSTransportableControl::addWaiting(const MSEdge* const edge, MSTransportable* transportable) {
146  myWaiting4Vehicle[edge].push_back(transportable);
148  myHaveNewWaiting = true;
149 }
150 
151 
152 bool
153 MSTransportableControl::boardAnyWaiting(MSEdge* edge, SUMOVehicle* vehicle, const SUMOVehicleParameter::Stop& stop, SUMOTime& timeToBoardNextPerson, SUMOTime& stopDuration) {
154  bool ret = false;
155  if (myWaiting4Vehicle.find(edge) != myWaiting4Vehicle.end()) {
158  for (TransportableVector::iterator i = wait.begin(); i != wait.end();) {
159  if ((*i)->isWaitingFor(vehicle)
160  && vehicle->getVehicleType().getPersonCapacity() > vehicle->getPersonNumber()
161  && timeToBoardNextPerson <= currentTime
162  && stop.startPos <= (*i)->getEdgePos()
163  && (*i)->getEdgePos() <= stop.endPos) {
164  edge->removePerson(*i);
165  vehicle->addPerson(*i);
166  if (timeToBoardNextPerson >= 0) { // meso does not have boarding times
167  //if the time a person needs to enter the vehicle extends the duration of the stop of the vehicle extend
168  //the duration by setting it to the boarding duration of the person
169  const SUMOTime boardingDuration = vehicle->getVehicleType().getBoardingDuration();
170  if (boardingDuration >= stopDuration) {
171  stopDuration = boardingDuration;
172  }
173  //update the time point at which the next person can board the vehicle
174  if (timeToBoardNextPerson > currentTime - DELTA_T) {
175  timeToBoardNextPerson += boardingDuration;
176  } else {
177  timeToBoardNextPerson = currentTime + boardingDuration;
178  }
179  }
180 
181  static_cast<MSTransportable::Stage_Driving*>((*i)->getCurrentStage())->setVehicle(vehicle);
182  i = wait.erase(i);
184  ret = true;
185  } else {
186  ++i;
187  }
188  }
189  if (wait.size() == 0) {
190  myWaiting4Vehicle.erase(myWaiting4Vehicle.find(edge));
191  }
192  }
193  return ret;
194 }
195 
196 
197 bool
198 MSTransportableControl::loadAnyWaiting(MSEdge* edge, SUMOVehicle* vehicle, const SUMOVehicleParameter::Stop& stop, SUMOTime& timeToLoadNextContainer, SUMOTime& stopDuration) {
199  bool ret = false;
200  if (myWaiting4Vehicle.find(edge) != myWaiting4Vehicle.end()) {
201  TransportableVector& waitContainers = myWaiting4Vehicle[edge];
202  for (TransportableVector::iterator i = waitContainers.begin(); i != waitContainers.end();) {
204  if ((*i)->isWaitingFor(vehicle)
205  && vehicle->getVehicleType().getContainerCapacity() > vehicle->getContainerNumber()
206  && timeToLoadNextContainer <= currentTime
207  && stop.startPos <= (*i)->getEdgePos()
208  && (*i)->getEdgePos() <= stop.endPos) {
209  edge->removeContainer(*i);
210  vehicle->addContainer(*i);
211  //if the time a container needs to get loaded on the vehicle extends the duration of the stop of the vehicle extend
212  //the duration by setting it to the loading duration of the container
213  const SUMOTime loadingDuration = vehicle->getVehicleType().getLoadingDuration();
214  if (loadingDuration >= stopDuration) {
215  stopDuration = loadingDuration;
216  }
217  //update the time point at which the next container can be loaded on the vehicle
218  timeToLoadNextContainer = currentTime + loadingDuration;
219 
220  static_cast<MSContainer::MSContainerStage_Driving*>((*i)->getCurrentStage())->setVehicle(vehicle);
221  i = waitContainers.erase(i);
223  ret = true;
224  } else {
225  ++i;
226  }
227  }
228  if (waitContainers.size() == 0) {
229  myWaiting4Vehicle.erase(myWaiting4Vehicle.find(edge));
230  }
231  }
232  return ret;
233 }
234 
235 
236 bool
238  return !myTransportables.empty();
239 }
240 
241 
242 bool
245 }
246 
247 
248 int
251 }
252 
253 
254 void
256  for (std::map<const MSEdge*, TransportableVector>::const_iterator i = myWaiting4Vehicle.begin(); i != myWaiting4Vehicle.end(); ++i) {
257  const MSEdge* edge = (*i).first;
258  const TransportableVector& pv = (*i).second;
259  for (TransportableVector::const_iterator j = pv.begin(); j != pv.end(); ++j) {
260  MSTransportable* p = (*j);
261  std::string transportableType;
262  if (dynamic_cast<MSPerson*>(p) != nullptr) {
263  edge->removePerson(p);
264  transportableType = "Person";
265  } else {
266  transportableType = "Container";
267  edge->removeContainer(p);
268  }
270  const std::string waitDescription = stage == nullptr ? "waiting" : stage->getWaitingDescription();
271  WRITE_WARNING(transportableType + " '" + p->getID() + "' aborted " + waitDescription + ".");
272  erase(p);
273  }
274  }
275 }
276 
277 void
279  const MSEdge* edge = t->getEdge();
280  auto it = myWaiting4Vehicle.find(edge);
281  if (it != myWaiting4Vehicle.end()) {
282  TransportableVector& waiting = it->second;
283  auto it2 = std::find(waiting.begin(), waiting.end(), t);
284  if (it2 != waiting.end()) {
285  waiting.erase(it2);
286  }
287  }
288 }
289 
290 void
292  for (std::map<SUMOTime, TransportableVector>::iterator it = myWaiting4Departure.begin(); it != myWaiting4Departure.end(); ++it) {
293  TransportableVector& ts = it->second;
294  TransportableVector::iterator it2 = std::find(ts.begin(), ts.end(), t);
295  if (it2 != ts.end()) {
296  ts.erase(it2);
297  }
298  }
299  for (std::map<SUMOTime, TransportableVector>::iterator it = myWaitingUntil.begin(); it != myWaitingUntil.end(); ++it) {
300  TransportableVector& ts = it->second;
301  TransportableVector::iterator it2 = std::find(ts.begin(), ts.end(), t);
302  if (it2 != ts.end()) {
303  ts.erase(it2);
304  }
305  }
306 }
307 
308 
311  std::mt19937* rng) const {
312  const double speedFactor = vtype->computeChosenSpeedDeviation(rng);
313  return new MSPerson(pars, vtype, plan, speedFactor);
314 }
315 
316 
319  return new MSContainer(pars, vtype, plan);
320 }
321 
322 
323 /****************************************************************************/
MSVehicleType
The car-following model and parameter.
Definition: MSVehicleType.h:65
MSVehicleType::getBoardingDuration
SUMOTime getBoardingDuration() const
Get this vehicle type's boarding duration.
Definition: MSVehicleType.h:299
MSTransportableControl::abortWaitingForVehicle
void abortWaitingForVehicle(MSTransportable *t)
let the given transportable abort waiting for a vehicle (when removing stage via TraCI)
Definition: MSTransportableControl.cpp:278
MSTransportableControl::boardAnyWaiting
bool boardAnyWaiting(MSEdge *edge, SUMOVehicle *vehicle, const SUMOVehicleParameter::Stop &stop, SUMOTime &timeToBoardNextPerson, SUMOTime &stopDuration)
board any applicable persons Boards any people who wait on that edge for the given vehicle and remove...
Definition: MSTransportableControl.cpp:153
MSTransportableControl::getActiveCount
int getActiveCount()
return the number of active transportable objects
Definition: MSTransportableControl.cpp:249
WRITE_WARNING
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:275
SUMOVehicle::addPerson
virtual void addPerson(MSTransportable *person)=0
Adds a person to this vehicle.
MSNet.h
OutputDevice_String
An output device that encapsulates an ofstream.
Definition: OutputDevice_String.h:39
DELTA_T
SUMOTime DELTA_T
Definition: SUMOTime.cpp:36
OptionsCont.h
MSTransportable::Stage_Driving
Definition: MSTransportable.h:435
SUMOTrafficObject::getVehicleType
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle's type.
MSTransportableControl::erase
virtual void erase(MSTransportable *transportable)
removes a single transportable
Definition: MSTransportableControl.cpp:85
MSNet
The simulated network and simulation perfomer.
Definition: MSNet.h:91
SUMOVehicle::getContainerNumber
virtual int getContainerNumber() const =0
Returns the number of containers.
MSPerson
Definition: MSPerson.h:63
MSTransportableControl::~MSTransportableControl
virtual ~MSTransportableControl()
Destructor.
Definition: MSTransportableControl.cpp:51
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:34
SUMOVehicle
Representation of a vehicle.
Definition: SUMOVehicle.h:60
MSTransportableControl::myWaiting4Vehicle
std::map< const MSEdge *, TransportableVector > myWaiting4Vehicle
the lists of waiting transportables
Definition: MSTransportableControl.h:218
OptionsCont::getOptions
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:57
SUMOVehicleParameter
Structure representing possible vehicle parameter.
Definition: SUMOVehicleParameter.h:297
MSEdge.h
MSTransportable
Definition: MSTransportable.h:58
MSTransportableControl::add
bool add(MSTransportable *transportable)
Adds a single transportable, returns false if an id clash occurred.
Definition: MSTransportableControl.cpp:61
MSVehicleType::getPersonCapacity
int getPersonCapacity() const
Get this vehicle type's person capacity.
Definition: MSVehicleType.h:284
MSTransportableControl::setWaitEnd
void setWaitEnd(SUMOTime time, MSTransportable *transportable)
sets the arrival time for a waiting transportable
Definition: MSTransportableControl.cpp:106
SUMOVehicleParameter::depart
SUMOTime depart
Definition: SUMOVehicleParameter.h:482
MSTransportableControl::loadAnyWaiting
bool loadAnyWaiting(MSEdge *edge, SUMOVehicle *vehicle, const SUMOVehicleParameter::Stop &stop, SUMOTime &timeToLoadNextContainer, SUMOTime &stopDuration)
load any applicable containers Loads any container that is waiting on that edge for the given vehicle...
Definition: MSTransportableControl.cpp:198
MSVehicle.h
MSTransportableControl::hasNonWaiting
bool hasNonWaiting() const
checks whether any transportable is still engaged in walking / stopping
Definition: MSTransportableControl.cpp:243
MSTransportableControl::get
MSTransportable * get(const std::string &id) const
Returns the named transportable, if existing.
Definition: MSTransportableControl.cpp:75
MSTransportableControl::myWaiting4Departure
std::map< SUMOTime, TransportableVector > myWaiting4Departure
Transportables waiting for departure.
Definition: MSTransportableControl.h:212
MSTransportable::getEdge
const MSEdge * getEdge() const
Returns the current edge.
Definition: MSTransportable.h:627
MSTransportableControl::myRunningNumber
int myRunningNumber
The number of transportables within the network (build and inserted but not removed)
Definition: MSTransportableControl.h:224
MSVehicleType::computeChosenSpeedDeviation
double computeChosenSpeedDeviation(std::mt19937 *rng, const double minDev=-1.) const
Computes and returns the speed deviation.
Definition: MSVehicleType.cpp:82
MSTransportable::getCurrentStage
MSTransportable::Stage * getCurrentStage() const
Return the current stage.
Definition: MSTransportable.h:677
MSTransportableControl.h
MSNet::getCurrentTimeStep
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:283
MSContainer
Definition: MSContainer.h:60
MSTransportableControl::buildPerson
virtual MSTransportable * buildPerson(const SUMOVehicleParameter *pars, MSVehicleType *vtype, MSTransportable::MSTransportablePlan *plan, std::mt19937 *rng) const
Builds a new person.
Definition: MSTransportableControl.cpp:310
SUMOVehicleParameter::id
std::string id
The vehicle's id.
Definition: SUMOVehicleParameter.h:468
MSTransportableControl::hasTransportables
bool hasTransportables() const
checks whether any transportable waits to finish her plan
Definition: MSTransportableControl.cpp:237
OutputDevice.h
MSTransportableControl::abortAnyWaitingForVehicle
void abortAnyWaitingForVehicle()
aborts the plan for any transportable that is still waiting for a ride
Definition: MSTransportableControl.cpp:255
MSEdge
A road/street connecting two junctions.
Definition: MSEdge.h:78
SUMOVehicleParameter::Stop::endPos
double endPos
The stopping position end.
Definition: SUMOVehicleParameter.h:604
MSTransportableControl::myLoadedNumber
int myLoadedNumber
The number of build transportables.
Definition: MSTransportableControl.h:221
MSContainer::MSContainerStage_Driving
Definition: MSContainer.h:69
MSContainer.h
MSTransportableControl::myWaitingUntil
std::map< SUMOTime, TransportableVector > myWaitingUntil
the lists of walking / stopping transportables
Definition: MSTransportableControl.h:215
MSEdge::removeContainer
virtual void removeContainer(MSTransportable *container) const
Remove container from myContainers.
Definition: MSEdge.h:633
MSPerson.h
MSTransportable::tripInfoOutput
virtual void tripInfoOutput(OutputDevice &os) const =0
Called on writing tripinfo output.
MSEdge::removePerson
virtual void removePerson(MSTransportable *p) const
Definition: MSEdge.h:620
SUMOVehicleParameter::Stop::startPos
double startPos
The stopping position start.
Definition: SUMOVehicleParameter.h:601
SUMOVehicle::addContainer
virtual void addContainer(MSTransportable *container)=0
Adds a container to this vehicle.
MSTransportableControl::myHaveNewWaiting
bool myHaveNewWaiting
whether a new transportable waiting for a vehicle has been added in the last step
Definition: MSTransportableControl.h:233
MSTransportableControl::TransportableVector
std::vector< MSTransportable * > TransportableVector
Definition of a list of transportables.
Definition: MSTransportableControl.h:54
MSTransportableControl::myWaitingForVehicleNumber
int myWaitingForVehicleNumber
The number of transportables waiting for vehicles.
Definition: MSTransportableControl.h:230
MSTransportable::MSTransportablePlan
std::vector< MSTransportable::Stage * > MSTransportablePlan
the structure holding the plan of a transportable
Definition: MSTransportable.h:587
MSNet::getInstance
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
MSTransportable::routeOutput
virtual void routeOutput(OutputDevice &os, const bool withRouteLength) const =0
Called on writing vehroute output.
MSTransportableControl::MSTransportableControl
MSTransportableControl()
Constructor.
Definition: MSTransportableControl.cpp:42
MSTransportableControl::addWaiting
void addWaiting(const MSEdge *edge, MSTransportable *person)
adds a transportable to the list of transportables waiting for a vehicle on the specified edge
Definition: MSTransportableControl.cpp:145
OutputDevice_String.h
MSTransportableControl::checkWaiting
void checkWaiting(MSNet *net, const SUMOTime time)
checks whether any transportables waiting time is over
Definition: MSTransportableControl.cpp:117
MSTransportable::getID
const std::string & getID() const
returns the id of the transportable
Definition: MSTransportable.cpp:699
SUMOVehicle::getPersonNumber
virtual int getPersonNumber() const =0
Returns the number of persons.
config.h
MSVehicleType::getContainerCapacity
int getContainerCapacity() const
Get this vehicle type's container capacity.
Definition: MSVehicleType.h:292
MSTransportableControl::myTransportables
std::map< std::string, MSTransportable * > myTransportables
all currently created transportables by id
Definition: MSTransportableControl.h:209
MSTransportable::Stage_Driving::getWaitingDescription
std::string getWaitingDescription() const
Return where the person waits and for what.
Definition: MSTransportable.cpp:642
MSTransportableControl::abortWaiting
void abortWaiting(MSTransportable *t)
aborts waiting stage of transportable
Definition: MSTransportableControl.cpp:291
MSTransportableControl::buildContainer
virtual MSTransportable * buildContainer(const SUMOVehicleParameter *pars, MSVehicleType *vtype, MSTransportable::MSTransportablePlan *plan) const
Builds a new container.
Definition: MSTransportableControl.cpp:318
MSTransportable::getParameter
const SUMOVehicleParameter & getParameter() const
Definition: MSTransportable.h:602
OutputDevice::getDeviceByOption
static OutputDevice & getDeviceByOption(const std::string &name)
Returns the device described by the option.
Definition: OutputDevice.cpp:116
SUMOVehicleParameter::Stop
Definition of vehicle stop (position and duration)
Definition: SUMOVehicleParameter.h:572
MSVehicleType::getLoadingDuration
SUMOTime getLoadingDuration() const
Get this vehicle type's loading duration.
Definition: MSVehicleType.h:306