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
35#ifdef HAVE_FOX
37#endif
38
39
40// ===========================================================================
41// class declarations
42// ===========================================================================
43class MSTransportable;
45
46// ===========================================================================
47// class definitions
48// ===========================================================================
68public:
70
72 static void initWeightConstants(const OptionsCont& oc);
73
75 static void initWeightUpdate();
76
78 static void initEdgeWeights(SUMOVehicleClass svc, SUMOTime lastAdaption = -1, int index = -1);
79
81 static bool hasEdgeUpdates() {
82 return myEdgeWeightSettingCommand != nullptr;
83 }
84
87 return myLastAdaptation;
88 }
89
90 static bool haveExtras() {
91 return myHaveExtras;
92 }
93
95 static inline void applyExtras(const MSEdge* const e, const SUMOVehicle* const v, SUMOTime step, double& effort) {
96 if (gWeightsRandomFactor != 1.) {
97 long long int key = v->getRandomSeed() ^ e->getNumericalID();
99 key ^= step;
100 }
101 effort *= (1 + RandHelper::randHash(key) * (gWeightsRandomFactor - 1));
102 }
103 if (myPriorityFactor != 0) {
104 // lower priority should result in higher effort (and the edge with
105 // minimum priority receives a factor of 1 + myPriorityFactor
106 const double relativeInversePrio = 1 - ((e->getPriority() - myMinEdgePriority) / myEdgePriorityRange);
107 effort *= 1 + relativeInversePrio * myPriorityFactor;
108 }
111 }
112 }
113
115 static ConstMSRoutePtr getCachedRoute(const std::pair<const MSEdge*, const MSEdge*>& key);
116
117 static void initRouter(SUMOVehicle* vehicle = nullptr);
118
120 static void reroute(SUMOVehicle& vehicle, const SUMOTime currentTime, const std::string& info,
121 const bool onInit = false, const bool silent = false, const Prohibitions& prohibited = {});
122
124 static void reroute(MSTransportable& t, const SUMOTime currentTime, const std::string& info,
125 const bool onInit = false, const bool silent = false, const Prohibitions& prohibited = {});
126
128 static void setEdgeTravelTime(const MSEdge* const edge, const double travelTime);
129
131 static void cleanup();
132
134 static bool isEnabled() {
135 return !myWithTaz && myAdaptationInterval >= 0;
136 }
137
139 static MSVehicleRouter& getRouterTT(const int rngIndex,
141 const Prohibitions& prohibited = {});
142
144 static MSTransportableRouter& getIntermodalRouterTT(const int rngIndex,
145 const Prohibitions& prohibited = {});
146
148 static bool hasBikeSpeeds() {
149 return myBikeSpeeds;
150 }
151
166 static double getEffort(const MSEdge* const e, const SUMOVehicle* const v, double t);
167 static double getEffortBike(const MSEdge* const e, const SUMOVehicle* const v, double t);
168 static double getEffortExtra(const MSEdge* const e, const SUMOVehicle* const v, double t);
170
172 static double getAssumedSpeed(const MSEdge* edge, const SUMOVehicle* veh);
173
175 static bool withTaz() {
176 return myWithTaz;
177 }
178
180 static void addEdgeTravelTime(const MSEdge& edge, const SUMOTime travelTime);
181
186 static void saveState(OutputDevice& out);
187
192 static void loadState(const SUMOSAXAttributes& attrs);
193
194#ifdef HAVE_FOX
195 static void waitForAll();
196#endif
197
198
199private:
200#ifdef HAVE_FOX
205 class RoutingTask : public MFXWorkerThread::Task {
206 public:
207 RoutingTask(SUMOVehicle& v, const SUMOTime time, const std::string& info,
208 const bool onInit, const bool silent, const Prohibitions& prohibited)
209 : myVehicle(v), myTime(time), myInfo(info), myOnInit(onInit), mySilent(silent), myProhibited(prohibited) {}
210 void run(MFXWorkerThread* context);
211 private:
212 SUMOVehicle& myVehicle;
213 const SUMOTime myTime;
214 const std::string myInfo;
215 const bool myOnInit;
216 const bool mySilent;
217 const Prohibitions myProhibited;
218 private:
220 RoutingTask& operator=(const RoutingTask&) = delete;
221 };
222
223#endif
224
227
239 static SUMOTime adaptEdgeEfforts(SUMOTime currentTime);
240
241 static double patchSpeedForTurns(const MSEdge* edge, double currSpeed);
243
245 static void _initEdgeWeights(std::vector<double>& edgeSpeeds, std::vector<std::vector<double> >& pastEdgeSpeeds);
246
249
250private:
253
255 static double myAdaptationWeight;
256
259
262
265
268
269 typedef std::pair<SUMOTime, int> TimeAndCount;
270
272 static std::vector<double> myEdgeSpeeds;
273 static std::vector<double> myEdgeBikeSpeeds;
274
276 static std::vector<TimeAndCount> myEdgeTravelTimes;
277
279 static std::vector<std::vector<double> > myPastEdgeSpeeds;
280 static std::vector<std::vector<double> > myPastEdgeBikeSpeeds;
281
283 static bool myWithTaz;
284
286 static bool myBikeSpeeds;
287
290
292 static std::map<std::pair<const MSEdge*, const MSEdge*>, ConstMSRoutePtr> myCachedRoutes;
293
295 static double myPriorityFactor;
296
298 static double myMinEdgePriority;
300 static double myEdgePriorityRange;
301
304
306 static bool myHaveExtras;
307
308#ifdef HAVE_FOX
310 static FXMutex myRouteCacheMutex;
311#endif
312
313private:
316
319
320
321};
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
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:199
double getPreference(const std::string &routingType, const SUMOVTypeParameter &pars) const
retriefe edge type specific routing preference
Definition MSNet.cpp:378
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 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 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 void _initEdgeWeights(std::vector< double > &edgeSpeeds, std::vector< std::vector< double > > &pastEdgeSpeeds)
initialized edge speed storage into the given containers
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 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 initWeightUpdate()
intialize period edge weight update
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 void initRouter(SUMOVehicle *vehicle=nullptr)
static SUMOAbstractRouter< MSEdge, SUMOVehicle >::Operation myEffortFunc
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 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 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
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