Eclipse SUMO - Simulation of Urban MObility
MSDispatch_TraCI.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-2022 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  const MSEdge* from, double fromPos,
43  const MSEdge* to, double toPos,
44  std::string group,
45  const std::string& line,
46  int maxCapacity,
47  int maxContainerCapacity) {
48  Reservation* res = MSDispatch::addReservation(person, reservationTime, pickupTime, from, fromPos, to, toPos, group, line, maxCapacity, maxContainerCapacity);
49  if (!myReservationLookup.has(res)) {
50  myReservationLookup.insert(res->id, res);
51  }
52  return res;
53 }
54 
55 std::string
57  const MSEdge* from, double fromPos,
58  const MSEdge* to, double toPos,
59  std::string group) {
60  const std::string removedID = MSDispatch::removeReservation(person, from, fromPos, to, toPos, group);
61  if (myReservationLookup.hasString(removedID)) {
62  // warning! res is already deleted
63  const Reservation* res = myReservationLookup.get(removedID);
64  myReservationLookup.remove(removedID, res);
65  }
66  return removedID;
67 }
68 
69 
70 void
72  myReservationLookup.remove(res->id, res);
74 }
75 
76 void
77 MSDispatch_TraCI::interpretDispatch(MSDevice_Taxi* taxi, const std::vector<std::string>& reservationsIDs) {
78  std::vector<const Reservation*> reservations;
79  for (std::string resID : reservationsIDs) {
80  if (myReservationLookup.hasString(resID)) {
81  reservations.push_back(myReservationLookup.get(resID));
82  } else {
83  throw InvalidArgument("Reservation id '" + resID + "' is not known");
84  }
85  }
86  try {
87  if (reservations.size() == 1) {
88  taxi->dispatch(*reservations.front());
89  } else {
90  taxi->dispatchShared(reservations);
91  }
92  } catch (ProcessError& e) {
93  throw InvalidArgument(e.what());
94  }
95  // in case of ride sharing the same reservation may occur multiple times
96  std::set<const Reservation*> unique(reservations.begin(), reservations.end());
97  for (const Reservation* res : unique) {
98  servedReservation(res);
99  }
100 }
101 
102 
103 std::string
104 MSDispatch_TraCI::splitReservation(std::string resID, std::vector<std::string> personIDs) {
105  if (myReservationLookup.hasString(resID)) {
106  Reservation* res = const_cast<Reservation*>(myReservationLookup.get(resID));
107  if (myRunningReservations.count(res) != 0) {
108  throw InvalidArgument("Cannot split reservation '" + resID + "' after dispatch");
109  }
110  std::set<std::string> allPersons;
111  for (MSTransportable* t : res->persons) {
112  allPersons.insert(t->getID());
113  }
114  for (std::string p : personIDs) {
115  if (allPersons.count(p) == 0) {
116  throw InvalidArgument("Person '" + p + "' is not part of reservation '" + resID + "'");
117  }
118  }
119  if (personIDs.size() == allPersons.size()) {
120  throw InvalidArgument("Cannot remove all person from reservation '" + resID + "'");
121  }
122  std::vector<MSTransportable*> split;
123  for (const std::string& p : personIDs) {
124  for (MSTransportable* const t : res->persons) {
125  if (t->getID() == p) {
126  res->persons.erase(t);
127  split.push_back(t);
128  break;
129  }
130  }
131  }
133  res->reservationTime, res->pickupTime,
134  res->from, res->fromPos,
135  res->to, res->toPos, res->group, res->line);
136  myGroupReservations[res->group].push_back(newRes);
137  myReservationLookup.insert(newRes->id, newRes);
138  return newRes->id;
139  } else {
140  throw InvalidArgument("Reservation id '" + resID + "' is not known");
141  }
142 }
143 
144 
145 
146 //
147 /****************************************************************************/
std::vector< std::string > & split(const std::string &s, char delim, std::vector< std::string > &elems)
long long int SUMOTime
Definition: SUMOTime.h:32
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)
Definition: MSDevice_Taxi.h:48
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
void interpretDispatch(MSDevice_Taxi *taxi, const std::vector< std::string > &reservationsIDs)
trigger taxi dispatch.
Reservation * addReservation(MSTransportable *person, SUMOTime reservationTime, SUMOTime pickupTime, const MSEdge *from, double fromPos, const MSEdge *to, double toPos, std::string group, const std::string &line, int maxCapacity, int maxContainerCapacity) override
add a new reservation
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 it's 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 it's id
Definition: MSDispatch.cpp:125
std::map< std::string, std::vector< Reservation * > > myGroupReservations
Definition: MSDispatch.h:185
virtual Reservation * addReservation(MSTransportable *person, SUMOTime reservationTime, SUMOTime pickupTime, const MSEdge *from, double fromPos, const MSEdge *to, double toPos, std::string group, const std::string &line, int maxCapacity, int maxContainerCapacity)
add a new reservation
Definition: MSDispatch.cpp:61
int myReservationCount
Definition: MSDispatch.h:184
virtual void fulfilledReservation(const Reservation *res)
erase reservation from storage
Definition: MSDispatch.cpp:207
void servedReservation(const Reservation *res)
Definition: MSDispatch.cpp:185
std::set< const Reservation * > myRunningReservations
Definition: MSDispatch.h:179
A road/street connecting two junctions.
Definition: MSEdge.h:77
const std::string & getID() const
Returns the id.
Definition: Named.h:74
void remove(const std::string str, const T key)
bool has(const T key) const
bool hasString(const std::string &str) const
T get(const std::string &str) const
void insert(const std::string str, const T key, bool checkDuplicates=true)
SUMOTime pickupTime
Definition: MSDispatch.h:72
std::string id
Definition: MSDispatch.h:69
const MSEdge * to
Definition: MSDispatch.h:75
double fromPos
Definition: MSDispatch.h:74
std::string line
Definition: MSDispatch.h:78
const MSEdge * from
Definition: MSDispatch.h:73
SUMOTime reservationTime
Definition: MSDispatch.h:71
std::string group
Definition: MSDispatch.h:77
std::set< MSTransportable * > persons
Definition: MSDispatch.h:70
double toPos
Definition: MSDispatch.h:76