Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
MSDispatch_TraCI.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-2025 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>
24#include "MSDispatch_TraCI.h"
25
26//#define DEBUG_RESERVATION
27//#define DEBUG_DISPATCH
28//#define DEBUG_SERVABLE
29//#define DEBUG_TRAVELTIME
30//#define DEBUG_DETOUR
31//#define DEBUG_COND2(obj) (obj->getID() == "p0")
32#define DEBUG_COND2(obj) (true)
33
34// ===========================================================================
35// MSDispatch_TraCI methods
36// ===========================================================================
37
40 SUMOTime reservationTime,
41 SUMOTime pickupTime,
42 SUMOTime earliestPickupTime,
43 const MSEdge* from, double fromPos,
44 const MSStoppingPlace* fromStop,
45 const MSEdge* to, double toPos,
46 const MSStoppingPlace* toStop,
47 std::string group,
48 const std::string& line,
49 int maxCapacity,
50 int maxContainerCapacity) {
51 Reservation* res = MSDispatch::addReservation(person, reservationTime, pickupTime, earliestPickupTime, from, fromPos, fromStop, to, toPos, toStop, group, line, maxCapacity, maxContainerCapacity);
52 if (!myReservationLookup.has(res)) {
53 myReservationLookup.insert(res->id, res);
54 }
55 return res;
56}
57
58std::string
60 const MSEdge* from, double fromPos,
61 const MSEdge* to, double toPos,
62 std::string group) {
63 const std::string removedID = MSDispatch::removeReservation(person, from, fromPos, to, toPos, group);
64 if (myReservationLookup.hasString(removedID)) {
65 // warning! res is already deleted
66 const Reservation* res = myReservationLookup.get(removedID);
67 myReservationLookup.remove(removedID, res);
68 }
69 return removedID;
70}
71
72
73void
79
80void
81MSDispatch_TraCI::interpretDispatch(MSDevice_Taxi* taxi, const std::vector<std::string>& reservationsIDs) {
82 std::vector<const Reservation*> reservations;
83 for (std::string resID : reservationsIDs) {
84 if (myReservationLookup.hasString(resID)) {
85 reservations.push_back(myReservationLookup.get(resID));
86 } else {
87 throw InvalidArgument("Reservation id '" + resID + "' is not known");
88 }
89 }
90 try {
91 if (reservations.size() == 1) {
92 taxi->dispatch(*reservations.front());
93 } else {
94 taxi->dispatchShared(reservations);
95 }
96 } catch (ProcessError& e) {
97 throw InvalidArgument(e.what());
98 }
99 // in case of ride sharing the same reservation may occur multiple times
100 std::set<const Reservation*> unique(reservations.begin(), reservations.end());
101 for (const Reservation* res : unique) {
102 servedReservation(res, taxi);
103 }
104}
105
106
107std::string
108MSDispatch_TraCI::splitReservation(std::string resID, std::vector<std::string> personIDs) {
109 if (myReservationLookup.hasString(resID)) {
110 Reservation* res = const_cast<Reservation*>(myReservationLookup.get(resID));
111 if (myRunningReservations.count(res->group) != 0 && myRunningReservations[res->group].count(res) != 0) {
112 throw InvalidArgument("Cannot split reservation '" + resID + "' after dispatch");
113 }
114 std::set<std::string> allPersons;
115 for (const MSTransportable* t : res->persons) {
116 allPersons.insert(t->getID());
117 }
118 for (std::string p : personIDs) {
119 if (allPersons.count(p) == 0) {
120 throw InvalidArgument("Person '" + p + "' is not part of reservation '" + resID + "'");
121 }
122 }
123 if (personIDs.size() == allPersons.size()) {
124 throw InvalidArgument("Cannot remove all person from reservation '" + resID + "'");
125 }
126 std::vector<const MSTransportable*> split;
127 for (const std::string& p : personIDs) {
128 for (const MSTransportable* const t : res->persons) {
129 if (t->getID() == p) {
130 res->persons.erase(t);
131 split.push_back(t);
132 break;
133 }
134 }
135 }
137 res->reservationTime, res->pickupTime,
139 res->from, res->fromPos, res->fromStop,
140 res->to, res->toPos, res->toStop,
141 res->group, res->line);
142 myGroupReservations[res->group].push_back(newRes);
143 myReservationLookup.insert(newRes->id, newRes);
144 return newRes->id;
145 } else {
146 throw InvalidArgument("Reservation id '" + resID + "' is not known");
147 }
148}
149
150
151
152//
153/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
std::vector< std::string > & split(const std::string &s, char delim, std::vector< std::string > &elems)
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 dispatch(const Reservation &res)
service the given reservation
void dispatchShared(std::vector< const Reservation * > reservations)
service the given reservations
void fulfilledReservation(const Reservation *res) override
erase reservation from storage
std::string splitReservation(std::string resID, std::vector< std::string > personIDs)
split existing reservations and return the new reservation id
StringBijection< const Reservation * > myReservationLookup
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) override
add a new reservation
void interpretDispatch(MSDevice_Taxi *taxi, const std::vector< std::string > &reservationsIDs)
trigger taxi dispatch.
std::string removeReservation(MSTransportable *person, const MSEdge *from, double fromPos, const MSEdge *to, double toPos, std::string group) override
remove person from reservation. If the whole reservation is removed, return its id
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:194
std::map< std::string, std::vector< Reservation * > > myGroupReservations
Definition MSDispatch.h:213
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:208
virtual void fulfilledReservation(const Reservation *res)
erase reservation from storage
std::map< std::string, std::map< const Reservation *, MSDevice_Taxi * > > myRunningReservations
Definition MSDispatch.h:203
void servedReservation(const Reservation *res, MSDevice_Taxi *taxi)
A road/street connecting two junctions.
Definition MSEdge.h:77
A lane area vehicles can halt at.
void remove(const std::string str, const T key)
remove string
bool has(const T key) const
check if the given key exist
bool hasString(const std::string &str) const
check if the given string exist
int size() const
get number of key-attributes
T get(const std::string &str) const
get key
void insert(const std::string str, const T key, bool checkDuplicates=true)
insert string and their associated key
const MSStoppingPlace * toStop
Definition MSDispatch.h:86
SUMOTime pickupTime
Definition MSDispatch.h:79
const MSStoppingPlace * fromStop
Definition MSDispatch.h:83
std::string id
Definition MSDispatch.h:76
const MSEdge * to
Definition MSDispatch.h:84
double fromPos
Definition MSDispatch.h:82
std::string line
Definition MSDispatch.h:88
const MSEdge * from
Definition MSDispatch.h:81
SUMOTime reservationTime
Definition MSDispatch.h:78
std::string group
Definition MSDispatch.h:87
SUMOTime earliestPickupTime
Definition MSDispatch.h:80
std::set< const MSTransportable * > persons
Definition MSDispatch.h:77
double toPos
Definition MSDispatch.h:85