Eclipse SUMO - Simulation of Urban MObility
IntermodalRouter.h
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3// Copyright (C) 2001-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/****************************************************************************/
19// The IntermodalRouter builds a special network and (delegates to a SUMOAbstractRouter)
20/****************************************************************************/
21#pragma once
22#include <config.h>
23
24#include <string>
25#include <vector>
26#include <algorithm>
27#include <assert.h>
32#include "SUMOAbstractRouter.h"
33#include "DijkstraRouter.h"
34#include "AStarRouter.h"
35#include "IntermodalNetwork.h"
36#include "EffortCalculator.h"
37#include "CarEdge.h"
38#include "StopEdge.h"
39#include "PedestrianRouter.h"
40
41//#define IntermodalRouter_DEBUG_ROUTES
42
43
44// ===========================================================================
45// class definitions
46// ===========================================================================
51template<class E, class L, class N, class V>
52class IntermodalRouter : public SUMOAbstractRouter<E, IntermodalTrip<E, N, V> > {
53public:
55
56private:
63
64public:
65 struct TripItem {
66 TripItem(const std::string& _line = "") :
67 line(_line), intended(_line) {}
68 std::string line;
69 std::string vType = "";
70 std::string destStop = "";
71 std::string intended; // intended public transport vehicle id
72 double depart = -1.; // intended public transport departure
73 std::vector<const E*> edges;
74 double traveltime = 0.;
75 double cost = 0.;
76 double length = 0.;
79 std::string description = "";
80 std::vector<double> exitTimes;
81 };
82
84 IntermodalRouter(CreateNetCallback callback, const int carWalkTransfer, double taxiWait, const std::string& routingAlgorithm,
85 const int routingMode = 0, EffortCalculator* calc = nullptr) :
86 SUMOAbstractRouter<E, _IntermodalTrip>("IntermodalRouter", true, nullptr, nullptr, false, false),
87 myAmClone(false), myInternalRouter(nullptr), myIntermodalNet(nullptr),
88 myCallback(callback), myCarWalkTransfer(carWalkTransfer), myTaxiWait(taxiWait),
89 myRoutingAlgorithm(routingAlgorithm),
90 myRoutingMode(routingMode), myExternalEffort(calc) {
91 }
92
95 delete myInternalRouter;
96 if (!myAmClone) {
97 delete myIntermodalNet;
98 }
99 }
100
102 createNet();
104 }
105
106 int getCarWalkTransfer() const {
107 return myCarWalkTransfer;
108 }
109
112 bool compute(const E* from, const E* to,
113 const double departPos, const std::string& originStopID,
114 const double arrivalPos, const std::string& stopID,
115 const double speed, const V* const vehicle, const SVCPermissions modeSet, const SUMOTime msTime,
116 std::vector<TripItem>& into, const double externalFactor = 0.) {
117 createNet();
118 _IntermodalTrip trip(from, to, departPos, arrivalPos, speed, msTime, 0, vehicle, modeSet, myExternalEffort, externalFactor);
119 std::vector<const _IntermodalEdge*> intoEdges;
120 //std::cout << "compute from=" << from->getID() << " to=" << to->getID() << " dPos=" << departPos << " aPos=" << arrivalPos << " stopID=" << stopID << " speed=" << speed << " veh=" << Named::getIDSecure(vehicle) << " modeSet=" << modeSet << " t=" << msTime << " iFrom=" << myIntermodalNet->getDepartEdge(from, trip.departPos)->getID() << " iTo=" << (stopID != "" ? myIntermodalNet->getStopEdge(stopID) : myIntermodalNet->getArrivalEdge(to, trip.arrivalPos))->getID() << "\n";
121 const _IntermodalEdge* iFrom = originStopID != "" ? myIntermodalNet->getStopEdge(originStopID) : myIntermodalNet->getDepartEdge(from, trip.departPos);
122 const _IntermodalEdge* iTo = stopID != "" ? myIntermodalNet->getStopEdge(stopID) : myIntermodalNet->getArrivalEdge(to, trip.arrivalPos);
123 const bool success = myInternalRouter->compute(iFrom, iTo, &trip, msTime, intoEdges);
124 if (success) {
125 std::string lastLine = "";
126 double time = STEPS2TIME(msTime);
127 double effort = 0.;
128 double length = 0.;
129 const _IntermodalEdge* prev = nullptr;
130 for (const _IntermodalEdge* iEdge : intoEdges) {
131 bool addedEdge = false;
132 if (iEdge->includeInRoute(false)) {
133 if (iEdge->getLine() == "!stop") {
134 if (into.size() > 0) {
135 // previous stage ends at stop
136 into.back().destStop = iEdge->getID();
137 if (myExternalEffort != nullptr) {
138 into.back().description = myExternalEffort->output(iEdge->getNumericalID());
139 }
140 if (lastLine == "!ped") {
141 lastLine = ""; // a stop always starts a new trip item
142 }
143 } else {
144 // trip starts at stop
145 lastLine = "";
146 into.push_back(TripItem("!stop"));
147 into.back().destStop = iEdge->getID();
148 }
149 } else {
150 if (iEdge->getLine() != lastLine) {
151 lastLine = iEdge->getLine();
152 if (lastLine == "!car") {
153 into.push_back(TripItem(vehicle->getID()));
154 into.back().vType = vehicle->getParameter().vtypeid;
155 } else if (lastLine == "!ped") {
156 into.push_back(TripItem());
157 } else {
158 into.push_back(TripItem(lastLine));
159 into.back().depart = iEdge->getIntended(time, into.back().intended);
160 }
161 into.back().departPos = iEdge->getStartPos();
162 }
163 if (into.back().edges.empty() || into.back().edges.back() != iEdge->getEdge()) {
164 into.back().edges.push_back(iEdge->getEdge());
165 into.back().arrivalPos = iEdge->getEndPos();
166 addedEdge = true;
167 }
168 }
169 }
170 const double prevTime = time, prevEffort = effort, prevLength = length;
171 myInternalRouter->updateViaCost(prev, iEdge, &trip, time, effort, length);
172 // correct intermodal length:
173 length += iEdge->getPartialLength(&trip) - iEdge->getLength();
174 prev = iEdge;
175 if (!into.empty()) {
176 into.back().traveltime += time - prevTime;
177 into.back().cost += effort - prevEffort;
178 into.back().length += length - prevLength;
179 if (into.back().depart < 0) {
180 into.back().depart = prevTime;
181 }
182 if (addedEdge) {
183 into.back().exitTimes.push_back(time);
184 }
185 }
186 }
187 }
188 if (into.size() > 0) {
189 into.back().arrivalPos = arrivalPos;
190 }
191#ifdef IntermodalRouter_DEBUG_ROUTES
192 double time = STEPS2TIME(msTime);
193 for (const _IntermodalEdge* iEdge : intoEdges) {
194 const double edgeEffort = myInternalRouter->getEffort(iEdge, &trip, time);
195 time += edgeEffort;
196 std::cout << iEdge->getID() << "(" << iEdge->getLine() << "): " << edgeEffort << " l=" << iEdge->getLength() << " pL=" << iEdge->getPartialLength(&trip) << "\n";
197 }
198 std::cout << TIME2STEPS(msTime) << " trip from " << from->getID() << " to " << (to != nullptr ? to->getID() : stopID)
199 << " departPos=" << trip.departPos
200 << " arrivalPos=" << trip.arrivalPos
201 << " modes=" << getVehicleClassNames(modeSet)
202 << " edges=" << toString(intoEdges)
203// << " resultEdges=" << toString(into)
204 << " time=" << time
205 << "\n";
206#endif
207 return success;
208 }
209
212 bool compute(const E*, const E*, const _IntermodalTrip* const,
213 SUMOTime, std::vector<const E*>&, bool) {
214 throw ProcessError("Do not use this method");
215 }
216
217 inline void setBulkMode(const bool mode) {
219 if (myInternalRouter != nullptr) {
221 }
222 }
223
224 void prohibit(const std::vector<E*>& toProhibit) {
225 createNet();
226 std::vector<_IntermodalEdge*> toProhibitPE;
227 for (typename std::vector<E*>::const_iterator it = toProhibit.begin(); it != toProhibit.end(); ++it) {
228 toProhibitPE.push_back(myIntermodalNet->getBothDirections(*it).first);
229 toProhibitPE.push_back(myIntermodalNet->getBothDirections(*it).second);
230 toProhibitPE.push_back(myIntermodalNet->getCarEdge(*it));
231 }
232 myInternalRouter->prohibit(toProhibitPE);
233 }
234
236 createNet();
239 dev.writeAttr(SUMO_ATTR_ID, e->getID());
240 dev.writeAttr(SUMO_ATTR_LINE, e->getLine());
241 dev.writeAttr(SUMO_ATTR_LENGTH, e->getLength());
242 dev.writeAttr("successors", toString(e->getSuccessors(SVC_IGNORING)));
243 dev.closeTag();
244 }
245 }
246
248 createNet();
249 _IntermodalTrip trip(nullptr, nullptr, 0., 0., DEFAULT_PEDESTRIAN_SPEED, 0, 0, nullptr, SVC_PASSENGER | SVC_BICYCLE | SVC_BUS);
252 dev.writeAttr(SUMO_ATTR_ID, e->getID());
253 dev.writeAttr("traveltime", e->getTravelTime(&trip, 0.));
254 dev.writeAttr("effort", e->getEffort(&trip, 0.));
255 dev.closeTag();
256 }
257 }
258
260 return myIntermodalNet;
261 }
262
264 return myExternalEffort;
265 }
266
267private:
268 IntermodalRouter(Network* net, const int carWalkTransfer, double taxiWait, const std::string& routingAlgorithm,
269 const int routingMode, EffortCalculator* calc) :
270 SUMOAbstractRouter<E, _IntermodalTrip>("IntermodalRouterClone", true, nullptr, nullptr, false, false),
271 myAmClone(true), myInternalRouter(nullptr), myIntermodalNet(net),
272 myCarWalkTransfer(carWalkTransfer),
273 myTaxiWait(taxiWait),
274 myRoutingAlgorithm(routingAlgorithm), myRoutingMode(routingMode), myExternalEffort(calc) {
275 createNet();
276 }
277
278 static inline double getCombined(const _IntermodalEdge* const edge, const _IntermodalTrip* const trip, double time) {
279 return edge->getTravelTime(trip, time) + trip->externalFactor * trip->calc->getEffort(edge->getNumericalID());
280 }
281
282 inline void createNet() {
283 if (myIntermodalNet == nullptr) {
284 myIntermodalNet = new Network(E::getAllEdges(), false, myCarWalkTransfer);
285 myIntermodalNet->addCarEdges(E::getAllEdges(), myTaxiWait);
286 myCallback(*this);
287 }
288 if (myInternalRouter == nullptr) {
289 switch (myRoutingMode) {
290 case 0:
291 if (myRoutingAlgorithm == "astar") {
294 } else {
297 }
298 break;
299 case 1:
301 break;
302 case 2:
304 break;
305 case 3:
306 if (myExternalEffort != nullptr) {
307 std::vector<std::string> edgeLines;
308 for (const auto e : myIntermodalNet->getAllEdges()) {
309 edgeLines.push_back(e->getLine());
310 }
311 myExternalEffort->init(edgeLines);
312 }
314 break;
315 }
316 }
317 }
318
319private:
320 const bool myAmClone;
325 const double myTaxiWait;
326 const std::string myRoutingAlgorithm;
327 const int myRoutingMode;
329
330
331private:
334
335};
long long int SUMOTime
Definition: GUI.h:36
#define STEPS2TIME(x)
Definition: SUMOTime.h:54
#define TIME2STEPS(x)
Definition: SUMOTime.h:56
const std::string & getVehicleClassNames(SVCPermissions permissions, bool expand)
Returns the ids of the given classes, divided using a ' '.
@ SVC_IGNORING
vehicles ignoring classes
@ SVC_PASSENGER
vehicle is a passenger car (a "normal" car)
@ SVC_BICYCLE
vehicle is a bicycle
@ SVC_BUS
vehicle is a bus
const double DEFAULT_PEDESTRIAN_SPEED
int SVCPermissions
bitset where each bit declares whether a certain SVC may use this edge/lane
@ SUMO_TAG_EDGE
begin/end of the description of an edge
@ SUMO_ATTR_LINE
@ SUMO_ATTR_LENGTH
@ SUMO_ATTR_ID
double gWeightsRandomFactor
Definition: StdDefs.cpp:30
const double INVALID_DOUBLE
Definition: StdDefs.h:60
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
Computes the shortest path through a network using the A* algorithm.
Definition: AStarRouter.h:76
Computes the shortest path through a network using the Dijkstra algorithm.
the effort calculator interface
virtual double getEffort(const int numericalID) const =0
virtual std::string output(const int edge) const =0
virtual void init(const std::vector< std::string > &edges)=0
the base edge type that is given to the internal router (SUMOAbstractRouter)
static double getTravelTimeStatic(const IntermodalEdge *const edge, const IntermodalTrip< E, N, V > *const trip, double time)
virtual double getTravelTime(const IntermodalTrip< E, N, V > *const, double) const
int getNumericalID() const
static double getEffortStatic(const IntermodalEdge *const edge, const IntermodalTrip< E, N, V > *const trip, double time)
virtual double getTravelTimeAggregated(const IntermodalTrip< E, N, V > *const trip, double time) const
static double getTravelTimeStaticRandomized(const IntermodalEdge *const edge, const IntermodalTrip< E, N, V > *const trip, double time)
the intermodal network storing edges, connections and the mappings to the "real" edges
const std::vector< _IntermodalEdge * > & getAllEdges()
_IntermodalEdge * getCarEdge(const E *e) const
Returns the associated car edge.
_IntermodalEdge * getStopEdge(const std::string &stopId) const
Returns the associated stop edge.
const EdgePair & getBothDirections(const E *e) const
Returns the pair of forward and backward edge.
void addCarEdges(const std::vector< E * > &edges, double taxiWait)
_IntermodalEdge * getArrivalEdge(const E *e, const double pos) const
Returns the arriving intermodal edge.
const _IntermodalEdge * getDepartEdge(const E *e, const double pos) const
Returns the departing intermodal edge.
EffortCalculator * getExternalEffort() const
SUMOAbstractRouter< _IntermodalEdge, _IntermodalTrip > _InternalRouter
AStarRouter< _IntermodalEdge, _IntermodalTrip > _InternalAStar
IntermodalTrip< E, N, V > _IntermodalTrip
CreateNetCallback myCallback
static double getCombined(const _IntermodalEdge *const edge, const _IntermodalTrip *const trip, double time)
void(* CreateNetCallback)(IntermodalRouter< E, L, N, V > &)
IntermodalEdge< E, L, N, V > _IntermodalEdge
const int myCarWalkTransfer
Network * myIntermodalNet
virtual ~IntermodalRouter()
Destructor.
IntermodalNetwork< E, L, N, V > Network
IntermodalRouter(Network *net, const int carWalkTransfer, double taxiWait, const std::string &routingAlgorithm, const int routingMode, EffortCalculator *calc)
SUMOAbstractRouter< E, _IntermodalTrip > * clone()
IntermodalRouter(CreateNetCallback callback, const int carWalkTransfer, double taxiWait, const std::string &routingAlgorithm, const int routingMode=0, EffortCalculator *calc=nullptr)
Constructor.
bool compute(const E *, const E *, const _IntermodalTrip *const, SUMOTime, std::vector< const E * > &, bool)
Builds the route between the given edges using the minimum effort at the given time The definition of...
DijkstraRouter< _IntermodalEdge, _IntermodalTrip > _InternalDijkstra
const std::string myRoutingAlgorithm
IntermodalRouter & operator=(const IntermodalRouter &s)
Invalidated assignment operator.
const double myTaxiWait
void prohibit(const std::vector< E * > &toProhibit)
Network * getNetwork() const
void writeWeights(OutputDevice &dev)
void setBulkMode(const bool mode)
bool compute(const E *from, const E *to, const double departPos, const std::string &originStopID, const double arrivalPos, const std::string &stopID, const double speed, const V *const vehicle, const SVCPermissions modeSet, const SUMOTime msTime, std::vector< TripItem > &into, const double externalFactor=0.)
Builds the route between the given edges using the minimum effort at the given time The definition of...
EffortCalculator *const myExternalEffort
_InternalRouter * myInternalRouter
int getCarWalkTransfer() const
void writeNetwork(OutputDevice &dev)
the "vehicle" type that is given to the internal router (SUMOAbstractRouter)
const EffortCalculator *const calc
const double departPos
const double arrivalPos
const double externalFactor
const std::string & getID() const
Returns the id.
Definition: Named.h:74
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:251
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.
virtual void setBulkMode(const bool mode)
virtual void prohibit(const std::vector< E * > &toProhibit)
void updateViaCost(const E *const prev, const E *const e, const V *const v, double &time, double &effort, double &length) const
double getEffort(const E *const e, const V *const v, double t) const
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...
TripItem(const std::string &_line="")
std::vector< double > exitTimes
std::vector< const E * > edges