Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
MSDispatch_Greedy.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>
26#include "MSRoutingEngine.h"
28
29//#define DEBUG_DISPATCH
30//#define DEBUG_SERVABLE
31//#define DEBUG_TRAVELTIME
32//#define DEBUG_COND2(res) (res->id == "")
33#define DEBUG_COND2(obj) (true)
34
35// ===========================================================================
36// MSDispatch_Greedy methods
37// ===========================================================================
38
39void
40MSDispatch_Greedy::computeDispatch(SUMOTime now, const std::vector<MSDevice_Taxi*>& fleet) {
41 int numDispatched = 0;
42 int numPostponed = 0;
43 // find available vehicles
44 std::set<MSDevice_Taxi*, MSVehicleDevice::ComparatorNumericalVehicleIdLess> available;
45 for (auto* taxi : fleet) {
46 if (taxi->isEmpty()) {
47 available.insert(taxi);
48 }
49 }
50 // greedy assign closest vehicle in reservation order
52 std::vector<Reservation*> reservations = getReservations();
53 std::sort(reservations.begin(), reservations.end(), time_sorter());
54#ifdef DEBUG_DISPATCH
55 std::cout << SIMTIME << " computeDispatch fleet=" << fleet.size() << " available=" << available.size() << " reservations=" << toString(reservations) << "\n";
56#endif
57 for (auto it = reservations.begin(); it != reservations.end();) {
58 if (available.size() == 0) {
59 break;
60 }
61 Reservation* res = *it;
62 if (res->recheck > now) {
63 it++;
64 numPostponed++;
65 continue;
66 }
67 //Position pos = res.from->getLanes().front()->geometryPositionAtOffset(res.fromPos);
68 MSDevice_Taxi* closest = nullptr;
69 SUMOTime closestTime = SUMOTime_MAX;
70 bool tooEarly = false;
71 bool unreachable = false;
72 bool unreachableDest = false;
73 for (auto* taxi : available) {
74 if (remainingCapacity(taxi, res) < 0 || !taxi->compatibleLine(res)) {
75 continue;
76 }
77 SUMOTime travelTime = computePickupTime(now, taxi, *res, router);
78 const bool destConnected = isReachable(now, taxi, *res, router);
79#ifdef DEBUG_TRAVELTIME
80 if (DEBUG_COND2(res)) {
81 std::cout << SIMTIME << " taxi=" << taxi->getHolder().getID() << " person=" << toString(res->persons) << " traveltime=" << time2string(travelTime) << " reachable=" << destConnected << "\n";
82 }
83#endif
84 if (travelTime < closestTime && destConnected) {
85 closestTime = travelTime;
86 closest = taxi;
87 SUMOTime taxiWait = res->pickupTime - (now + closestTime);
88 if (taxiWait > myMaximumWaitingTime) {
89 // no need to service this customer now
90 tooEarly = true;
91 res->recheck += MAX2(now + myRecheckTime, res->pickupTime - closestTime - myRecheckSafety);
92 break;
93 }
94 } else if (travelTime == SUMOTime_MAX) {
95 unreachable = true;
96 } else if (!destConnected) {
97 unreachableDest = true;
98 }
99 }
100 if (tooEarly || closest == nullptr) {
101 // too early or all taxis are too small
102 SUMOTime resWait = now - res->pickupTime;
103 if ((unreachable || unreachableDest) && resWait > myKeepUnreachableResTime) {
104 it = reservations.erase(it);
105 if (unreachable) {
106 WRITE_WARNINGF("Aborting reservation for customers '%' from '%' after waiting time % because no taxi can reach the pickup location, time=%.",
107 toString(res->persons), res->from->getID(), time2string(resWait), time2string(SIMSTEP));
108 } else {
109 WRITE_WARNINGF("Aborting reservation for customers '%' to '%' after waiting time % because no taxi can reach the dropoff location, time=%.",
110 toString(res->persons), res->from->getID(), time2string(resWait), time2string(SIMSTEP));
111 }
112 std::set<const MSTransportable*, ComparatorNumericalIdLess> persons = res->persons;
113 for (const MSTransportable* p : persons) {
114 removeReservation(const_cast<MSTransportable*>(p), res->from, res->fromPos, res->to, res->toPos, res->group);
115 }
116 } else {
117 it++;
118 numPostponed++;
119 }
120 } else {
121 numDispatched += dispatch(closest, it, router, reservations);
122 available.erase(closest);
123 }
124 }
125 // check if any taxis are able to service the remaining requests
126 myHasServableReservations = reservations.size() > 0 && (available.size() < fleet.size() || numPostponed > 0 || numDispatched > 0);
127#ifdef DEBUG_SERVABLE
128 std::cout << SIMTIME << " reservations=" << reservations.size() << " avail=" << available.size()
129 << " fleet=" << fleet.size() << " postponed=" << numPostponed << " dispatched=" << numDispatched << "\n";
130#endif
131}
132
133
134int
135MSDispatch_Greedy::dispatch(MSDevice_Taxi* taxi, std::vector<Reservation*>::iterator& resIt, SUMOAbstractRouter<MSEdge, SUMOVehicle>& /*router*/, std::vector<Reservation*>& reservations) {
136#ifdef DEBUG_DISPATCH
137 if (DEBUG_COND2(*resIt)) {
138 std::cout << SIMTIME << " dispatch taxi=" << taxi->getHolder().getID() << " person=" << toString((*resIt)->persons) << "\n";
139 }
140#endif
141 taxi->dispatch(**resIt);
142 servedReservation(*resIt, taxi); // deleting res
143 resIt = reservations.erase(resIt);
144 return 1;
145}
146
147
148// ===========================================================================
149// MSDispatch_GreedyClosest methods
150// ===========================================================================
151
152void
153MSDispatch_GreedyClosest::computeDispatch(SUMOTime now, const std::vector<MSDevice_Taxi*>& fleet) {
154 bool havePostponed = false;
155 int numDispatched = 0;
156 // find available vehicles
157 std::set<MSDevice_Taxi*, MSVehicleDevice::ComparatorNumericalVehicleIdLess> available;
158 for (auto* taxi : fleet) {
159 if (taxi->isEmpty()) {
160 available.insert(taxi);
161 }
162 }
163#ifdef DEBUG_DISPATCH
164 std::cout << SIMTIME << " computeDispatch fleet=" << fleet.size() << " available=" << available.size() << "\n";
165#endif
166 // greedy assign closest vehicle
168 std::vector<Reservation*> activeReservations;
169 for (Reservation* res : getReservations()) {
170 if (res->recheck <= now) {
171 activeReservations.push_back(res);
172 }
173 }
174 if (!available.empty() && !activeReservations.empty()) {
175 struct DispatchCandidate {
176 SUMOTime travelTime;
177 int resIdx;
178 int taxiIdx;
179 Reservation* res;
180 bool operator<(const DispatchCandidate& other) const {
181 if (travelTime != other.travelTime) {
182 return travelTime < other.travelTime;
183 }
184 if (resIdx != other.resIdx) {
185 return resIdx < other.resIdx;
186 }
187 return taxiIdx < other.taxiIdx;
188 }
189 };
190 const std::vector<MSDevice_Taxi*> availableVec(available.begin(), available.end());
191 std::vector<DispatchCandidate> candidates;
192 candidates.reserve(activeReservations.size() * availableVec.size());
193 router.init(-1, now);
194 router.setAutoBulkMode(true);
195 for (int tIdx = 0; tIdx < (int)availableVec.size(); ++tIdx) {
196 MSDevice_Taxi* const taxi = availableVec[tIdx];
197 for (int rIdx = 0; rIdx < (int)activeReservations.size(); ++rIdx) {
198 Reservation* const res = activeReservations[rIdx];
199 if (remainingCapacity(taxi, res) < 0 || !taxi->compatibleLine(res)) {
200 continue;
201 }
202 const SUMOTime travelTime = computePickupTime(now, taxi, *res, router);
203 const SUMOTime taxiWait = res->pickupTime - (now + travelTime);
204#ifdef DEBUG_TRAVELTIME
205 if (DEBUG_COND2(res)) std::cout << SIMTIME << " taxi=" << taxi->getHolder().getID() << " person=" << toString(res->persons)
206 << " traveltime=" << time2string(travelTime)
207 << " pickupTime=" << time2string(res->pickupTime)
208 << " taxiWait=" << time2string(taxiWait) << "\n";
209#endif
210 if (travelTime < SUMOTime_MAX && taxiWait < myMaximumWaitingTime) {
211 candidates.push_back({travelTime, rIdx, tIdx, res});
212 }
213 }
214 }
215 router.setAutoBulkMode(false);
216 std::sort(candidates.begin(), candidates.end());
217 for (const DispatchCandidate& cand : candidates) {
218 if (available.empty() || activeReservations.empty()) {
219 break;
220 }
221 MSDevice_Taxi* const closestTaxi = availableVec[cand.taxiIdx];
222 if (available.count(closestTaxi) == 0) {
223 continue;
224 }
225 auto closeIt = std::find(activeReservations.begin(), activeReservations.end(), cand.res);
226 if (closeIt == activeReservations.end()) {
227 continue;
228 }
229#ifdef DEBUG_DISPATCH
230 if (DEBUG_COND2(cand.res)) std::cout << SIMTIME << " bestTaxi=" << closestTaxi->getHolder().getID() << " bestRes=" << toString(cand.res->persons)
231 << " taxiPos=" << closestTaxi->getHolder().getPositionOnLane() << " resFromPos=" << cand.res->fromPos
232 << " traveltime=" << time2string(cand.travelTime)
233 << " taxiWait=" << time2string(cand.res->pickupTime - (now + cand.travelTime)) << "\n";
234#endif
235 numDispatched += dispatch(closestTaxi, closeIt, router, activeReservations);
236 available.erase(closestTaxi);
237 }
238 if (!available.empty() && !activeReservations.empty()) {
239 // all remaining reservations are too early, unreachable, or too big
240 havePostponed = true;
241 }
242 }
243 // check if any taxis are able to service the remaining requests
244 myHasServableReservations = getReservations().size() > 0 && (available.size() < fleet.size() || havePostponed || numDispatched > 0);
245#ifdef DEBUG_SERVABLE
246 std::cout << SIMTIME << " reservations=" << getReservations().size() << " avail=" << available.size()
247 << " fleet=" << fleet.size() << " postponed=" << havePostponed << " dispatched=" << numDispatched << "\n";
248#endif
249}
250
251
252/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
#define DEBUG_COND2(obj)
#define WRITE_WARNINGF(...)
Definition MsgHandler.h:299
std::string time2string(SUMOTime t, bool humanReadable)
convert SUMOTime to string (independently of global format setting)
Definition SUMOTime.cpp:107
#define SIMSTEP
Definition SUMOTime.h:69
#define SUMOTime_MAX
Definition SUMOTime.h:34
#define SIMTIME
Definition SUMOTime.h:70
@ 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:57
A device which collects info on the vehicle trip (mainly on departure and arrival)
void dispatch(const Reservation &res)
service the given reservation
bool compatibleLine(const Reservation *res)
whether the given reservation is compatible with the taxi line
sorts reservations by time
Definition MSDispatch.h:118
void computeDispatch(SUMOTime now, const std::vector< MSDevice_Taxi * > &fleet)
computes dispatch and updates reservations
const SUMOTime myRecheckTime
recheck interval for early reservations
virtual void computeDispatch(SUMOTime now, const std::vector< MSDevice_Taxi * > &fleet)
computes dispatch and updates reservations
virtual int dispatch(MSDevice_Taxi *taxi, std::vector< Reservation * >::iterator &resIt, SUMOAbstractRouter< MSEdge, SUMOVehicle > &router, std::vector< Reservation * > &reservations)
trigger taxi dispatch.
const SUMOTime myRecheckSafety
const SUMOTime myMaximumWaitingTime
maximum time to arrive earlier at customer
const int myRoutingMode
which router/edge weights to use
Definition MSDispatch.h:236
bool isReachable(SUMOTime t, const MSDevice_Taxi *taxi, const Reservation &res, SUMOAbstractRouter< MSEdge, SUMOVehicle > &router)
compute whether the reservation is servable
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
std::vector< Reservation * > getReservations()
retrieve all reservations
SUMOTime myKeepUnreachableResTime
the duration before canceling unreachable reservations
Definition MSDispatch.h:231
virtual SUMOAbstractRouter< MSEdge, SUMOVehicle > & getRouter() const
void servedReservation(const Reservation *res, MSDevice_Taxi *taxi)
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition MSNet.cpp:201
MSVehicleRouter & getRouterTT(int rngIndex, const Prohibitions &prohibited={}) const
Definition MSNet.cpp:1659
static MSVehicleRouter & getRouterTT(const int rngIndex, SUMOVehicleClass svc, const Prohibitions &prohibited={})
return the vehicle router instance
SUMOVehicle & getHolder() const
Returns the vehicle that holds this device.
const std::string & getID() const
Returns the id.
Definition Named.h:73
void init(const int edgeID, const SUMOTime msTime)
void setAutoBulkMode(const bool mode)
virtual double getPositionOnLane() const =0
Get the object's position along the lane.
bool operator<(const json_pointer< RefStringTypeLhs > &lhs, const json_pointer< RefStringTypeRhs > &rhs) noexcept
Definition json.hpp:15485
SUMOTime pickupTime
Definition MSDispatch.h:79
const MSEdge * to
Definition MSDispatch.h:84
SUMOTime recheck
Definition MSDispatch.h:89
double fromPos
Definition MSDispatch.h:82
const MSEdge * from
Definition MSDispatch.h:81
std::string group
Definition MSDispatch.h:87
std::set< const MSTransportable *, ComparatorNumericalIdLess > persons
Definition MSDispatch.h:77
double toPos
Definition MSDispatch.h:85