Eclipse SUMO - Simulation of Urban MObility
MSDispatch.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2007-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
18 // An algorithm that performs dispatch for the taxi device
19 /****************************************************************************/
20 #include <config.h>
21 
22 #include <limits>
23 #include <microsim/MSNet.h>
24 #include <microsim/MSEdge.h>
26 #include "MSRoutingEngine.h"
27 #include "MSDispatch.h"
28 
29 //#define DEBUG_RESERVATION
30 //#define DEBUG_DISPATCH
31 //#define DEBUG_SERVABLE
32 //#define DEBUG_TRAVELTIME
33 //#define DEBUG_DETOUR
34 //#define DEBUG_COND2(obj) (obj->getID() == "p0")
35 #define DEBUG_COND2(obj) (true)
36 
37 
38 // ===========================================================================
39 // Reservation methods
40 // ===========================================================================
41 
42 std::string
44  return toString(persons);
45 }
46 
47 // ===========================================================================
48 // MSDispatch methods
49 // ===========================================================================
50 
51 MSDispatch::MSDispatch(const std::map<std::string, std::string>& params) :
52  Parameterised(params),
53  myOutput(nullptr) {
54  const std::string outFile = OptionsCont::getOptions().getString("device.taxi.dispatch-algorithm.output");
55  if (outFile != "") {
57  myOutput->writeXMLHeader("DispatchInfo", "");
58  }
59 }
60 
61 
64  SUMOTime reservationTime,
65  SUMOTime pickupTime,
66  const MSEdge* from, double fromPos,
67  const MSEdge* to, double toPos,
68  const std::string& group,
69  int maxCapacity) {
70  // no new reservation nedded if the person can be added to an existing group
71  Reservation* result = nullptr;
72  bool added = false;
73  auto it = myGroupReservations.find(group);
74  if (it != myGroupReservations.end()) {
75  // try to add to existing reservation
76  for (Reservation* res : it->second) {
77  if (res->persons.count(person) == 0
78  && res->from == from
79  && res->to == to
80  && res->fromPos == fromPos
81  && res->toPos == toPos
82  && (int)res->persons.size() < maxCapacity) {
83  res->persons.insert(person);
84  result = res;
85  added = true;
86  break;
87  }
88  }
89  }
90  if (!added) {
91  Reservation* newRes = new Reservation({person}, reservationTime, pickupTime, from, fromPos, to, toPos, group);
92  myGroupReservations[group].push_back(newRes);
93  result = newRes;
94  }
96 #ifdef DEBUG_RESERVATION
97  if (DEBUG_COND2(person)) std::cout << SIMTIME
98  << " addReservation p=" << person->getID()
99  << " rT=" << time2string(reservationTime)
100  << " pT=" << time2string(pickupTime)
101  << " from=" << from->getID() << " fromPos=" << fromPos
102  << " to=" << to->getID() << " toPos=" << toPos
103  << " group=" << group
104  << " added=" << added
105  << "\n";
106 #endif
107  return result;
108 }
109 
110 std::vector<Reservation*>
112  std::vector<Reservation*> reservations;
113  for (const auto& it : myGroupReservations) {
114  reservations.insert(reservations.end(), it.second.begin(), it.second.end());
115  }
116  return reservations;
117 }
118 
119 
120 void
122  auto it = myGroupReservations.find(res->group);
123  if (it == myGroupReservations.end()) {
124  throw ProcessError("Inconsistent group reservations.");
125  }
126  auto it2 = std::find(it->second.begin(), it->second.end(), res);
127  if (it2 == it->second.end()) {
128  throw ProcessError("Inconsistent group reservations (2).");
129  }
130  delete *it2;
131  it->second.erase(it2);
132  if (it->second.empty()) {
133  myGroupReservations.erase(it);
134  }
135 }
136 
137 
138 SUMOTime
140  ConstMSEdgeVector edges;
141  router.compute(taxi->getHolder().getEdge(), taxi->getHolder().getPositionOnLane(),
142  res.from, res.fromPos, &taxi->getHolder(), t, edges);
143  return TIME2STEPS(router.recomputeCosts(edges, &taxi->getHolder(), t));
144 }
145 
146 
147 double
149  const MSEdge* from, double fromPos,
150  const MSEdge* via, double viaPos,
151  const MSEdge* to, double toPos,
153  double& timeDirect) {
154  ConstMSEdgeVector edges;
155  if (timeDirect < 0) {
156  router.compute(from, fromPos, to, toPos, &taxi->getHolder(), t, edges);
157  timeDirect = router.recomputeCosts(edges, &taxi->getHolder(), fromPos, toPos, t);
158  edges.clear();
159  }
160 
161  router.compute(from, fromPos, via, viaPos, &taxi->getHolder(), t, edges);
162  const double start = STEPS2TIME(t);
163  const double leg1 = router.recomputeCosts(edges, &taxi->getHolder(), fromPos, viaPos, t);
164 #ifdef DEBUG_DETOUR
165  std::cout << " leg1=" << toString(edges) << " startPos=" << fromPos << " toPos=" << viaPos << " time=" << leg1 << "\n";
166 #endif
167  const double wait = MAX2(0.0, STEPS2TIME(viaTime) - (start + leg1));
168  edges.clear();
169  const SUMOTime timeContinue = TIME2STEPS(start + leg1 + wait);
170  router.compute(via, viaPos, to, toPos, &taxi->getHolder(), timeContinue, edges);
171  const double leg2 = router.recomputeCosts(edges, &taxi->getHolder(), viaPos, toPos, timeContinue);
172  const double timeDetour = leg1 + wait + leg2;
173 #ifdef DEBUG_DETOUR
174  std::cout << " leg2=" << toString(edges) << " startPos=" << viaPos << " toPos=" << toPos << " time=" << leg2 << "\n";
175  std::cout << " t=" << STEPS2TIME(t) << " vt=" << STEPS2TIME(viaTime)
176  << " from=" << from->getID() << " to=" << to->getID() << " via=" << via->getID()
177  << " direct=" << timeDirect << " detour=" << timeDetour << " wait=" << wait << "\n";
178 #endif
179  return timeDetour;
180 }
181 
182 /****************************************************************************/
#define DEBUG_COND2(obj)
Definition: MSDispatch.cpp:35
std::vector< const MSEdge * > ConstMSEdgeVector
Definition: MSEdge.h:74
std::string time2string(SUMOTime t)
convert SUMOTime to string
Definition: SUMOTime.cpp:68
#define STEPS2TIME(x)
Definition: SUMOTime.h:53
#define SIMTIME
Definition: SUMOTime.h:60
#define TIME2STEPS(x)
Definition: SUMOTime.h:55
long long int SUMOTime
Definition: SUMOTime.h:31
T MAX2(T a, T b)
Definition: StdDefs.h:79
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:44
A device which collects info on the vehicle trip (mainly on departure and arrival)
Definition: MSDevice_Taxi.h:48
OutputDevice * myOutput
optional file output for dispatch information
Definition: MSDispatch.h:145
static SUMOTime computePickupTime(SUMOTime t, const MSDevice_Taxi *taxi, const Reservation &res, SUMOAbstractRouter< MSEdge, SUMOVehicle > &router)
compute time to pick up the given reservation
Definition: MSDispatch.cpp:139
bool myHasServableReservations
whether the last call to computeDispatch has left servable reservations
Definition: MSDispatch.h:139
std::map< std::string, std::vector< Reservation * > > myGroupReservations
Definition: MSDispatch.h:148
std::vector< Reservation * > getReservations()
retrieve all reservations
Definition: MSDispatch.cpp:111
static double computeDetourTime(SUMOTime t, SUMOTime viaTime, const MSDevice_Taxi *taxi, const MSEdge *from, double fromPos, const MSEdge *via, double viaPos, const MSEdge *to, double toPos, SUMOAbstractRouter< MSEdge, SUMOVehicle > &router, double &timeDirect)
compute directTime and detourTime
Definition: MSDispatch.cpp:148
virtual Reservation * addReservation(MSTransportable *person, SUMOTime reservationTime, SUMOTime pickupTime, const MSEdge *from, double fromPos, const MSEdge *to, double toPos, const std::string &group, int maxCapacity)
add a new reservation
Definition: MSDispatch.cpp:63
MSDispatch(const std::map< std::string, std::string > &params)
Constructor;.
Definition: MSDispatch.cpp:51
void servedReservation(const Reservation *res)
Definition: MSDispatch.cpp:121
A road/street connecting two junctions.
Definition: MSEdge.h:77
SUMOVehicle & getHolder() const
Returns the vehicle that holds this device.
const std::string & getID() const
Returns the id.
Definition: Named.h:73
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
bool writeXMLHeader(const std::string &rootElement, const std::string &schemaFile, std::map< SumoXMLAttr, std::string > attrs=std::map< SumoXMLAttr, std::string >())
Writes an XML header with optional configuration.
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
An upper class for objects with additional parameters.
Definition: Parameterised.h:39
double recomputeCosts(const std::vector< const E * > &edges, const V *const v, SUMOTime msTime, double *lengthp=nullptr) const
virtual bool compute(const E *from, const E *to, const V *const vehicle, SUMOTime msTime, std::vector< const E * > &into, bool silent=false)=0
Builds the route between the given edges using the minimum effort at the given time The definition of...
virtual const MSEdge * getEdge() const =0
Returns the edge the object is currently at.
virtual double getPositionOnLane() const =0
Get the object's position along the lane.
const MSEdge * to
Definition: MSDispatch.h:61
std::string getID() const
debug identification
Definition: MSDispatch.cpp:43
double fromPos
Definition: MSDispatch.h:60
const MSEdge * from
Definition: MSDispatch.h:59
std::string group
Definition: MSDispatch.h:63
std::set< MSTransportable * > persons
Definition: MSDispatch.h:56
double toPos
Definition: MSDispatch.h:62