Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
MSRoutingEngine.h
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2007-2026 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// A device that performs vehicle rerouting based on current edge speeds
21/****************************************************************************/
22#pragma once
23#include <config.h>
24
25#include <atomic>
26#include <set>
27#include <vector>
28#include <map>
29#include <mutex>
30#include <thread>
34#include <microsim/MSEdge.h>
36#include <memory>
37#include <atomic>
38#include <map>
39#include <mutex>
40namespace RoutingKit {
41struct CustomizableContractionHierarchyMetric;
42struct CustomizableContractionHierarchyPartialCustomization;
43}
44
45#ifdef HAVE_FOX
47#endif
48
49
50// ===========================================================================
51// class declarations
52// ===========================================================================
53class MSTransportable;
54class MSVehicleType;
56
57// ===========================================================================
58// class definitions
59// ===========================================================================
79public:
81
83 static void initWeightConstants(const OptionsCont& oc);
84
86 static void initWeightUpdate(SUMOTime lastAdaption = - 1);
87
89 static void initEdgeWeights(SUMOVehicleClass svc, SUMOTime lastAdaption = -1, int index = -1);
90
92 static bool hasEdgeUpdates() {
93 return myEdgeWeightSettingCommand != nullptr;
94 }
95
98 return myLastAdaptation;
99 }
100
101 static bool haveExtras() {
102 return myHaveExtras;
103 }
104
106 static inline void applyExtras(const MSEdge* const e, const SUMOVehicle* const v, SUMOTime step, double& effort) {
107 if (gWeightsRandomFactor != 1. && v != nullptr) {
108 long long int key = v->getRandomSeed() ^ e->getNumericalID();
110 key ^= step;
111 }
112 effort *= (1 + RandHelper::randHash(key) * (gWeightsRandomFactor - 1));
113 }
114 if (myPriorityFactor != 0) {
115 // lower priority should result in higher effort (and the edge with
116 // minimum priority receives a factor of 1 + myPriorityFactor
117 const double relativeInversePrio = 1 - ((e->getPriority() - myMinEdgePriority) / myEdgePriorityRange);
118 effort *= 1 + relativeInversePrio * myPriorityFactor;
119 }
120 if (gRoutingPreferences && v != nullptr) {
122 }
123 }
124
126 static ConstMSRoutePtr getCachedRoute(const std::pair<const MSEdge*, const MSEdge*>& key);
127
136
147
151 static MSCCHGraph* ensureCCHGraph();
152
153 static void initRouter(SUMOVehicle* vehicle = nullptr);
154
156 static void reroute(SUMOVehicle& vehicle, const SUMOTime currentTime, const std::string& info,
157 const bool onInit = false, const bool silent = false, const Prohibitions& prohibited = {});
158
160 static void reroute(MSTransportable& t, const SUMOTime currentTime, const std::string& info,
161 const bool onInit = false, const bool silent = false, const Prohibitions& prohibited = {});
162
164 static void setEdgeTravelTime(const MSEdge* const edge, const double travelTime);
165
167 static void cleanup();
168
176 static void cleanupCCH();
177
179 static bool isEnabled() {
180 return !myWithTaz && myAdaptationInterval >= 0;
181 }
182
184 static MSVehicleRouter& getRouterTT(const int rngIndex,
186 const Prohibitions& prohibited = {});
187
189 static MSTransportableRouter& getIntermodalRouterTT(const int rngIndex,
190 const Prohibitions& prohibited = {});
191
193 static bool hasBikeSpeeds() {
194 return myBikeSpeeds;
195 }
196
211 static double getEffort(const MSEdge* const e, const SUMOVehicle* const v, double t);
212 static double getEffortBike(const MSEdge* const e, const SUMOVehicle* const v, double t);
213 static double getEffortExtra(const MSEdge* const e, const SUMOVehicle* const v, double t);
215
217 static double getAssumedSpeed(const MSEdge* edge, const SUMOVehicle* veh);
218
220 static bool withTaz() {
221 return myWithTaz;
222 }
223
225 static void addEdgeTravelTime(const MSEdge& edge, const SUMOTime travelTime);
226
231 static void saveState(OutputDevice& out);
232
237 static void loadState(const SUMOSAXAttributes& attrs);
238
240 static void loadCCHMetric(const SUMOSAXAttributes& attrs);
241
242#ifdef HAVE_FOX
243 static void waitForAll();
244#endif
245
246
247private:
248#ifdef HAVE_FOX
253 class RoutingTask : public MFXWorkerThread::Task {
254 public:
255 RoutingTask(SUMOVehicle& v, const SUMOTime time, const std::string& info,
256 const bool onInit, const bool silent, const Prohibitions& prohibited)
257 : myVehicle(v), myTime(time), myInfo(info), myOnInit(onInit), mySilent(silent), myProhibited(prohibited) {}
258 void run(MFXWorkerThread* context);
259 private:
260 SUMOVehicle& myVehicle;
261 const SUMOTime myTime;
262 const std::string myInfo;
263 const bool myOnInit;
264 const bool mySilent;
265 const Prohibitions myProhibited;
266 private:
268 RoutingTask& operator=(const RoutingTask&) = delete;
269 };
270
271#endif
272
275
287 static SUMOTime adaptEdgeEfforts(SUMOTime currentTime);
288
289 static double patchSpeedForTurns(const MSEdge* edge, double currSpeed);
291
293 static void _initEdgeWeights(std::vector<double>& edgeSpeeds, std::vector<std::vector<double> >& pastEdgeSpeeds);
294
297
298private:
301
303 static double myAdaptationWeight;
304
307
310
313
316
317 typedef std::pair<SUMOTime, int> TimeAndCount;
318
320 static std::vector<double> myEdgeSpeeds;
321 static std::vector<double> myEdgeBikeSpeeds;
322
324 static std::vector<TimeAndCount> myEdgeTravelTimes;
325
327 static std::vector<std::vector<double> > myPastEdgeSpeeds;
328 static std::vector<std::vector<double> > myPastEdgeBikeSpeeds;
329
331 static bool myWithTaz;
332
334 static bool myBikeSpeeds;
335
338
340 static std::atomic<bool> myRouterReady;
341
343 static std::mutex myRouterInitLock;
344
346 static std::map<std::pair<const MSEdge*, const MSEdge*>, ConstMSRoutePtr> myCachedRoutes;
347
349 static double myPriorityFactor;
350
352 static double myMinEdgePriority;
354 static double myEdgePriorityRange;
355
358
360 static bool myHaveExtras;
361
362#ifdef HAVE_FOX
364 static FXMutex myRouteCacheMutex;
365#endif
366
385 static SUMOVehicle* buildCCHRefVehicle(const MSVehicleType* type, int slot);
388 static void initCCH();
390 static void buildCCHLive();
393
394public:
399 static void invalidateCCHEdge(const MSEdge* e);
400private:
401
402private:
405
408
409
410};
long long int SUMOTime
Definition GUI.h:36
std::shared_ptr< const MSRoute > ConstMSRoutePtr
Definition Route.h:32
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types.
bool gRoutingPreferences
Definition StdDefs.cpp:37
double gWeightsRandomFactor
Definition StdDefs.cpp:35
Metric-independent RoutingKit CCH topology over the PURE road graph.
Definition CCHGraph.h:97
per-vehicle-type CCH metric store over a shared CCHGraph
Base (microsim) event class.
Definition Command.h:50
Abstract superclass of a task to be run with an index to keep track of pending tasks.
A thread repeatingly calculating incoming tasks.
A road/street connecting two junctions.
Definition MSEdge.h:78
int getPriority() const
Returns the priority of the edge.
Definition MSEdge.h:338
const std::string & getRoutingType() const
Returns the type of the edge.
Definition MSEdge.h:327
int getNumericalID() const
Returns the numerical id of the edge.
Definition MSEdge.h:308
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition MSNet.cpp:201
double getPreference(const std::string &routingType, const SUMOVTypeParameter &pars) const
retriefe edge type specific routing preference
Definition MSNet.cpp:392
A device that performs vehicle rerouting based on current edge speeds.
static void saveState(OutputDevice &out)
Saves the state (i.e. recorded speeds)
static SUMOTime myAdaptationInterval
At which time interval the edge weights get updated.
static void initEdgeWeights(SUMOVehicleClass svc, SUMOTime lastAdaption=-1, int index=-1)
initialize the edge weights if not done before
static bool haveExtras()
static double myAdaptationWeight
Information which weight prior edge efforts have.
static int myAdaptationStepsIndex
The current index in the pastEdgeSpeed ring-buffer.
static double myMinEdgePriority
Minimum priority for all edges.
static std::vector< TimeAndCount > myEdgeTravelTimes
Sum of travel times experienced by equipped vehicles for each edge.
MSRoutingEngine(const MSRoutingEngine &)
Invalidated copy constructor.
static double getEffortBike(const MSEdge *const e, const SUMOVehicle *const v, double t)
static SUMOVehicle * buildCCHRefVehicle(const MSVehicleType *type, int slot)
construct the unregistered effort-reference vehicle for a type: never counted, inserted or given devi...
static MSCCHGraph * myCCHGraph
the immutable shared CCH topology (built once), or nullptr if inactive
static MSCCHGraph * ensureCCHGraph()
the shared CCH topology, built on first demand (used by MSNet::getRouterTT to construct its CCH route...
static void setEdgeTravelTime(const MSEdge *const edge, const double travelTime)
adapt the known travel time for an edge
static SUMOTime getLastAdaptation()
Information when the last edge weight adaptation occurred.
static double myEdgePriorityRange
the difference between maximum and minimum priority for all edges
static bool withTaz()
whether taz-routing is enabled
static SumoRNG * getThreadRNG()
returns RNG associated with the current thread
static double myPriorityFactor
Coefficient for factoring edge priority into routing weight.
static void cleanupCCH()
tears down the CCH state (myCCHLive/myCCHFreeflow/myCCHGraph) only – the ref vehicles owned by the CC...
static std::map< std::pair< const MSEdge *, const MSEdge * >, ConstMSRoutePtr > myCachedRoutes
The container of pre-calculated routes.
static MSCCHMetricFamily * getLoadedCCH()
the live family restored from a state, built on first use
static SUMOTime adaptEdgeEfforts(SUMOTime currentTime)
Adapt edge efforts by the current edge states.
MSRoutingEngine & operator=(const MSRoutingEngine &)
Invalidated assignment operator.
static bool myBikeSpeeds
whether separate speeds for bicycles shall be tracked
static MSCCHMetricFamily * myCCHLive
the rerouting device's LIVE metric family over the adaptive speed tables (see utils/router/CCHMetricF...
static void _initEdgeWeights(std::vector< double > &edgeSpeeds, std::vector< std::vector< double > > &pastEdgeSpeeds)
initialized edge speed storage into the given containers
static void initCCH()
build the shared CCH + the live family with one metric per loaded type, publish initial metrics
static bool isEnabled()
returns whether any routing actions take place
static bool myWithTaz
whether taz shall be used at initial rerouting
static std::vector< std::vector< double > > myPastEdgeBikeSpeeds
static std::vector< double > myEdgeSpeeds
The container of edge speeds.
std::pair< SUMOTime, int > TimeAndCount
static const RoutingKit::CustomizableContractionHierarchyMetric * getPublishedCCHMetric(SUMOVehicleClass vClass, SUMOTime time, const SUMOVehicle *veh)
the currently published (customized) CCH metric FOR A GIVEN vehicle class, or nullptr if that class h...
static bool myHaveExtras
whether extra routing cost modifications are configured
static bool hasEdgeUpdates()
returns whether any edge weight updates will take place
SUMOAbstractRouter< MSEdge, SUMOVehicle >::Prohibitions Prohibitions
static void addEdgeTravelTime(const MSEdge &edge, const SUMOTime travelTime)
record actual travel time for an edge
static void applyExtras(const MSEdge *const e, const SUMOVehicle *const v, SUMOTime step, double &effort)
apply cost modifications from randomness, priorityFactor and preferences
static bool myDynamicRandomness
whether randomness varies over time
static std::mutex myRouterInitLock
serialises building the routers when the first queries come from several threads
static std::atomic< bool > myRouterReady
whether the routers of all threads are built
static SUMOTime myLastAdaptation
Information when the last edge weight adaptation occurred.
static void buildCCHLive()
build the live family without metrics
static void cleanup()
deletes the router instance
static MSCCHMetricFamily * myCCHFreeflow
the STATIC free-flow metric family behind MSNet's routers (TraCI / triggers / GUI,...
static void initRouter(SUMOVehicle *vehicle=nullptr)
static SUMOAbstractRouter< MSEdge, SUMOVehicle >::Operation myEffortFunc
static void invalidateCCHEdge(const MSEdge *e)
a runtime permission change (closure / re-opening) hit this edge: invalidate the graph's primed conne...
static void loadCCHMetric(const SUMOSAXAttributes &attrs)
Loads the arcs of a CCH metric that lag behind the speeds.
static ConstMSRoutePtr getCachedRoute(const std::pair< const MSEdge *, const MSEdge * > &key)
return the cached route or nullptr on miss
static int myAdaptationSteps
The number of steps for averaging edge speeds (ring-buffer)
static bool hasBikeSpeeds()
whether the router collects bicycle speeds
static MSRouterProvider * myRouterProvider
The router to use.
static Command * myEdgeWeightSettingCommand
The weights adaptation/overwriting command.
static std::vector< std::vector< double > > myPastEdgeSpeeds
The container of past edge speeds (when using a simple moving average)
static void initWeightUpdate(SUMOTime lastAdaption=- 1)
intialize period edge weight update
static void reroute(SUMOVehicle &vehicle, const SUMOTime currentTime, const std::string &info, const bool onInit=false, const bool silent=false, const Prohibitions &prohibited={})
initiate the rerouting, create router / thread pool on first use
static double getEffort(const MSEdge *const e, const SUMOVehicle *const v, double t)
Returns the effort to pass an edge.
static double getAssumedSpeed(const MSEdge *edge, const SUMOVehicle *veh)
return current travel speed assumption
static void loadState(const SUMOSAXAttributes &attrs)
Loads the state.
static const RoutingKit::CustomizableContractionHierarchyMetric * getFreeflowCCHMetric(SUMOVehicleClass vClass, SUMOTime time, const SUMOVehicle *veh)
the free-flow CCH metric for MSNet's routers (TraCI / triggers / GUI), keyed by vehicle type and fill...
static double patchSpeedForTurns(const MSEdge *edge, double currSpeed)
static MSVehicleRouter & getRouterTT(const int rngIndex, SUMOVehicleClass svc, const Prohibitions &prohibited={})
return the vehicle router instance
static double getEffortExtra(const MSEdge *const e, const SUMOVehicle *const v, double t)
static MSTransportableRouter & getIntermodalRouterTT(const int rngIndex, const Prohibitions &prohibited={})
return the person router instance
static std::vector< double > myEdgeBikeSpeeds
static void initWeightConstants(const OptionsCont &oc)
initialize constants for using myPriorityFactor
The car-following model and parameter.
A storage for options typed value containers)
Definition OptionsCont.h:92
Static storage of an output device and its base (abstract) implementation.
static double randHash(long long int x)
return a value scrambled value from [0, 1]
Definition RandHelper.h:257
std::map< const E *, RouterProhibition > Prohibitions
Encapsulated SAX-Attributes.
virtual long long int getRandomSeed() const =0
return an object-specific random constant
virtual const SUMOVTypeParameter & getVTypeParameter() const =0
Returns the object's "vehicle" type parameter.
Representation of a vehicle.
Definition SUMOVehicle.h:63