Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
MSDispatch_RouteExtension.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>
23#include <microsim/MSNet.h>
24#include <microsim/MSEdge.h>
26#include "MSRoutingEngine.h"
28
29//#define DEBUG_DISPATCH
30//#define DEBUG_COND2(obj) (obj->getID() == "p0")
31#define DEBUG_COND2(obj) (true)
32
33
34// ===========================================================================
35// method definitions
36// ===========================================================================
37void
38MSDispatch_RouteExtension::findInsertionPoint(std::vector<const Reservation*>::iterator& resIt, EdgePosVector::iterator& edgeIt,
39 const EdgePosVector::iterator& edgeEnd, ConstMSEdgeVector& route,
40 const MSEdge* newEdge, const double newPos) const {
41 for (const MSEdge* edge : route) {
42 while (edgeIt != edgeEnd && edge == edgeIt->first) {
43 if (edge == newEdge && edgeIt->second > newPos) {
44 break;
45 }
46 resIt++;
47 edgeIt++;
48 }
49 if (edge == newEdge) {
50 break;
51 }
52 }
53}
54
55
56int
57MSDispatch_RouteExtension::dispatch(MSDevice_Taxi* taxi, std::vector<Reservation*>::iterator& resIt, SUMOAbstractRouter<MSEdge, SUMOVehicle>& router, std::vector<Reservation*>& reservations) {
58 const Reservation* const res = *resIt;
59#ifdef DEBUG_DISPATCH
60 if (DEBUG_COND2(person)) {
61 std::cout << SIMTIME << " dispatch taxi=" << taxi->getHolder().getID() << " person=" << toString(res->persons) << "\n";
62 }
63#endif
65 const bool isPerson = (*res->persons.begin())->isPerson();
66 int capacityLeft = remainingCapacity(taxi, res);
67 std::vector<const Reservation*> sequence{ res, res };
68 std::vector<const Reservation*> toRemove{ res };
69 EdgePosVector posSequence({ std::make_pair(res->from, res->fromPos), std::make_pair(res->to, res->toPos) });
70 const Reservation* first = sequence.front();
71 const Reservation* last = sequence.back();
73 router.compute(first->from, first->fromPos, last->to, last->toPos, &taxi->getHolder(), MAX2(now, first->pickupTime), route);
74 // check whether the ride can be shared
75 for (auto it2 = resIt + 1; it2 != reservations.end();) {
76 Reservation* const res2 = *it2;
77 const bool isPerson2 = (*res2->persons.begin())->isPerson();
78 if (capacityLeft < (int)res2->persons.size() || isPerson != isPerson2 || !taxi->compatibleLine(res2)) {
79 it2++;
80 continue;
81 }
82 // check whether res2 picks up or gets picked up on the way
83 ConstMSEdgeVector route2;
84 // TODO It may be more efficient to check first whether from and to are already in the route
85 router.compute(res2->from, res2->fromPos, res2->to, res2->toPos, &taxi->getHolder(), MAX2(now, res2->pickupTime), route2);
86 const bool pickup = std::find(route.begin(), route.end(), res2->from) != route.end();
87 const bool dropoff = std::find(route.begin(), route.end(), res2->to) != route.end();
88 const bool pickup2 = std::find(route2.begin(), route2.end(), first->from) != route2.end();
89 const bool dropoff2 = std::find(route2.begin(), route2.end(), last->to) != route2.end();
90#ifdef DEBUG_DISPATCH
91 if (DEBUG_COND2(person)) std::cout << " consider sharing ride with " << toString(res2->persons)
92 << " from=" << res2->from->getID() << ":" << res2->fromPos << " to=" << res2->to->getID() << ":" << res2->toPos
93 << " pickup=" << pickup << " startFirst=" << pickup2
94 << " dropoff=" << dropoff << " endLast=" << dropoff2
95 << "\n";
96#endif
97 if ((pickup || pickup2) && (dropoff || dropoff2)) {
98 std::vector<const Reservation*>::iterator resSeqIt = sequence.begin();
99 EdgePosVector::iterator edgeIt = posSequence.begin();
100 if (pickup) {
101 // new reservation gets picked up
102 findInsertionPoint(resSeqIt, edgeIt, posSequence.end(), route, res2->from, res2->fromPos);
103 }
104 resSeqIt = sequence.insert(resSeqIt, res2) + 1;
105 edgeIt = posSequence.insert(edgeIt, std::make_pair(res2->from, res2->fromPos)) + 1;
106 if (dropoff) {
107 // new reservation drops off and route continues
108 findInsertionPoint(resSeqIt, edgeIt, posSequence.end(), route, res2->to, res2->toPos);
109 sequence.insert(resSeqIt, res2);
110 posSequence.insert(edgeIt, std::make_pair(res2->to, res2->toPos));
111 } else {
112 // new reservation ends last
113 sequence.push_back(res2);
114 posSequence.push_back(std::make_pair(res2->to, res2->toPos));
115 }
116 toRemove.push_back(res2);
117 it2 = reservations.erase(it2); // (resIt before it2) stays valid
118 // TODO we have more capacity if some pickup is after an earlier dropoff
119 capacityLeft -= (int)res2->persons.size();
120 if (capacityLeft == 0) {
121 break;
122 }
123 route.clear();
124 first = sequence.front();
125 last = sequence.back();
126 // TODO this is wrong for non linear networks! should be reusing the route snippets from above
127 router.compute(first->from, first->fromPos, last->to, last->toPos, &taxi->getHolder(), MAX2(now, first->pickupTime), route);
128 } else {
129 it2++;
130 }
131 }
132 if (sequence.size() > 2) {
133 taxi->dispatchShared(sequence);
134 if (myOutput != nullptr) {
135 myOutput->writeXMLHeader("DispatchInfo_RouteExtension", "");
136 myOutput->openTag("dispatchShared");
137 myOutput->writeAttr("time", time2string(now));
138 myOutput->writeAttr("id", taxi->getHolder().getID());
139 myOutput->writeAttr("persons", toString(res->persons));
140 myOutput->writeAttr("sharingPersons", toString(sequence));
141 myOutput->writeAttr("type", "routeExtension");
143 }
144#ifdef DEBUG_DISPATCH
145 if (DEBUG_COND2(person)) std::cout << " sharing ride with " << toString(sequence)
146 << "\n";
147#endif
148 } else {
149 taxi->dispatch(*res);
150 }
151 for (const Reservation* r : toRemove) {
152 servedReservation(r, taxi); // deleting r
153 }
154 resIt = reservations.erase(resIt);
155 return (int)toRemove.size();
156}
157
158
159/****************************************************************************/
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
std::string time2string(SUMOTime t, bool humanReadable)
convert SUMOTime to string (independently of global format setting)
Definition SUMOTime.cpp:91
#define SIMTIME
Definition SUMOTime.h:62
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 dispatch(const Reservation &res)
service the given reservation
void dispatchShared(std::vector< const Reservation * > reservations)
service the given reservations
bool compatibleLine(const Reservation *res)
whether the given reservation is compatible with the taxi line
void findInsertionPoint(std::vector< const Reservation * >::iterator &resIt, EdgePosVector::iterator &edgeIt, const EdgePosVector::iterator &edgeEnd, ConstMSEdgeVector &route, const MSEdge *newEdge, const double newPos) const
virtual int dispatch(MSDevice_Taxi *taxi, std::vector< Reservation * >::iterator &resIt, SUMOAbstractRouter< MSEdge, SUMOVehicle > &router, std::vector< Reservation * > &reservations)
trigger taxi dispatch.
std::vector< std::pair< const MSEdge *, double > > EdgePosVector
OutputDevice * myOutput
optional file output for dispatch information
Definition MSDispatch.h:206
int remainingCapacity(const MSDevice_Taxi *taxi, const Reservation *res)
whether the given taxi has sufficient capacity to serve the reservation
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:186
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition MSNet.h:334
SUMOVehicle & getHolder() const
Returns the vehicle that holds this device.
const std::string & getID() const
Returns the id.
Definition Named.h:74
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
bool writeXMLHeader(const std::string &rootElement, const std::string &schemaFile, std::map< SumoXMLAttr, std::string > attrs=std::map< SumoXMLAttr, std::string >(), bool includeConfig=true)
Writes an XML header with optional configuration.
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...
SUMOTime pickupTime
Definition MSDispatch.h:79
const MSEdge * to
Definition MSDispatch.h:84
double fromPos
Definition MSDispatch.h:82
const MSEdge * from
Definition MSDispatch.h:81
std::set< const MSTransportable * > persons
Definition MSDispatch.h:77
double toPos
Definition MSDispatch.h:85