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{
50 const std::string opt = "device.taxi.dispatch-algorithm.output";
51 if (OptionsCont::getOptions().isSet(opt)) {
52 OutputDevice::createDeviceByOption(opt, "DispatchInfo");
54 }
55 myKeepUnreachableResTime = string2time(OptionsCont::getOptions().getString("device.taxi.dispatch-keep-unreachable"));
56}
57
59 for (auto item : myGroupReservations) {
60 for (Reservation* res : item.second) {
61 delete res;
62 }
63 }
64 myGroupReservations.clear();
65}
66
67
70 SUMOTime reservationTime,
71 SUMOTime pickupTime,
72 SUMOTime earliestPickupTime,
73 const MSEdge* from, double fromPos,
74 const MSStoppingPlace* fromStop,
75 const MSEdge* to, double toPos,
76 const MSStoppingPlace* toStop,
77 std::string group,
78 const std::string& line,
79 int maxCapacity,
80 int maxContainerCapacity) {
81 // no new reservation nedded if the person can be added to an existing group
82 if (group == "") {
83 // the default empty group implies, no grouping is wanted (and
84 // transportable ids are unique)
85 group = person->getID();
86 } else {
87 auto it2 = myRunningReservations.find(group);
88 if (it2 != myRunningReservations.end()) {
89 for (auto item : it2->second) {
90 Reservation* res = const_cast<Reservation*>(item.first);
91 if (res->persons.count(person) == 0
92 && res->from == from
93 && res->to == to
94 && res->fromPos == fromPos
95 && res->toPos == toPos) {
96 MSDevice_Taxi* taxi = item.second;
97 if (taxi->getState() == taxi->PICKUP
98 && remainingCapacity(taxi, res) > 0
99 && taxi->compatibleLine(taxi->getHolder().getParameter().line, line)) {
100 //std::cout << SIMTIME << " addPerson=" << person->getID() << " extendRes=" << toString(res->persons) << " taxi=" << taxi->getHolder().getID() << " state=" << taxi->getState() << "\n";
101 res->persons.insert(person);
102 taxi->addCustomer(person, res);
103 return res;
104 }
105 }
106 }
107 }
108 }
109 Reservation* result = nullptr;
110 bool added = false;
111 auto it = myGroupReservations.find(group);
112 if (it != myGroupReservations.end()) {
113 // try to add to existing reservation
114 for (Reservation* res : it->second) {
115 if (res->persons.count(person) == 0
116 && res->from == from
117 && res->to == to
118 && res->fromPos == fromPos
119 && res->toPos == toPos) {
120 if (res->persons.size() > 0 && (*res->persons.begin())->isPerson() != person->isPerson()) {
121 WRITE_WARNINGF(TL("Mixing reservations of persons and containers with the same group is not supported for % and %"),
122 (*res->persons.begin())->getID(), person->getID());
123 }
124 if ((person->isPerson() && (int)res->persons.size() >= maxCapacity) ||
125 (!person->isPerson() && (int)res->persons.size() >= maxContainerCapacity)) {
126 // split group to ensure that at least one taxi is capable of delivering group size.
127 continue;
128 }
129 res->persons.insert(person);
130 result = res;
131 added = true;
132 break;
133 }
134 }
135 }
136 if (!added) {
137 Reservation* newRes = new Reservation(toString(myReservationCount++), {person}, reservationTime, pickupTime, earliestPickupTime, from, fromPos, fromStop, to, toPos, toStop, group, line);
138 myGroupReservations[group].push_back(newRes);
139 result = newRes;
140 }
142#ifdef DEBUG_RESERVATION
143 if (DEBUG_COND2(person)) std::cout << SIMTIME
144 << " addReservation p=" << person->getID()
145 << " rT=" << time2string(reservationTime)
146 << " pT=" << time2string(pickupTime)
147 << " from=" << from->getID() << " fromPos=" << fromPos
148 << " to=" << to->getID() << " toPos=" << toPos
149 << " group=" << group
150 << " added=" << added
151 << "\n";
152#endif
153 return result;
154}
155
156
157std::string
159 const MSEdge* from, double fromPos,
160 const MSEdge* to, double toPos,
161 std::string group) {
162 if (group == "") {
163 // the default empty group implies, no grouping is wanted (and
164 // transportable ids are unique)
165 group = person->getID();
166 }
167 std::string removedID = "";
168 auto it = myGroupReservations.find(group);
169 if (it != myGroupReservations.end()) {
170 for (auto itRes = it->second.begin(); itRes != it->second.end(); itRes++) {
171 Reservation* res = *itRes;
172 if (res->persons.count(person) != 0
173 && res->from == from
174 && res->to == to
175 && res->fromPos == fromPos
176 && res->toPos == toPos) {
177 res->persons.erase(person);
178 if (res->persons.empty()) {
179 removedID = res->id;
180 it->second.erase(itRes);
181 // cleans up MSDispatch_Greedy
183 if (it->second.empty()) {
184 myGroupReservations.erase(it);
185 }
186 }
187 break;
188 }
189 }
190 } else {
191 auto it2 = myRunningReservations.find(group);
192 if (it2 != myRunningReservations.end()) {
193 for (auto item : it2->second) {
194 const Reservation* res = item.first;
195 if (res->persons.count(person) != 0
196 && res->from == from
197 && res->to == to
198 && res->fromPos == fromPos
199 && res->toPos == toPos) {
200 MSDevice_Taxi* taxi = item.second;
201 taxi->cancelCustomer(person);
202 if (res->persons.empty()) {
203 removedID = res->id;
204 }
205 break;
206 }
207 }
208 }
209 }
211#ifdef DEBUG_RESERVATION
212 if (DEBUG_COND2(person)) std::cout << SIMTIME
213 << " removeReservation p=" << person->getID()
214 << " from=" << from->getID() << " fromPos=" << fromPos
215 << " to=" << to->getID() << " toPos=" << toPos
216 << " group=" << group
217 << " removedID=" << removedID
218 << " hasServable=" << myHasServableReservations
219 << "\n";
220#endif
221 return removedID;
222}
223
224
227 const MSEdge* from, double fromPos,
228 const MSEdge* to, double toPos,
229 std::string group, double newFromPos) {
230 if (group == "") {
231 // the default empty group implies, no grouping is wanted (and
232 // transportable ids are unique)
233 group = person->getID();
234 }
235 Reservation* result = nullptr;
236 std::string updatedID = "";
237 auto it = myGroupReservations.find(group);
238 if (it != myGroupReservations.end()) {
239 for (auto itRes = it->second.begin(); itRes != it->second.end(); itRes++) {
240 Reservation* res = *itRes;
241 // TODO: if there is already a reservation with the newFromPos, add to this reservation
242 // TODO: if there are other persons in this reservation, create a new reservation for the updated one
243 if (res->persons.count(person) != 0
244 && res->from == from
245 && res->to == to
246 && res->fromPos == fromPos
247 && res->toPos == toPos) {
248 // update fromPos
249 res->fromPos = newFromPos;
250 result = res;
251 updatedID = res->id;
252 break;
253 }
254 }
255 }
256#ifdef DEBUG_RESERVATION
257 if (DEBUG_COND2(person)) std::cout << SIMTIME
258 << " updateReservationFromPos p=" << person->getID()
259 << " from=" << from->getID() << " fromPos=" << fromPos
260 << " to=" << to->getID() << " toPos=" << toPos
261 << " group=" << group
262 << " newFromPos=" << newFromPos
263 << " updatedID=" << updatedID
264 << "\n";
265#endif
266 return result;
267}
268
269
270std::vector<Reservation*>
272 std::vector<Reservation*> reservations;
273 for (const auto& it : myGroupReservations) {
274 reservations.insert(reservations.end(), it.second.begin(), it.second.end());
275 }
276 return reservations;
277}
278
279
280std::vector<const Reservation*>
282 std::vector<const Reservation*> result;
283 for (auto item : myRunningReservations) {
284 for (auto item2 : item.second) {
285 result.push_back(item2.first);
286 }
287 }
288 return result;
289}
290
291
292void
294 auto itR = myRunningReservations.find(res->group);
295 if (itR != myRunningReservations.end() && itR->second.count(res) != 0) {
296 return; // was redispatch
297 }
298 auto it = myGroupReservations.find(res->group);
299 if (it == myGroupReservations.end()) {
300 throw ProcessError(TL("Inconsistent group reservations."));
301 }
302 auto it2 = std::find(it->second.begin(), it->second.end(), res);
303 if (it2 == it->second.end()) {
304 throw ProcessError(TL("Inconsistent group reservations (2)."));
305 }
306 myRunningReservations[res->group][res] = taxi;
307 const_cast<Reservation*>(*it2)->state = Reservation::ASSIGNED;
308 it->second.erase(it2);
309 if (it->second.empty()) {
310 myGroupReservations.erase(it);
311 }
312}
313
314
315void
317 myRunningReservations[res->group][res] = taxi;
318}
319
320
321void
323 myRunningReservations[res->group].erase(res);
324 if (myRunningReservations[res->group].empty()) {
325 myRunningReservations.erase(res->group);
326 }
327 delete res;
328}
329
330
335
336
339 ConstMSEdgeVector edges;
340 double fromPos = taxi->getHolder().getPositionOnLane() - NUMERICAL_EPS;
341 const MSEdge* from = *taxi->getHolder().getRerouteOrigin();
342 const bool originDiffers = from != taxi->getHolder().getEdge();
343 router.compute(from, originDiffers ? 0 : fromPos, res.from, res.fromPos, &taxi->getHolder(), t, edges, true);
344 if (edges.empty()) {
345 return SUMOTime_MAX;
346 } else {
347 if (originDiffers) {
348 assert(from = *(taxi->getHolder().getCurrentRouteEdge() + 1));
349 edges.insert(edges.begin(), taxi->getHolder().getEdge());
350 }
351 return TIME2STEPS(router.recomputeCostsPos(edges, &taxi->getHolder(), fromPos, res.fromPos, t));
352 }
353}
354
355
356bool
358 ConstMSEdgeVector edges;
359 router.compute(res.from, res.fromPos, res.to, res.toPos, &taxi->getHolder(), t, edges, true);
360 return !edges.empty();
361}
362
363
364double
366 const MSEdge* from, double fromPos,
367 const MSEdge* via, double viaPos,
368 const MSEdge* to, double toPos,
370 double& timeDirect) {
371 ConstMSEdgeVector edges;
372 if (timeDirect < 0) {
373 router.compute(from, fromPos, to, toPos, &taxi->getHolder(), t, edges, true);
374 timeDirect = router.recomputeCostsPos(edges, &taxi->getHolder(), fromPos, toPos, t);
375 edges.clear();
376 }
377
378 router.compute(from, fromPos, via, viaPos, &taxi->getHolder(), t, edges, true);
379 const double start = STEPS2TIME(t);
380 const double leg1 = router.recomputeCostsPos(edges, &taxi->getHolder(), fromPos, viaPos, t);
381#ifdef DEBUG_DETOUR
382 std::cout << " leg1=" << toString(edges) << " startPos=" << fromPos << " toPos=" << viaPos << " time=" << leg1 << "\n";
383#endif
384 const double wait = MAX2(0.0, STEPS2TIME(viaTime) - (start + leg1));
385 edges.clear();
386 const SUMOTime timeContinue = TIME2STEPS(start + leg1 + wait);
387 router.compute(via, viaPos, to, toPos, &taxi->getHolder(), timeContinue, edges, true);
388 const double leg2 = router.recomputeCostsPos(edges, &taxi->getHolder(), viaPos, toPos, timeContinue);
389 const double timeDetour = leg1 + wait + leg2;
390#ifdef DEBUG_DETOUR
391 std::cout << " leg2=" << toString(edges) << " startPos=" << viaPos << " toPos=" << toPos << " time=" << leg2 << "\n";
392 std::cout << " t=" << STEPS2TIME(t) << " vt=" << STEPS2TIME(viaTime)
393 << " from=" << from->getID() << " to=" << to->getID() << " via=" << via->getID()
394 << " direct=" << timeDirect << " detour=" << timeDetour << " wait=" << wait << "\n";
395#endif
396 return timeDetour;
397}
398
399
400int
402 assert(res->persons.size() > 0);
403 return ((*res->persons.begin())->isPerson()
405 : taxi->getHolder().getVehicleType().getContainerCapacity()) - (int)res->persons.size();
406}
407
408
409/****************************************************************************/
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:46
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
bool cancelCustomer(const MSTransportable *t)
remove person from reservations
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:1615
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 bool createDeviceByOption(const std::string &optionName, const std::string &rootElement="", const std::string &schemaFile="")
Creates the device using the output definition stored in the named option.
static OutputDevice & getDeviceByOption(const std::string &name)
Returns the device described by the 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