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 <set>
26#include <vector>
27#include <map>
28#include <thread>
32#include <microsim/MSEdge.h>
34#include <memory>
35#include <atomic>
36#include <map>
37#include <mutex>
38namespace RoutingKit {
39struct CustomizableContractionHierarchyMetric;
40struct CustomizableContractionHierarchyPartialCustomization;
41}
42
43#ifdef HAVE_FOX
45#endif
46
47
48// ===========================================================================
49// class declarations
50// ===========================================================================
51class MSTransportable;
52class MSVehicleType;
54
55// ===========================================================================
56// class definitions
57// ===========================================================================
77public:
79
81 static void initWeightConstants(const OptionsCont& oc);
82
84 static void initWeightUpdate(SUMOTime lastAdaption = - 1);
85
87 static void initEdgeWeights(SUMOVehicleClass svc, SUMOTime lastAdaption = -1, int index = -1);
88
90 static bool hasEdgeUpdates() {
91 return myEdgeWeightSettingCommand != nullptr;
92 }
93
96 return myLastAdaptation;
97 }
98
99 static bool haveExtras() {
100 return myHaveExtras;
101 }
102
104 static inline void applyExtras(const MSEdge* const e, const SUMOVehicle* const v, SUMOTime step, double& effort) {
105 if (gWeightsRandomFactor != 1.) {
106 long long int key = v->getRandomSeed() ^ e->getNumericalID();
108 key ^= step;
109 }
110 effort *= (1 + RandHelper::randHash(key) * (gWeightsRandomFactor - 1));
111 }
112 if (myPriorityFactor != 0) {
113 // lower priority should result in higher effort (and the edge with
114 // minimum priority receives a factor of 1 + myPriorityFactor
115 const double relativeInversePrio = 1 - ((e->getPriority() - myMinEdgePriority) / myEdgePriorityRange);
116 effort *= 1 + relativeInversePrio * myPriorityFactor;
117 }
120 }
121 }
122
124 static ConstMSRoutePtr getCachedRoute(const std::pair<const MSEdge*, const MSEdge*>& key);
125
134
145
149 static MSCCHGraph* ensureCCHGraph();
150
151 static void initRouter(SUMOVehicle* vehicle = nullptr);
152
154 static void reroute(SUMOVehicle& vehicle, const SUMOTime currentTime, const std::string& info,
155 const bool onInit = false, const bool silent = false, const Prohibitions& prohibited = {});
156
158 static void reroute(MSTransportable& t, const SUMOTime currentTime, const std::string& info,
159 const bool onInit = false, const bool silent = false, const Prohibitions& prohibited = {});
160
162 static void setEdgeTravelTime(const MSEdge* const edge, const double travelTime);
163
165 static void cleanup();
166
174 static void cleanupCCH();
175
177 static bool isEnabled() {
178 return !myWithTaz && myAdaptationInterval >= 0;
179 }
180
182 static MSVehicleRouter& getRouterTT(const int rngIndex,
184 const Prohibitions& prohibited = {});
185
187 static MSTransportableRouter& getIntermodalRouterTT(const int rngIndex,
188 const Prohibitions& prohibited = {});
189
191 static bool hasBikeSpeeds() {
192 return myBikeSpeeds;
193 }
194
209 static double getEffort(const MSEdge* const e, const SUMOVehicle* const v, double t);
210 static double getEffortBike(const MSEdge* const e, const SUMOVehicle* const v, double t);
211 static double getEffortExtra(const MSEdge* const e, const SUMOVehicle* const v, double t);
213
215 static double getAssumedSpeed(const MSEdge* edge, const SUMOVehicle* veh);
216
218 static bool withTaz() {
219 return myWithTaz;
220 }
221
223 static void addEdgeTravelTime(const MSEdge& edge, const SUMOTime travelTime);
224
229 static void saveState(OutputDevice& out);
230
235 static void loadState(const SUMOSAXAttributes& attrs);
236
237#ifdef HAVE_FOX
238 static void waitForAll();
239#endif
240
241
242private:
243#ifdef HAVE_FOX
248 class RoutingTask : public MFXWorkerThread::Task {
249 public:
250 RoutingTask(SUMOVehicle& v, const SUMOTime time, const std::string& info,
251 const bool onInit, const bool silent, const Prohibitions& prohibited)
252 : myVehicle(v), myTime(time), myInfo(info), myOnInit(onInit), mySilent(silent), myProhibited(prohibited) {}
253 void run(MFXWorkerThread* context);
254 private:
255 SUMOVehicle& myVehicle;
256 const SUMOTime myTime;
257 const std::string myInfo;
258 const bool myOnInit;
259 const bool mySilent;
260 const Prohibitions myProhibited;
261 private:
263 RoutingTask& operator=(const RoutingTask&) = delete;
264 };
265
266#endif
267
270
282 static SUMOTime adaptEdgeEfforts(SUMOTime currentTime);
283
284 static double patchSpeedForTurns(const MSEdge* edge, double currSpeed);
286
288 static void _initEdgeWeights(std::vector<double>& edgeSpeeds, std::vector<std::vector<double> >& pastEdgeSpeeds);
289
292
293private:
296
298 static double myAdaptationWeight;
299
302
305
308
311
312 typedef std::pair<SUMOTime, int> TimeAndCount;
313
315 static std::vector<double> myEdgeSpeeds;
316 static std::vector<double> myEdgeBikeSpeeds;
317
319 static std::vector<TimeAndCount> myEdgeTravelTimes;
320
322 static std::vector<std::vector<double> > myPastEdgeSpeeds;
323 static std::vector<std::vector<double> > myPastEdgeBikeSpeeds;
324
326 static bool myWithTaz;
327
329 static bool myBikeSpeeds;
330
333
335 static std::map<std::pair<const MSEdge*, const MSEdge*>, ConstMSRoutePtr> myCachedRoutes;
336
338 static double myPriorityFactor;
339
341 static double myMinEdgePriority;
343 static double myEdgePriorityRange;
344
347
349 static bool myHaveExtras;
350
351#ifdef HAVE_FOX
353 static FXMutex myRouteCacheMutex;
354#endif
355
374 static SUMOVehicle* buildCCHRefVehicle(const MSVehicleType* type, int slot);
377 static void initCCH();
378
379public:
384 static void invalidateCCHEdge(const MSEdge* e);
385private:
386
387private:
390
393
394
395};
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:77
int getPriority() const
Returns the priority of the edge.
Definition MSEdge.h:337
const std::string & getRoutingType() const
Returns the type of the edge.
Definition MSEdge.h:326
int getNumericalID() const
Returns the numerical id of the edge.
Definition MSEdge.h:307
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 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 SUMOTime myLastAdaptation
Information when the last edge weight adaptation occurred.
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 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:89
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:269
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