Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
DijkstraRouter.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-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/****************************************************************************/
20// Dijkstra shortest path algorithm using travel time or other values
21/****************************************************************************/
22#pragma once
23#include <config.h>
24
25#include <cassert>
26#include <string>
27#include <functional>
28#include <vector>
29#include <set>
30#include <limits>
31#include <algorithm>
32#include <iterator>
36#include "EffortCalculator.h"
37#include "SUMOAbstractRouter.h"
38
39//#define DijkstraRouter_DEBUG_QUERY
40//#define DijkstraRouter_DEBUG_QUERY_FRONTIER
41//#define DijkstraRouter_DEBUG_QUERY_VISITED
42//#define DijkstraRouter_DEBUG_QUERY_PERF
43//#define DijkstraRouter_DEBUG_BULKMODE
44
45#define DijkstraRouter_DEBUG_QUERY_VISITED_OUT std::cerr
46
47// ===========================================================================
48// class definitions
49// ===========================================================================
63template<class E, class V>
64class DijkstraRouter : public SUMOAbstractRouter<E, V> {
65public:
71 public:
73 bool operator()(const typename SUMOAbstractRouter<E, V>::EdgeInfo* nod1, const typename SUMOAbstractRouter<E, V>::EdgeInfo* nod2) const {
74 if (nod1->effort == nod2->effort) {
75 return nod1->edge->getNumericalID() > nod2->edge->getNumericalID();
76 }
77 return nod1->effort > nod2->effort;
78 }
79 };
80
81
83 DijkstraRouter(const std::vector<E*>& edges, bool unbuildIsWarning, typename SUMOAbstractRouter<E, V>::Operation effortOperation,
84 typename SUMOAbstractRouter<E, V>::Operation ttOperation = nullptr, bool silent = false, EffortCalculator* calc = nullptr,
85 const bool havePermissions = false, const bool haveRestrictions = false) :
86 SUMOAbstractRouter<E, V>("DijkstraRouter", unbuildIsWarning, effortOperation, ttOperation, havePermissions, haveRestrictions),
87 mySilent(silent), myExternalEffort(calc) {
88 for (typename std::vector<E*>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
89 this->myEdgeInfos.push_back(typename SUMOAbstractRouter<E, V>::EdgeInfo(*i));
90 }
91 }
92
94 virtual ~DijkstraRouter() { }
95
103
106 bool compute(const E* from, const E* to, const V* const vehicle,
107 SUMOTime msTime, std::vector<const E*>& into, bool silent = false) {
108 assert(from != nullptr && (vehicle == nullptr || to != nullptr));
109 // check whether from and to can be used
110 if (this->myEdgeInfos[from->getNumericalID()].prohibited || this->isProhibited(from, vehicle)) {
111 if (!silent) {
112 this->myErrorMsgHandler->inform("Vehicle '" + Named::getIDSecure(vehicle) + "' is not allowed on source edge '" + from->getID() + "'.");
113 }
114 return false;
115 }
116 if (to != nullptr && (this->myEdgeInfos[to->getNumericalID()].prohibited || this->isProhibited(to, vehicle))) {
117 if (!silent) {
118 this->myErrorMsgHandler->inform("Vehicle '" + Named::getIDSecure(vehicle) + "' is not allowed on destination edge '" + to->getID() + "'.");
119 }
120 return false;
121 }
122 double length = 0.; // dummy for the via edge cost update
123 this->startQuery();
124#ifdef DijkstraRouter_DEBUG_QUERY
125 std::cout << "DEBUG: starting search for '" << Named::getIDSecure(vehicle) << "' from '" << Named::getIDSecure(from) << "' to '" << Named::getIDSecure(to) << "' time: " << STEPS2TIME(msTime) << "\n";
126#endif
127 const SUMOVehicleClass vClass = vehicle == nullptr ? SVC_IGNORING : vehicle->getVClass();
128 const bool ignoreTransient = vehicle == nullptr ? false : vehicle->ignoreTransientPermissions();
129 std::tuple<const E*, const V*, SUMOTime> query = std::make_tuple(from, vehicle, msTime);
130 if ((this->myBulkMode || (this->myAutoBulkMode && query == myLastQuery)) && !this->myAmClean) {
131#ifdef DijkstraRouter_DEBUG_BULKMODE
132 if (query != myLastQuery) {
133 std::cout << " invalid bulk mode. myLastQuery="
134 << std::get<0>(myLastQuery)->getID() << ","
135 << std::get<1>(myLastQuery)->getID() << ","
136 << time2string(std::get<2>(myLastQuery))
137 << " query="
138 << std::get<0>(query)->getID() << ","
139 << std::get<1>(query)->getID() << ","
140 << time2string(std::get<2>(query))
141 << "\n";
142 }
143#endif
144 const auto& toInfo = this->myEdgeInfos[to->getNumericalID()];
145 if (toInfo.visited) {
146 this->buildPathFrom(&toInfo, into);
147 this->endQuery(1);
148 return true;
149 }
150 } else {
151 this->init(from->getNumericalID(), msTime);
152 if (myExternalEffort != nullptr) {
153 myExternalEffort->setInitialState(from->getNumericalID());
154 }
155 this->myAmClean = false;
156 }
157 myLastQuery = query;
158 // loop
159 int num_visited = 0;
160#ifdef DijkstraRouter_DEBUG_QUERY_VISITED
161 DijkstraRouter_DEBUG_QUERY_VISITED_OUT << "<edgeData>\n <interval begin=\"" << time2string(msTime) << "\" end=\"" << toString(msTime + 1000) << "\" id=\"" << Named::getIDSecure(vehicle) << "\">\n";
162#endif
163 while (!this->myFrontierList.empty()) {
164 num_visited += 1;
165 // use the node with the minimal length
166 auto* const minimumInfo = this->myFrontierList.front();
167 const E* const minEdge = minimumInfo->edge;
168#ifdef DijkstraRouter_DEBUG_QUERY
169 std::cout << "DEBUG: hit=" << minEdge->getID()
170 << " TT=" << minimumInfo->effort
171 << " EF=" << this->getEffort(minEdge, vehicle, minimumInfo->leaveTime)
172 << " Leave: " << minimumInfo->leaveTime << " Q: ";
173#ifdef DijkstraRouter_DEBUG_QUERY_FRONTIER
174 for (const auto& edgeInfo : this->myFrontierList) {
175 std::cout << "\n " << edgeInfo->effort << ", " << edgeInfo->edge->getID();
176 }
177#endif
178 std::cout << "\n";
179#endif
180#ifdef DijkstraRouter_DEBUG_QUERY_VISITED
181 DijkstraRouter_DEBUG_QUERY_VISITED_OUT << " <edge id=\"" << minEdge->getID() << "\" index=\"" << num_visited << "\" cost=\"" << minimumInfo->effort << "\" time=\"" << minimumInfo->leaveTime << "\"/>\n";
182#endif
183 // check whether the destination node was already reached
184 if (minEdge == to) {
185 //propagate last external effort state to destination edge
186 if (myExternalEffort != nullptr) {
187 myExternalEffort->update(minEdge->getNumericalID(), minimumInfo->prev->edge->getNumericalID(), minEdge->getLength());
188 }
189 this->buildPathFrom(minimumInfo, into);
190 this->endQuery(num_visited);
191#ifdef DijkstraRouter_DEBUG_QUERY_PERF
192 const double cost = this->recomputeCosts(into, vehicle, msTime);
193 std::cout << "visited " + toString(num_visited) + " edges (final path length=" + toString(into.size()) + " cost=" << cost << " edges=" + toString(into) + ")\n";
194#endif
195#ifdef DijkstraRouter_DEBUG_QUERY_VISITED
196 DijkstraRouter_DEBUG_QUERY_VISITED_OUT << " </interval>\n</edgeData>\n";
197#endif
198 return true;
199 }
200 std::pop_heap(this->myFrontierList.begin(), this->myFrontierList.end(), myComparator);
201 this->myFrontierList.pop_back();
202 this->myFound.push_back(minimumInfo);
203 minimumInfo->visited = true;
204 const double effortDelta = this->getEffort(minEdge, vehicle, minimumInfo->leaveTime);
205 const double leaveTime = minimumInfo->leaveTime + this->getTravelTime(minEdge, vehicle, minimumInfo->leaveTime, effortDelta);
206 if (myExternalEffort != nullptr) {
207 myExternalEffort->update(minEdge->getNumericalID(), minimumInfo->prev->edge->getNumericalID(), minEdge->getLength());
208 }
209 // check all ways from the node with the minimal length
210 for (const std::pair<const E*, const E*>& follower : minEdge->getViaSuccessors(vClass, ignoreTransient)) {
211 auto& followerInfo = this->myEdgeInfos[follower.first->getNumericalID()];
212 // check whether it can be used
213 if (followerInfo.prohibited || this->isProhibited(follower.first, vehicle)) {
214 continue;
215 }
216 double effort = minimumInfo->effort + effortDelta;
217 double time = leaveTime;
218 this->updateViaEdgeCost(follower.second, vehicle, time, effort, length);
219 assert(effort >= minimumInfo->effort);
220 assert(time >= minimumInfo->leaveTime);
221 const double oldEffort = followerInfo.effort;
222 if (!followerInfo.visited && effort < oldEffort) {
223 followerInfo.effort = effort;
224 followerInfo.leaveTime = time;
225 followerInfo.prev = minimumInfo;
226 if (oldEffort == std::numeric_limits<double>::max()) {
227 this->myFrontierList.push_back(&followerInfo);
228 std::push_heap(this->myFrontierList.begin(), this->myFrontierList.end(), myComparator);
229 } else {
230 std::push_heap(this->myFrontierList.begin(),
231 std::find(this->myFrontierList.begin(), this->myFrontierList.end(), &followerInfo) + 1,
233 }
234 }
235 }
236 }
237 this->endQuery(num_visited);
238#ifdef DijkstraRouter_DEBUG_QUERY_PERF
239 std::cout << "visited " + toString(num_visited) + " edges (unsuccessful path length: " + toString(into.size()) + ")\n";
240#endif
241 if (to != nullptr && !mySilent && !silent) {
242 this->myErrorMsgHandler->informf(TL("No connection between edge '%' and edge '%' found."), from->getID(), to->getID());
243 }
244#ifdef DijkstraRouter_DEBUG_QUERY_VISITED
245 DijkstraRouter_DEBUG_QUERY_VISITED_OUT << " </interval>\n</edgeData>\n";
246#endif
247 return false;
248 }
249
250private:
251 DijkstraRouter(const std::vector<typename SUMOAbstractRouter<E, V>::EdgeInfo>& edgeInfos, bool unbuildIsWarning,
252 typename SUMOAbstractRouter<E, V>::Operation effortOperation, typename SUMOAbstractRouter<E, V>::Operation ttOperation, bool silent, EffortCalculator* calc,
253 const bool havePermissions, const bool haveRestrictions) :
254 SUMOAbstractRouter<E, V>("DijkstraRouter", unbuildIsWarning, effortOperation, ttOperation, havePermissions, haveRestrictions),
255 mySilent(silent),
256 myExternalEffort(calc) {
257 for (const auto& edgeInfo : edgeInfos) {
258 this->myEdgeInfos.push_back(typename SUMOAbstractRouter<E, V>::EdgeInfo(edgeInfo.edge));
259 }
260 }
261
262private:
265
267 std::tuple<const E*, const V*, SUMOTime> myLastQuery;
268
270
272};
#define DijkstraRouter_DEBUG_QUERY_VISITED_OUT
long long int SUMOTime
Definition GUI.h:36
#define TL(string)
Definition MsgHandler.h:304
std::string time2string(SUMOTime t, bool humanReadable)
convert SUMOTime to string (independently of global format setting)
Definition SUMOTime.cpp:91
#define STEPS2TIME(x)
Definition SUMOTime.h:55
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types.
@ SVC_IGNORING
vehicles ignoring classes
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:46
bool operator()(const typename SUMOAbstractRouter< E, V >::EdgeInfo *nod1, const typename SUMOAbstractRouter< E, V >::EdgeInfo *nod2) const
Comparing method.
Computes the shortest path through a network using the Dijkstra algorithm.
bool mySilent
whether to suppress warning/error if no route was found
std::tuple< const E *, const V *, SUMOTime > myLastQuery
cache of the last query to enable automated bulk routing
EffortCalculator *const myExternalEffort
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...
virtual SUMOAbstractRouter< E, V > * clone()
DijkstraRouter(const std::vector< E * > &edges, bool unbuildIsWarning, typename SUMOAbstractRouter< E, V >::Operation effortOperation, typename SUMOAbstractRouter< E, V >::Operation ttOperation=nullptr, bool silent=false, EffortCalculator *calc=nullptr, const bool havePermissions=false, const bool haveRestrictions=false)
Constructor.
EdgeInfoByEffortComparator myComparator
virtual ~DijkstraRouter()
Destructor.
DijkstraRouter(const std::vector< typename SUMOAbstractRouter< E, V >::EdgeInfo > &edgeInfos, bool unbuildIsWarning, typename SUMOAbstractRouter< E, V >::Operation effortOperation, typename SUMOAbstractRouter< E, V >::Operation ttOperation, bool silent, EffortCalculator *calc, const bool havePermissions, const bool haveRestrictions)
the effort calculator interface
virtual void setInitialState(const int edge)=0
virtual void update(const int edge, const int prev, const double length)=0
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
void informf(const std::string &format, T value, Targs... Fargs)
adds a new formatted message
Definition MsgHandler.h:116
static std::string getIDSecure(const T *obj, const std::string &fallBack="NULL")
get an identifier for Named-like object which may be Null
Definition Named.h:67
const E *const edge
The current edge.
double effort
Effort to reach the edge.
Operation myTTOperation
The object's operation to perform for travel times.
MsgHandler *const myErrorMsgHandler
the handler for routing errors
const bool myHavePermissions
whether edge permissions need to be considered
bool myBulkMode
whether we are currently operating several route queries in a bulk
std::vector< typename SUMOAbstractRouter< E, V >::EdgeInfo > myEdgeInfos
The container of edge information.
Operation myOperation
The object's operation to perform.
double getTravelTime(const E *const e, const V *const v, const double t, const double effort) const
double getEffort(const E *const e, const V *const v, double t) const
void updateViaEdgeCost(const E *viaEdge, const V *const v, double &time, double &effort, double &length) const
void init(const int edgeID, const SUMOTime msTime)
bool myAmClean
whether we are already initialized
const bool myHaveRestrictions
whether edge restrictions need to be considered
void buildPathFrom(const typename SUMOAbstractRouter< E, V >::EdgeInfo *rbegin, std::vector< const E * > &edges)
Builds the path from marked edges.
bool myAutoBulkMode
whether we are currently trying to detect bulk mode automatically
virtual double recomputeCosts(const std::vector< const E * > &edges, const V *const v, SUMOTime msTime, double *lengthp=nullptr) const
void endQuery(int visits)
std::vector< typename SUMOAbstractRouter< E, V >::EdgeInfo * > myFrontierList
A container for reusage of the min edge heap.
std::vector< typename SUMOAbstractRouter< E, V >::EdgeInfo * > myFound
list of visited Edges (for resetting)