SUMO - Simulation of Urban MObility
MSContainerControl.cpp
Go to the documentation of this file.
1 /****************************************************************************/
8 // Stores all containers in the net and handles their waiting for cars.
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
11 // Copyright (C) 2001-2015 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <vector>
33 //#include <algorithm>
34 #include "MSNet.h"
35 #include "MSEdge.h"
36 #include "MSContainer.h"
37 //#include "MSVehicle.h"
38 #include "MSContainerControl.h"
41 
42 #ifdef CHECK_MEMORY_LEAKS
43 #include <foreign/nvwa/debug_new.h>
44 #endif // CHECK_MEMORY_LEAKS
45 
46 
47 // ===========================================================================
48 // method definitions
49 // ===========================================================================
51 
52 
54  for (std::map<std::string, MSContainer*>::iterator i = myContainers.begin(); i != myContainers.end(); ++i) {
55  delete(*i).second;
56  }
57  myContainers.clear();
58  myWaiting4Vehicle.clear();
59 }
60 
61 
62 bool
63 MSContainerControl::add(const std::string& id, MSContainer* container) {
64  if (myContainers.find(id) == myContainers.end()) {
65  myContainers[id] = container;
66  return true;
67  }
68  return false;
69 }
70 
71 
72 void
74  const std::string& id = container->getID();
75  if (OptionsCont::getOptions().isSet("tripinfo-output")) {
76  OutputDevice& od = OutputDevice::getDeviceByOption("tripinfo-output");
77  od.openTag("containerinfo").writeAttr("id", id).writeAttr("depart", time2string(container->getDesiredDepart()));
78  container->tripInfoOutput(od);
79  od.closeTag();
80  }
81  if (OptionsCont::getOptions().isSet("vehroute-output")) {
82  OutputDevice& od = OutputDevice::getDeviceByOption("vehroute-output");
83  od.openTag("container").writeAttr("id", id).writeAttr("depart", time2string(container->getDesiredDepart())).writeAttr("arrival", time2string(MSNet::getInstance()->getCurrentTimeStep()));
84  container->routeOutput(od);
85  od.closeTag();
86  od << "\n";
87  }
88  const std::map<std::string, MSContainer*>::iterator i = myContainers.find(id);
89  if (i != myContainers.end()) {
90  delete i->second;
91  myContainers.erase(i);
92  }
93 }
94 
95 
96 void
98  const SUMOTime step = time % DELTA_T == 0 ? time : (time / DELTA_T + 1) * DELTA_T;
99  if (myWaiting4Departure.find(step) == myWaiting4Departure.end()) {
101  }
102  myWaiting4Departure[step].push_back(container);
103 }
104 
105 
106 void
108  const SUMOTime step = time % DELTA_T == 0 ? time : (time / DELTA_T + 1) * DELTA_T;
109  if (myWaitingUntil.find(step) == myWaitingUntil.end()) {
111  }
112  myWaitingUntil[step].push_back(container);
113 }
114 
115 
116 void
118  while (myWaiting4Departure.find(time) != myWaiting4Departure.end()) {
119  const ContainerVector& containers = myWaiting4Departure[time];
120  // we cannot use an iterator here because there might be additions to the vector while proceeding
121  for (size_t i = 0; i < containers.size(); ++i) {
122  if (!containers[i]->proceed(net, time)) {
123  erase(containers[i]);
124  }
125  }
126  myWaiting4Departure.erase(time);
127  }
128  while (myWaitingUntil.find(time) != myWaitingUntil.end()) {
129  const ContainerVector& containers = myWaitingUntil[time];
130  // we cannot use an iterator here because there might be additions to the vector while proceeding
131  for (size_t i = 0; i < containers.size(); ++i) {
132  if (!containers[i]->proceed(net, time)) {
133  erase(containers[i]);
134  }
135  }
136  myWaitingUntil.erase(time);
137  }
138 }
139 
140 
141 void
142 MSContainerControl::addWaiting(const MSEdge* const edge, MSContainer* container) {
143  if (myWaiting4Vehicle.find(edge) == myWaiting4Vehicle.end()) {
144  myWaiting4Vehicle[edge] = std::vector<MSContainer*>();
145  }
146  myWaiting4Vehicle[edge].push_back(container);
147 }
148 
149 
150 //bool
151 //MSContainerControl::isWaiting4Vehicle(const MSEdge* const edge, MSContainer* /* p */) const {
152 // return myWaiting4Vehicle.find(edge) != myWaiting4Vehicle.end();
153 //}
154 
155 
156 bool
158  bool ret = false;
159  if (myWaiting4Vehicle.find(edge) != myWaiting4Vehicle.end()) {
160  ContainerVector& waitContainers = myWaiting4Vehicle[edge];
161  for (ContainerVector::iterator i = waitContainers.begin(); i != waitContainers.end();) {
162  const std::string& line = vehicle->getParameter().line == "" ? vehicle->getParameter().id : vehicle->getParameter().line;
164  if ((*i)->isWaitingFor(line) && vehicle->getVehicleType().getContainerCapacity() > vehicle->getContainerNumber()
165  && stop->timeToLoadNextContainer <= currentTime
166  && stop->startPos <= (*i)->getEdgePos() && (*i)->getEdgePos() <= stop->endPos) {
167  edge->removeContainer(*i);
168  vehicle->addContainer(*i);
169  //if the time a container needs to get loaded on the vehicle extends the duration of the stop of the vehicle extend
170  //the duration by setting it to the loading duration of the container
171  const SUMOTime loadingDuration = vehicle->getVehicleType().getLoadingDuration();
172  if (loadingDuration >= stop->duration) {
173  stop->duration = loadingDuration;
174  }
175  //update the time point at which the next container can be loaded on the vehicle
176  stop->timeToLoadNextContainer = currentTime + loadingDuration;
177 
178  static_cast<MSContainer::MSContainerStage_Driving*>((*i)->getCurrentStage())->setVehicle(vehicle);
179  i = waitContainers.erase(i);
180  ret = true;
181  } else {
182  ++i;
183  }
184  }
185  if (waitContainers.size() == 0) {
186  myWaiting4Vehicle.erase(myWaiting4Vehicle.find(edge));
187  }
188  }
189  return ret;
190 }
191 
192 bool
194  return !myContainers.empty();
195 }
196 
197 
198 bool
200  return !myWaiting4Departure.empty() || !myWaitingUntil.empty() || !myTranship.empty();
201 }
202 
203 
204 void
206  myTranship[c->getID()] = c;
207 }
208 
209 
210 void
212  std::map<std::string, MSContainer*>::iterator i = myTranship.find(c->getID());
213  if (i != myTranship.end()) {
214  myTranship.erase(i);
215  }
216 }
217 
218 
219 void
221  for (std::map<const MSEdge*, ContainerVector>::const_iterator i = myWaiting4Vehicle.begin(); i != myWaiting4Vehicle.end(); ++i) {
222  const MSEdge* edge = (*i).first;
223  const ContainerVector& pv = (*i).second;
224  for (ContainerVector::const_iterator j = pv.begin(); j != pv.end(); ++j) {
225  MSContainer* p = (*j);
226  edge->removeContainer(p);
227  WRITE_WARNING("Container " + p->getID() + " aborted waiting for a transport that will never come.");
228  erase(p);
229  }
230  }
231 }
232 
233 
236  return new MSContainer(pars, vtype, plan);
237 }
238 
239 /****************************************************************************/
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:257
std::map< SUMOTime, ContainerVector > myWaiting4Departure
Containers waiting for departure.
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:80
void setWaitEnd(SUMOTime time, MSContainer *container)
sets the arrival time for a waiting container
SUMOTime getLoadingDuration() const
Get this vehicle type's loading duration.
void abortWaiting()
aborts the plan for any container that is still waiting for a ride
void addWaiting(const MSEdge *edge, MSContainer *container)
adds a container to the list of containers waiting for a vehicle on the specified edge ...
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:61
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:159
Definition of vehicle stop (position and duration)
Definition: MSVehicle.h:560
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:235
void setTranship(MSContainer *c)
adds a container to myTranship
virtual void removeContainer(MSContainer *container) const
Remove container from myContainers.
Definition: MSEdge.h:573
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
The simulated network and simulation perfomer.
Definition: MSNet.h:94
The car-following model and parameter.
Definition: MSVehicleType.h:74
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:67
virtual void erase(MSContainer *container)
removes a single container
std::vector< MSContainer * > ContainerVector
A road/street connecting two junctions.
Definition: MSEdge.h:81
unsigned int getContainerNumber() const
Returns the number of containers.
Definition: MSVehicle.cpp:2370
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 timeToLoadNextContainer
The time at which the vehicle is able to load another container.
Definition: MSVehicle.h:592
SUMOTime duration
The stopping duration.
Definition: MSVehicle.h:574
SUMOTime getDesiredDepart() const
Returns the desired departure time.
std::string line
The vehicle's line (mainly for public transport)
std::vector< MSContainerStage * > MSContainerPlan
the structure holding the plan of a container
Definition: MSContainer.h:553
MSContainerControl()
constructor
virtual MSContainer * buildContainer(const SUMOVehicleParameter *pars, const MSVehicleType *vtype, MSContainer::MSContainerPlan *plan) const
Builds a new container.
Structure representing possible vehicle parameter.
bool add(const std::string &id, MSContainer *container)
adds a single container, returns false if an id clash occured
static OutputDevice & getDeviceByOption(const std::string &name)
Returns the device described by the option.
std::map< std::string, MSContainer * > myTranship
all containers being transhiped
void setDeparture(SUMOTime time, MSContainer *container)
sets the arrival time for a waiting container
void addContainer(MSContainer *container)
Adds a container.
Definition: MSVehicle.cpp:2344
void unsetTranship(MSContainer *c)
removes a container from myTranship
const MSVehicleType & getVehicleType() const
Returns the vehicle's type definition.
Definition: MSBaseVehicle.h:90
const std::string & getID() const
returns the container id
int SUMOTime
Definition: SUMOTime.h:43
const SUMOVehicleParameter & getParameter() const
Returns the vehicle's parameter (including departure definition)
std::map< SUMOTime, ContainerVector > myWaitingUntil
the lists of walking / stopping containers
SUMOReal endPos
The stopping position end.
Definition: MSVehicle.h:572
std::map< const MSEdge *, ContainerVector > myWaiting4Vehicle
the lists of waiting containers
unsigned int getContainerCapacity() const
Get this vehicle type's container capacity.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
bool closeTag()
Closes the most recently opened tag.
#define DELTA_T
Definition: SUMOTime.h:50
void tripInfoOutput(OutputDevice &os) const
Called on writing tripinfo output.
bool hasNonWaiting() const
checks whether any container is still engaged in walking / stopping
SUMOReal startPos
The stopping position start.
Definition: MSVehicle.h:570
bool hasContainers() const
checks whether any container waits to finish her plan
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
void routeOutput(OutputDevice &os) const
Called on writing vehroute output.
void checkWaitingContainers(MSNet *net, const SUMOTime time)
checks whether any containers waiting time is over
std::map< std::string, MSContainer * > myContainers
all containers by id
std::string id
The vehicle's id.
virtual ~MSContainerControl()
destructor