Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
MSDispatch.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2007-2026 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>
25#include <microsim/MSGlobals.h>
27#include "MSRoutingEngine.h"
28#include "MSDispatch.h"
29
30//#define DEBUG_RESERVATION
31//#define DEBUG_DETOUR
32//#define DEBUG_COND2(obj) (obj->getID() == "p0")
33#define DEBUG_COND2(obj) (true)
34
35
36// ===========================================================================
37// Reservation methods
38// ===========================================================================
39
40// ===========================================================================
41// MSDispatch methods
42// ===========================================================================
43
45 Parameterised(params),
46 myOutput(nullptr),
47 myReservationCount(0),
48 myRoutingMode(StringUtils::toInt(getParameter("routingMode", "1"))) {
49 const std::string opt = "device.taxi.dispatch-algorithm.output";
50 if (OptionsCont::getOptions().isSet(opt)) {
51 OutputDevice::createDeviceByOption(opt, "DispatchInfo");
53 }
54 myKeepUnreachableResTime = string2time(OptionsCont::getOptions().getString("device.taxi.dispatch-keep-unreachable"));
55}
56
58 for (auto item : myGroupReservations) {
59 for (Reservation* res : item.second) {
60 delete res;
61 }
62 }
63 myGroupReservations.clear();
64}
65
66
69 SUMOTime reservationTime,
70 SUMOTime pickupTime,
71 SUMOTime earliestPickupTime,
72 const MSEdge* from, double fromPos,
73 const MSStoppingPlace* fromStop,
74 const MSEdge* to, double toPos,
75 const MSStoppingPlace* toStop,
76 std::string group,
77 const std::string& line,
78 int maxCapacity,
79 int maxContainerCapacity) {
80 // no new reservation nedded if the person can be added to an existing group
81 if (group == "") {
82 // the default empty group implies, no grouping is wanted (and
83 // transportable ids are unique)
84 group = person->getID();
85 } else {
86 auto it2 = myRunningReservations.find(group);
87 if (it2 != myRunningReservations.end()) {
88 for (auto item : it2->second) {
89 Reservation* res = const_cast<Reservation*>(item.first);
90 if (res->persons.count(person) == 0
91 && res->from == from
92 && res->to == to
93 && res->fromPos == fromPos
94 && res->toPos == toPos) {
95 MSDevice_Taxi* taxi = item.second;
96 if (taxi->getState() == taxi->PICKUP
97 && remainingCapacity(taxi, res) > 0
98 && taxi->compatibleLine(taxi->getHolder().getParameter().line, line)) {
99 //std::cout << SIMTIME << " addPerson=" << person->getID() << " extendRes=" << toString(res->persons) << " taxi=" << taxi->getHolder().getID() << " state=" << taxi->getState() << "\n";
100 res->persons.insert(person);
101 taxi->addCustomer(person, res);
102 return res;
103 }
104 }
105 }
106 }
107 }
108 Reservation* result = nullptr;
109 bool added = false;
110 auto it = myGroupReservations.find(group);
111 if (it != myGroupReservations.end()) {
112 // try to add to existing reservation
113 for (Reservation* res : it->second) {
114 if (res->persons.count(person) == 0
115 && res->from == from
116 && res->to == to
117 && res->fromPos == fromPos
118 && res->toPos == toPos) {
119 if (res->persons.size() > 0 && (*res->persons.begin())->isPerson() != person->isPerson()) {
120 WRITE_WARNINGF(TL("Mixing reservations of persons and containers with the same group is not supported for % and %"),
121 (*res->persons.begin())->getID(), person->getID());
122 }
123 if ((person->isPerson() && (int)res->persons.size() >= maxCapacity) ||
124 (!person->isPerson() && (int)res->persons.size() >= maxContainerCapacity)) {
125 // split group to ensure that at least one taxi is capable of delivering group size.
126 continue;
127 }
128 res->persons.insert(person);
129 result = res;
130 added = true;
131 break;
132 }
133 }
134 }
135 if (!added) {
136 Reservation* newRes = new Reservation(toString(myReservationCount++), {person}, reservationTime, pickupTime, earliestPickupTime, from, fromPos, fromStop, to, toPos, toStop, group, line);
137 myGroupReservations[group].push_back(newRes);
138 result = newRes;
139 }
141#ifdef DEBUG_RESERVATION
142 if (DEBUG_COND2(person)) std::cout << SIMTIME
143 << " addReservation p=" << person->getID()
144 << " rT=" << time2string(reservationTime)
145 << " pT=" << time2string(pickupTime)
146 << " from=" << from->getID() << " fromPos=" << fromPos
147 << " to=" << to->getID() << " toPos=" << toPos
148 << " group=" << group
149 << " added=" << added
150 << "\n";
151#endif
152 return result;
153}
154
155
156std::string
158 const MSEdge* from, double fromPos,
159 const MSEdge* to, double toPos,
160 std::string group) {
161 if (group == "") {
162 // the default empty group implies, no grouping is wanted (and
163 // transportable ids are unique)
164 group = person->getID();
165 }
166 std::string removedID = "";
167 auto it = myGroupReservations.find(group);
168 if (it != myGroupReservations.end()) {
169 for (auto itRes = it->second.begin(); itRes != it->second.end(); itRes++) {
170 Reservation* res = *itRes;
171 if (res->persons.count(person) != 0
172 && res->from == from
173 && res->to == to
174 && res->fromPos == fromPos
175 && res->toPos == toPos) {
176 res->persons.erase(person);
177 if (res->persons.empty()) {
178 removedID = res->id;
179 it->second.erase(itRes);
180 // cleans up MSDispatch_Greedy
182 if (it->second.empty()) {
183 myGroupReservations.erase(it);
184 }
185 }
186 break;
187 }
188 }
189 } else {
190 auto it2 = myRunningReservations.find(group);
191 if (it2 != myRunningReservations.end()) {
192 for (auto item : it2->second) {
193 const Reservation* const res = item.first;
194 if (res->persons.count(person) != 0
195 && res->from == from
196 && res->to == to
197 && res->fromPos == fromPos
198 && res->toPos == toPos) {
199 if (res->persons.size() == 1) {
200 removedID = res->id;
201 }
202 item.second->cancelCustomer(person); // will delete res via fulfilledReservation if necessary
203 break;
204 }
205 }
206 }
207 }
209#ifdef DEBUG_RESERVATION
210 if (DEBUG_COND2(person)) std::cout << SIMTIME
211 << " removeReservation p=" << person->getID()
212 << " from=" << from->getID() << " fromPos=" << fromPos
213 << " to=" << to->getID() << " toPos=" << toPos
214 << " group=" << group
215 << " removedID=" << removedID
216 << " hasServable=" << myHasServableReservations
217 << "\n";
218#endif
219 return removedID;
220}
221
222
225 const MSEdge* from, double fromPos,
226 const MSEdge* to, double toPos,
227 std::string group, double newFromPos) {
228 if (group == "") {
229 // the default empty group implies, no grouping is wanted (and
230 // transportable ids are unique)
231 group = person->getID();
232 }
233 Reservation* result = nullptr;
234 std::string updatedID = "";
235 auto it = myGroupReservations.find(group);
236 if (it != myGroupReservations.end()) {
237 for (auto itRes = it->second.begin(); itRes != it->second.end(); itRes++) {
238 Reservation* res = *itRes;
239 // TODO: if there is already a reservation with the newFromPos, add to this reservation
240 // TODO: if there are other persons in this reservation, create a new reservation for the updated one
241 if (res->persons.count(person) != 0
242 && res->from == from
243 && res->to == to
244 && res->fromPos == fromPos
245 && res->toPos == toPos) {
246 // update fromPos
247 res->fromPos = newFromPos;
248 result = res;
249 updatedID = res->id;
250 break;
251 }
252 }
253 }
254#ifdef DEBUG_RESERVATION
255 if (DEBUG_COND2(person)) std::cout << SIMTIME
256 << " updateReservationFromPos p=" << person->getID()
257 << " from=" << from->getID() << " fromPos=" << fromPos
258 << " to=" << to->getID() << " toPos=" << toPos
259 << " group=" << group
260 << " newFromPos=" << newFromPos
261 << " updatedID=" << updatedID
262 << "\n";
263#endif
264 return result;
265}
266
267
268std::vector<Reservation*>
270 std::vector<Reservation*> reservations;
271 for (const auto& it : myGroupReservations) {
272 reservations.insert(reservations.end(), it.second.begin(), it.second.end());
273 }
274 return reservations;
275}
276
277
278std::vector<const Reservation*>
280 std::vector<const Reservation*> result;
281 for (auto item : myRunningReservations) {
282 for (auto item2 : item.second) {
283 result.push_back(item2.first);
284 }
285 }
286 return result;
287}
288
289
290void
292 auto itR = myRunningReservations.find(res->group);
293 if (itR != myRunningReservations.end() && itR->second.count(res) != 0) {
294 return; // was redispatch
295 }
296 auto it = myGroupReservations.find(res->group);
297 if (it == myGroupReservations.end()) {
298 throw ProcessError(TL("Inconsistent group reservations."));
299 }
300 auto it2 = std::find(it->second.begin(), it->second.end(), res);
301 if (it2 == it->second.end()) {
302 throw ProcessError(TL("Inconsistent group reservations (2)."));
303 }
304 myRunningReservations[res->group][res] = taxi;
305 const_cast<Reservation*>(*it2)->state = Reservation::ASSIGNED;
306 it->second.erase(it2);
307 if (it->second.empty()) {
308 myGroupReservations.erase(it);
309 }
310}
311
312
313void
315 myRunningReservations[res->group][res] = taxi;
316}
317
318
319void
321 myRunningReservations[res->group].erase(res);
322 if (myRunningReservations[res->group].empty()) {
323 myRunningReservations.erase(res->group);
324 }
325 delete res;
326}
327
328
333
334
337 ConstMSEdgeVector edges;
338 double fromPos = taxi->getHolder().getPositionOnLane() - NUMERICAL_EPS;
339 const MSEdge* from = *taxi->getHolder().getRerouteOrigin();
340 const bool originDiffers = from != taxi->getHolder().getEdge();
341 router.compute(from, originDiffers ? 0 : fromPos, res.from, res.fromPos, &taxi->getHolder(), t, edges, true);
342 if (edges.empty()) {
343 return SUMOTime_MAX;
344 } else {
345 if (originDiffers) {
346 assert(from == *(taxi->getHolder().getCurrentRouteEdge() + 1));
347 edges.insert(edges.begin(), taxi->getHolder().getEdge());
348 }
349 return TIME2STEPS(router.recomputeCostsPos(edges, &taxi->getHolder(), fromPos, res.fromPos, t));
350 }
351}
352
353
354bool
356 ConstMSEdgeVector edges;
357 router.compute(res.from, res.fromPos, res.to, res.toPos, &taxi->getHolder(), t, edges, true);
358 return !edges.empty();
359}
360
361
362double
364 const MSEdge* from, double fromPos,
365 const MSEdge* via, double viaPos,
366 const MSEdge* to, double toPos,
368 double& timeDirect) {
369 ConstMSEdgeVector edges;
370 if (timeDirect < 0) {
371 router.compute(from, fromPos, to, toPos, &taxi->getHolder(), t, edges, true);
372 timeDirect = router.recomputeCostsPos(edges, &taxi->getHolder(), fromPos, toPos, t);
373 edges.clear();
374 }
375
376 router.compute(from, fromPos, via, viaPos, &taxi->getHolder(), t, edges, true);
377 const double start = STEPS2TIME(t);
378 const double leg1 = router.recomputeCostsPos(edges, &taxi->getHolder(), fromPos, viaPos, t);
379#ifdef DEBUG_DETOUR
380 std::cout << " leg1=" << toString(edges) << " startPos=" << fromPos << " toPos=" << viaPos << " time=" << leg1 << "\n";
381#endif
382 const double wait = MAX2(0.0, STEPS2TIME(viaTime) - (start + leg1));
383 edges.clear();
384 const SUMOTime timeContinue = TIME2STEPS(start + leg1 + wait);
385 router.compute(via, viaPos, to, toPos, &taxi->getHolder(), timeContinue, edges, true);
386 const double leg2 = router.recomputeCostsPos(edges, &taxi->getHolder(), viaPos, toPos, timeContinue);
387 const double timeDetour = leg1 + wait + leg2;
388#ifdef DEBUG_DETOUR
389 std::cout << " leg2=" << toString(edges) << " startPos=" << viaPos << " toPos=" << toPos << " time=" << leg2 << "\n";
390 std::cout << " t=" << STEPS2TIME(t) << " vt=" << STEPS2TIME(viaTime)
391 << " from=" << from->getID() << " to=" << to->getID() << " via=" << via->getID()
392 << " direct=" << timeDirect << " detour=" << timeDetour << " wait=" << wait << "\n";
393#endif
394 return timeDetour;
395}
396
397
398int
400 assert(res->persons.size() > 0);
401 return ((*res->persons.begin())->isPerson()
403 : taxi->getHolder().getVehicleType().getContainerCapacity()) - (int)res->persons.size();
404}
405
406
407/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
#define DEBUG_COND2(obj)
Definition MESegment.cpp:54
std::vector< const MSEdge * > ConstMSEdgeVector
Definition MSEdge.h:74
#define WRITE_WARNINGF(...)
Definition MsgHandler.h:287
#define TL(string)
Definition MsgHandler.h:304
SUMOTime string2time(const std::string &r)
convert string to SUMOTime
Definition SUMOTime.cpp:46
std::string time2string(SUMOTime t, bool humanReadable)
convert SUMOTime to string (independently of global format setting)
Definition SUMOTime.cpp:91
#define STEPS2TIME(x)
Definition SUMOTime.h:58
#define SUMOTime_MAX
Definition SUMOTime.h:34
#define SIMTIME
Definition SUMOTime.h:65
#define TIME2STEPS(x)
Definition SUMOTime.h:60
@ SVC_TAXI
vehicle is a taxi
T MAX2(T a, T b)
Definition StdDefs.h:86
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:49
A device which collects info on the vehicle trip (mainly on departure and arrival)
void addCustomer(const MSTransportable *t, const Reservation *res)
add person after extending reservation
int getState() const
bool compatibleLine(const Reservation *res)
whether the given reservation is compatible with the taxi line
OutputDevice * myOutput
optional file output for dispatch information
Definition MSDispatch.h:212
const int myRoutingMode
which router/edge weights to use
Definition MSDispatch.h:222
bool isReachable(SUMOTime t, const MSDevice_Taxi *taxi, const Reservation &res, SUMOAbstractRouter< MSEdge, SUMOVehicle > &router)
compute whether the reservation is servable
void swappedRunning(const Reservation *res, MSDevice_Taxi *taxi)
int remainingCapacity(const MSDevice_Taxi *taxi, const Reservation *res)
whether the given taxi has sufficient capacity to serve the reservation
static SUMOTime computePickupTime(SUMOTime t, const MSDevice_Taxi *taxi, const Reservation &res, SUMOAbstractRouter< MSEdge, SUMOVehicle > &router)
compute time to pick up the given reservation
virtual std::string removeReservation(MSTransportable *person, const MSEdge *from, double fromPos, const MSEdge *to, double toPos, std::string group)
remove person from reservation. If the whole reservation is removed, return its id
bool myHasServableReservations
whether the last call to computeDispatch has left servable reservations
Definition MSDispatch.h:198
virtual Reservation * updateReservationFromPos(MSTransportable *person, const MSEdge *from, double fromPos, const MSEdge *to, double toPos, std::string group, double newFromPos)
update fromPos of the person's reservation. TODO: if there is already a reservation with the newFromP...
std::map< std::string, std::vector< Reservation * > > myGroupReservations
Definition MSDispatch.h:219
std::vector< Reservation * > getReservations()
retrieve all reservations
virtual std::vector< const Reservation * > getRunningReservations()
retrieve all reservations that were already dispatched and are still active
SUMOTime myKeepUnreachableResTime
the duration before canceling unreachable reservations
Definition MSDispatch.h:217
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
virtual SUMOAbstractRouter< MSEdge, SUMOVehicle > & getRouter() const
virtual Reservation * addReservation(MSTransportable *person, SUMOTime reservationTime, SUMOTime pickupTime, SUMOTime earliestPickupTime, const MSEdge *from, double fromPos, const MSStoppingPlace *fromStop, const MSEdge *to, double toPos, const MSStoppingPlace *tostop, std::string group, const std::string &line, int maxCapacity, int maxContainerCapacity)
add a new reservation
int myReservationCount
Definition MSDispatch.h:214
virtual void fulfilledReservation(const Reservation *res)
erase reservation from storage
MSDispatch(const Parameterised::Map &params)
Constructor;.
std::map< std::string, std::map< const Reservation *, MSDevice_Taxi *, ComparatorIdLess > > myRunningReservations
Definition MSDispatch.h:209
virtual ~MSDispatch()
Destructor.
void servedReservation(const Reservation *res, MSDevice_Taxi *taxi)
A road/street connecting two junctions.
Definition MSEdge.h:77
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition MSNet.cpp:187
MSVehicleRouter & getRouterTT(int rngIndex, const Prohibitions &prohibited={}) const
Definition MSNet.cpp:1607
static MSVehicleRouter & getRouterTT(const int rngIndex, SUMOVehicleClass svc, const Prohibitions &prohibited={})
return the vehicle router instance
A lane area vehicles can halt at.
bool isPerson() const override
Whether it is a person.
SUMOVehicle & getHolder() const
Returns the vehicle that holds this device.
int getPersonCapacity() const
Get this vehicle type's person capacity.
int getContainerCapacity() const
Get this vehicle type's container capacity.
const std::string & getID() const
Returns the id.
Definition Named.h:74
static OptionsCont & getOptions()
Retrieves the options.
static OutputDevice & getDeviceByOption(const std::string &name)
Returns the device described by the option.
static bool createDeviceByOption(const std::string &optionName, const std::string &rootElement="", const std::string &schemaFile="", const int maximumDepth=2)
Creates the device using the output definition stored in the named option.
An upper class for objects with additional parameters.
std::map< std::string, std::string > Map
parameters map
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...
double recomputeCostsPos(const std::vector< const E * > &edges, const V *const v, double fromPos, double toPos, SUMOTime msTime, double *lengthp=nullptr) const
virtual const MSVehicleType & getVehicleType() const =0
Returns the object's "vehicle" type.
virtual const SUMOVehicleParameter & getParameter() const =0
Returns the vehicle's parameter (including departure definition)
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.
virtual ConstMSEdgeVector::const_iterator getRerouteOrigin() const =0
Returns the starting point for reroutes (usually the current edge)
virtual const ConstMSEdgeVector::const_iterator & getCurrentRouteEdge() const =0
Returns an iterator pointing to the current edge in this vehicles route.
std::string line
The vehicle's line (mainly for public transport)
Some static methods for string processing.
Definition StringUtils.h:40
std::string id
Definition MSDispatch.h:76
const MSEdge * to
Definition MSDispatch.h:84
double fromPos
Definition MSDispatch.h:82
const MSEdge * from
Definition MSDispatch.h:81
std::string group
Definition MSDispatch.h:87
ReservationState state
Definition MSDispatch.h:90
std::set< const MSTransportable * > persons
Definition MSDispatch.h:77
double toPos
Definition MSDispatch.h:85