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