Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
RailwayRouter.h
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2024 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// The RailwayRouter builds a special network for railway routing to handle train reversal restrictions (delegates to a SUMOAbstractRouter)
19/****************************************************************************/
20#pragma once
21#include <config.h>
22
23#include <string>
24#include <vector>
25#include <algorithm>
26#include <assert.h>
27#ifdef HAVE_FOX
29#endif
34#include "SUMOAbstractRouter.h"
35#include "DijkstraRouter.h"
36#include "RailEdge.h"
37
38
39//#define RailwayRouter_DEBUG_ROUTES
40
41// ===========================================================================
42// class definitions
43// ===========================================================================
66template<class E, class V>
67class RailwayRouter : public SUMOAbstractRouter<E, V> {
68
69private:
70
71
75
76public:
77
79 RailwayRouter(const std::vector<E*>& edges, bool unbuildIsWarning, typename SUMOAbstractRouter<E, V>::Operation effortOperation,
80 typename SUMOAbstractRouter<E, V>::Operation ttOperation = nullptr, bool silent = false,
81 const bool havePermissions = false, const bool haveRestrictions = false, double maxTrainLength = 5000) :
82 SUMOAbstractRouter<E, V>("RailwayRouter", unbuildIsWarning, effortOperation, ttOperation, havePermissions, haveRestrictions),
83 myInternalRouter(nullptr), myOriginal(nullptr), mySilent(silent),
84 myMaxTrainLength(maxTrainLength) {
85 myStaticOperation = effortOperation;
86 for (const E* const edge : edges) {
87 myInitialEdges.push_back(edge->getRailwayRoutingEdge());
88 }
89 }
90
92 virtual ~RailwayRouter() {
93 delete myInternalRouter;
94 }
95
97 return new RailwayRouter<E, V>(this);
98 }
99
102 bool compute(const E* from, const E* to, const V* const vehicle, SUMOTime msTime, std::vector<const E*>& into, bool silent = false) {
104 if (vehicle->getLength() > myMaxTrainLength) {
105 WRITE_WARNINGF("Vehicle '%' with length % exceeds configured value of --railway.max-train-length %",
106 vehicle->getID(), toString(vehicle->getLength()), toString(myMaxTrainLength));
107 }
108 return _compute(from, to, vehicle, msTime, into, silent, false);
109 }
110
111 void prohibit(const std::vector<E*>& toProhibit) {
113 std::vector<_RailEdge*> railEdges;
114 for (E* edge : toProhibit) {
115 railEdges.push_back(edge->getRailwayRoutingEdge());
116 }
117 myInternalRouter->prohibit(railEdges);
118 this->myProhibited = toProhibit;
119#ifdef RailwayRouter_DEBUG_ROUTES
120 std::cout << "RailRouter prohibit=" << toString(toProhibit) << "\n";
121#endif
122 }
123
124
125 double recomputeCosts(const std::vector<const E*>& edges, const V* const v, SUMOTime msTime, double* lengthp = nullptr) const {
126 double effort = SUMOAbstractRouter<E, V>::recomputeCosts(edges, v, msTime, lengthp);
127 const E* prev = nullptr;
128 // reversal corrections
129 double timeCorrection = STEPS2TIME(msTime);
130 for (const E* const e : edges) {
131 if (prev != nullptr && e->getBidiEdge() == prev) {
132 if (e->getLength() > v->getLength()) {
133 // vehicle doesn't need to drive to the end of the edge
134 // @note exact timing correction for time-dependent effort is not done
135 const double savingsFactor = 1 - v->getLength() / e->getLength();
136 double effortCorrection = 0;
137 double lengthCorrection = 0.;
138 effortCorrection += this->getEffort(prev, v, timeCorrection);
139 this->updateViaCost(prev, e, v, timeCorrection, effortCorrection, lengthCorrection);
140 effort -= savingsFactor * effortCorrection;
141 if (lengthp != nullptr) {
142 *lengthp -= savingsFactor * lengthCorrection;
143 }
144 }
145 }
146 prev = e;
147 }
148 return effort;
149 }
150
151
152private:
154 SUMOAbstractRouter<E, V>(other),
155 myInternalRouter(nullptr),
156 myOriginal(other),
157 mySilent(other->mySilent),
159 {}
160
167
168 bool _compute(const E* from, const E* to, const V* const vehicle, SUMOTime msTime, std::vector<const E*>& into, bool silent, bool avoidUnsafeBackTracking) {
169 // make sure that the vehicle can turn-around when starting on a short edge (the virtual turn-around for this lies backwards along the route / track)
170 std::vector<double> backLengths;
171 double backDist = vehicle->getLength() - from->getLength();
172 const E* start = from;
173 while (backDist > 0) {
174 const E* prev = getStraightPredecessor(start, into, (int)backLengths.size());
175 if (prev == nullptr) {
176#ifdef RailwayRouter_DEBUG_ROUTES
177 std::cout << " Could not determine back edge for vehicle '" << vehicle->getID() << "' when routing from edge '" << from->getID() << "' at time=" << time2string(msTime) << "\n";
178#endif
179 //WRITE_WARNING("Could not determine back edge for vehicle '" + vehicle->getID() + "' when routing from edge '" + from->getID() + "' at time=" + time2string(msTime));
180 break;
181 }
182 backDist -= prev->getLength();
183 if (avoidUnsafeBackTracking && prev->getSuccessors().size() > 1) {
184 bool foundSwitch = false;
185 for (const E* succ : prev->getSuccessors()) {
186 if (succ != start && succ != prev->getBidiEdge()) {
187 foundSwitch = true;
188 break;
189 }
190 }
191 if (foundSwitch) {
192 break;
193 }
194 }
195 backLengths.push_back(prev->getLength() + (backLengths.empty()
196 ? MIN2(vehicle->getLength(), from->getLength())
197 : backLengths.back()));
198 start = prev;
199 }
200
201 std::vector<const _RailEdge*> intoTmp;
202 bool success = myInternalRouter->compute(start->getRailwayRoutingEdge(), to->getRailwayRoutingEdge(), vehicle, msTime, intoTmp, silent);
203#ifdef RailwayRouter_DEBUG_ROUTES
204 std::cout << "RailRouter veh=" << vehicle->getID() << " from=" << from->getID() << " to=" << to->getID() << " t=" << time2string(msTime)
205 << " safe=" << avoidUnsafeBackTracking << " success=" << success << " into=" << toString(into) << "\n";
206#endif
207 if (success) {
208 const size_t intoSize = into.size();
209 int backIndex = (int)backLengths.size();
210 for (const _RailEdge* railEdge : intoTmp) {
211 if (railEdge->getOriginal() != nullptr) {
212 backIndex--;
213 }
214 // prevent premature reversal on back edge (extend train length)
215 const double length = backIndex >= 0 ? backLengths[backIndex] : vehicle->getLength();
216 railEdge->insertOriginalEdges(length, into);
217 }
218#ifdef RailwayRouter_DEBUG_ROUTES
219 std::cout << "RailRouter: internal result=" << toString(intoTmp) << "\n";
220 std::cout << "RailRouter: expanded result=" << toString(into) << "\n";
221#endif
222 if (backLengths.size() > 0) {
223 // skip the virtual back-edges
224 into.erase(into.begin() + intoSize, into.begin() + intoSize + backLengths.size());
225#ifdef RailwayRouter_DEBUG_ROUTES
226 std::cout << "RailRouter: backLengths=" << toString(backLengths) << " intoSize=" << intoSize << " final result=" << toString(into) << "\n";
227#endif
228 if (*(into.begin() + intoSize) != from) {
229 if (!avoidUnsafeBackTracking) {
230 // try again, this time with more safety (but unable to
231 // make use of turn-arounds on short edge)
232 into.erase(into.begin() + intoSize, into.end());
233 return _compute(from, to, vehicle, msTime, into, silent, true);
234 } else {
235 WRITE_WARNING("Railway routing failure due to turn-around on short edge '" + from->getID()
236 + "' for vehicle '" + vehicle->getID() + "' time=" + time2string(msTime) + ".");
237 }
238 }
239 }
240 if (this->myProhibited.size() > 0) {
241 // make sure that turnarounds don't use prohibited edges
242 for (const E* e : into) {
243 if (std::find(this->myProhibited.begin(), this->myProhibited.end(), e) != this->myProhibited.end()) {
244 into.clear();
245 success = false;
246 break;
247 }
248 }
249 }
250 }
251 return success;
252
253 }
254
255 const std::vector<_RailEdge*>& getRailEdges() {
256 if (myOriginal != nullptr) {
257 return myOriginal->getRailEdges();
258 }
259#ifdef HAVE_FOX
260 FXMutexLock locker(myLock);
261#endif
262 if (myRailEdges.empty()) {
264 int numericalID = myInitialEdges.back()->getNumericalID() + 1;
265 for (_RailEdge* railEdge : myInitialEdges) {
266 railEdge->init(myRailEdges, numericalID, myMaxTrainLength);
267 }
268 }
269 return myRailEdges;
270 }
271
272 static inline double getTravelTimeStatic(const RailEdge<E, V>* const edge, const V* const veh, double time) {
273 if (edge->getOriginal() != nullptr) {
274 return (*myStaticOperation)(edge->getOriginal(), veh, time);
275 } else {
276 // turnaround edge
277 if (edge->isVirtual()) {
278 // add up time for replacement edges
279 std::vector<const E*> repl;
280 edge->insertOriginalEdges(veh->getLength(), repl);
281 assert(repl.size() > 0);
282 double seenDist = 0;
283 double result = 0;
284 repl.pop_back(); // last edge must not be used fully
285 for (const E* e : repl) {
286 result += (*myStaticOperation)(e, veh, time + result);
287 seenDist += e->getLength();
288 }
289 const double lengthOnLastEdge = MAX2(0.0, veh->getLength() - seenDist);
290 return result + myReversalPenalty + lengthOnLastEdge * myReversalPenaltyFactor;
291 } else {
292 // if the edge from which this turnaround starts is longer
293 // than the vehicle then we are overstimating the cost
294 // (because the turnaround may happen before driving to the end)
295 // However, unless the vehicle starts on this edge, we should be taking a
296 // virtual turnaround at the end of the previous edge. Otherwise, the exaggerated cost doesn't
297 return myReversalPenalty;
298 }
299 }
300 }
301
302
303 static const E* getStraightPredecessor(const E* edge, std::vector<const E*>& prevRoute, int backIndex) {
304 const E* result = nullptr;
305#ifdef RailwayRouter_DEBUG_ROUTES
306 std::cout << " getStraightPredecessor edge=" << edge->getID() << " prevRouteSize=" << prevRoute.size() << " backIndex=" << backIndex << "\n";
307#endif
308 if ((int)prevRoute.size() > backIndex) {
309 return prevRoute[(int)prevRoute.size() - 1 - backIndex];
310 }
311 for (const E* cand : edge->getPredecessors()) {
312 if (!cand->isInternal() && cand->getBidiEdge() != edge) {
313 //std::cout << " cand=" << cand->getID() << "\n";
314 if (result == nullptr) {
315 result = cand;
316 } else {
317 // predecessor not unique. Better abort with a warning
318 return nullptr;
319 }
320 }
321 }
322 return result;
323 }
324
325private:
329 std::vector<_RailEdge*> myInitialEdges;
331 std::vector<_RailEdge*> myRailEdges;
332
334 const bool mySilent;
335
336 const double myMaxTrainLength;
337
338#ifdef HAVE_FOX
340 mutable FXMutex myLock;
341#endif
342
345
346 static double myReversalPenalty;
348
349private:
352
353};
354
355template<class E, class V>
357template<class E, class V>
359template<class E, class V>
long long int SUMOTime
Definition GUI.h:36
#define WRITE_WARNINGF(...)
Definition MsgHandler.h:296
#define WRITE_WARNING(msg)
Definition MsgHandler.h:295
std::string time2string(SUMOTime t, bool humanReadable)
convert SUMOTime to string (independently of global format setting)
Definition SUMOTime.cpp:69
#define STEPS2TIME(x)
Definition SUMOTime.h:55
T MIN2(T a, T b)
Definition StdDefs.h:76
T MAX2(T a, T b)
Definition StdDefs.h:82
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:46
Computes the shortest path through a network using the Dijkstra algorithm.
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
the edge type representing backward edges
Definition RailEdge.h:38
void insertOriginalEdges(double length, std::vector< const E * > &into) const
Definition RailEdge.h:181
bool isVirtual() const
Definition RailEdge.h:262
const E * getOriginal() const
Returns the original edge.
Definition RailEdge.h:170
SUMOAbstractRouter< _RailEdge, V > _InternalRouter
static double myReversalPenaltyFactor
RailwayRouter< E, V > *const myOriginal
RailwayRouter(RailwayRouter *other)
DijkstraRouter< _RailEdge, V > _InternalDijkstra
std::vector< _RailEdge * > myInitialEdges
a RailEdge for every existing edge, filled on construction (but not in clones)
RailwayRouter & operator=(const RailwayRouter &s)
Invalidated assignment operator.
std::vector< _RailEdge * > myRailEdges
complete rail network filled on demand (but not in clones)
double recomputeCosts(const std::vector< const E * > &edges, const V *const v, SUMOTime msTime, double *lengthp=nullptr) const
const bool mySilent
whether to suppress warning/error if no route was found
bool compute(const E *from, const E *to, const V *const vehicle, SUMOTime msTime, std::vector< const E * > &into, bool silent=false)
Builds the route between the given edges using the minimum effort at the given time The definition of...
RailwayRouter(const std::vector< E * > &edges, bool unbuildIsWarning, typename SUMOAbstractRouter< E, V >::Operation effortOperation, typename SUMOAbstractRouter< E, V >::Operation ttOperation=nullptr, bool silent=false, const bool havePermissions=false, const bool haveRestrictions=false, double maxTrainLength=5000)
Constructor.
_InternalRouter * myInternalRouter
static SUMOAbstractRouter< E, V >::Operation myStaticOperation
The object's operation to perform. (hack)
const double myMaxTrainLength
bool _compute(const E *from, const E *to, const V *const vehicle, SUMOTime msTime, std::vector< const E * > &into, bool silent, bool avoidUnsafeBackTracking)
void ensureInternalRouter()
const std::vector< _RailEdge * > & getRailEdges()
RailEdge< E, V > _RailEdge
static double myReversalPenalty
static const E * getStraightPredecessor(const E *edge, std::vector< const E * > &prevRoute, int backIndex)
static double getTravelTimeStatic(const RailEdge< E, V > *const edge, const V *const veh, double time)
SUMOAbstractRouter< E, V > * clone()
virtual ~RailwayRouter()
Destructor.
void prohibit(const std::vector< E * > &toProhibit)
MsgHandler *const myErrorMsgHandler
the handler for routing errors
std::vector< E * > myProhibited
The list of explicitly prohibited edges.
const bool myHavePermissions
whether edge permissions need to be considered
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
const bool myHaveRestrictions
whether edge restrictions need to be considered
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...
virtual double recomputeCosts(const std::vector< const E * > &edges, const V *const v, SUMOTime msTime, double *lengthp=nullptr) const