Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
MSStoppingPlaceRerouter.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/****************************************************************************/
18// The MSStoppingPlaceRerouter provides an interface to structure the rerouting
19// to the best StoppingPlace according to the evaluation components and
20// associated weights.
21/****************************************************************************/
22#pragma once
23#include <config.h>
24#include <map>
25#include <functional>
27#include <utils/common/Named.h>
31#include <microsim/MSNet.h>
32#include <microsim/MSStop.h>
34
35class MSEdge;
36
45
46
48public:
50 typedef std::map<const MSStoppingPlace*, StoppingPlaceMemoryEntry, ComparatorIdLess> StoppingPlaceMap;
51
54
55 // Destructor
57
63 auto it = myMap.find(id);
64 if (it == myMap.end()) {
65 return false;
66 } else {
67 myMap.erase(it);
68 return true;
69 }
70 }
71
73 void clear() {
74 myMap.clear();
75 }
76
78 int size() const {
79 return (int)myMap.size();
80 }
81
83 void rememberBlockedStoppingPlace(const MSStoppingPlace* stoppingPlace, bool local) {
84 myMap[stoppingPlace].blockedAtTime = SIMSTEP;
85 if (local) {
86 myMap[stoppingPlace].blockedAtTimeLocal = SIMSTEP;
87 }
88 }
89
91 SUMOTime sawBlockedStoppingPlace(const MSStoppingPlace* stoppingPlace, bool local) const {
92 auto it = myMap.find(stoppingPlace);
93 if (it == myMap.end()) {
94 return -1;
95 } else {
96 return local ? it->second.blockedAtTimeLocal : it->second.blockedAtTime;
97 }
98 }
99
101 void rememberStoppingPlaceScore(const MSStoppingPlace* stoppingPlace, const std::string& score) {
102 myMap[stoppingPlace].score = score;
103 }
104
106 for (auto& item : myMap) {
107 item.second.score = "";
108 }
109 }
110
112 typename StoppingPlaceMap::const_iterator begin() const {
113 return myMap.begin();
114 }
115
117 typename StoppingPlaceMap::const_iterator end() const {
118 return myMap.end();
119 }
120
121private:
124
125};
126
127
129public:
130 typedef std::map<std::string, double> StoppingPlaceParamMap_t;
131 typedef std::map<std::string, bool> StoppingPlaceParamSwitchMap_t;
132 typedef std::map<MSStoppingPlace*, StoppingPlaceParamMap_t, ComparatorIdLess> StoppingPlaceMap_t;
133 typedef std::pair<MSStoppingPlace*, bool> StoppingPlaceVisible;
134 typedef std::map<const MSEdge*, double> Prohibitions;
135
137 MSStoppingPlaceRerouter(std::string paramPrefix = "", bool checkValidity = false, StoppingPlaceParamMap_t addEvalParams = {}, StoppingPlaceParamSwitchMap_t addInvertParams = {});
138
139 // Destructor
141
155 MSStoppingPlace* rerouteStoppingPlace(MSStoppingPlace* destStoppingPlace, const std::vector<StoppingPlaceVisible>& stoppingPlaceCandidates, const std::vector<double>& probs, SUMOVehicle& veh,
156 bool& newDestination, ConstMSEdgeVector& newRoute, StoppingPlaceParamMap_t& scores, const Prohibitions& closedEdges = {},
157 const int insertStopIndex = 0, const bool keepCurrentStop = true);
176 virtual bool evaluateDestination(SUMOVehicle& veh, double brakeGap, bool newDestination,
177 MSStoppingPlace* alternative, double occupancy, double prob,
179 std::map<MSStoppingPlace*, ConstMSEdgeVector>& newRoutes,
180 std::map<MSStoppingPlace*, ConstMSEdgeVector>& stoppingPlaceApproaches,
181 StoppingPlaceParamMap_t& maxValues,
182 StoppingPlaceParamMap_t& addInput,
183 const int insertStopIndex = 0,
184 const bool keepCurrentStop = true);
185
201 virtual bool evaluateCustomComponents(SUMOVehicle& veh, double brakeGap, bool newDestination,
202 MSStoppingPlace* alternative, double occupancy, double prob,
204 ConstMSEdgeVector& newRoute,
205 ConstMSEdgeVector& stoppingPlaceApproach,
206 StoppingPlaceParamMap_t& maxValues,
207 StoppingPlaceParamMap_t& addInput);
208
210 virtual bool validComponentValues(StoppingPlaceParamMap_t& stoppingPlaceValues);
211
213 virtual bool useStoppingPlace(MSStoppingPlace* stoppingPlace);
214
216 virtual SUMOAbstractRouter<MSEdge, SUMOVehicle>& getRouter(SUMOVehicle& veh, const Prohibitions& prohibited = {});
217
219 virtual double getStoppingPlaceOccupancy(MSStoppingPlace* stoppingPlace) = 0;
220
222 virtual double getLastStepStoppingPlaceOccupancy(MSStoppingPlace* stoppingPlace) = 0;
223
225 virtual double getStoppingPlaceCapacity(MSStoppingPlace* stoppingPlace) = 0;
226
228 virtual void rememberBlockedStoppingPlace(SUMOVehicle& veh, const MSStoppingPlace* stoppingPlace, bool blocked) = 0;
229
231 virtual void rememberStoppingPlaceScore(SUMOVehicle& veh, MSStoppingPlace* place, const std::string& score) = 0;
232
234 virtual void resetStoppingPlaceScores(SUMOVehicle& veh) = 0;
235
237 virtual SUMOTime sawBlockedStoppingPlace(SUMOVehicle& veh, MSStoppingPlace* place, bool local) = 0;
238
241
243 virtual void setNumberStoppingPlaceReroutes(SUMOVehicle& veh, int value) = 0;
244
247
256 double getWeight(SUMOVehicle& veh, const std::string param, const double defaultWeight, const bool warn = false);
257
263 static void updateMaxValues(StoppingPlaceParamMap_t& stoppingPlaceValues, StoppingPlaceParamMap_t& maxValues);
264
274 static double getTargetValue(const StoppingPlaceParamMap_t& absValues, const StoppingPlaceParamMap_t& maxValues, const StoppingPlaceParamMap_t& weights, const StoppingPlaceParamSwitchMap_t& norm, const StoppingPlaceParamSwitchMap_t& invert);
275
276protected:
278 void readEvaluationWeights(SUMOVehicle& veh, StoppingPlaceParamMap_t& stoppingPlaceParams, StoppingPlaceParamMap_t& stoppingPlaceDefaults, StoppingPlaceParamMap_t& maxValues) {
279 for (auto& it : stoppingPlaceParams) {
280 const double value = getWeight(veh, it.first, stoppingPlaceDefaults[it.first]);
281 it.second = value;
282 if (value > maxValues[it.first]) {
283 maxValues[it.first] = value;
284 }
285 }
286 }
287
289 const MSRouteIterator determineRerouteOrigin(SUMOVehicle& veh, int insertStopIndex) {
290 const int stopCount = (int)veh.getStops().size();
291 if (insertStopIndex == 0 || stopCount == 0) {
292 return veh.getRerouteOrigin();
293 }
294 // if the stop index is too high cap to route end
295 const int stopIndex = MIN2(insertStopIndex, stopCount - 1);
296 std::list<MSStop>::const_iterator it = veh.getStops().begin();
297 std::advance(it, stopIndex);
298 MSStop relevantStop = *it;
299 return relevantStop.edge;
300 }
301
302private:
303
306
307protected:
308 const std::string myParamPrefix;
313};
long long int SUMOTime
Definition GUI.h:36
std::vector< const MSEdge * > ConstMSEdgeVector
Definition MSEdge.h:74
ConstMSEdgeVector::const_iterator MSRouteIterator
Definition MSRoute.h:57
#define SIMSTEP
Definition SUMOTime.h:61
T MIN2(T a, T b)
Definition StdDefs.h:80
A road/street connecting two junctions.
Definition MSEdge.h:77
MSRouteIterator edge
The edge in the route to stop at.
Definition MSStop.h:48
A lane area vehicles can halt at.
virtual bool useStoppingPlace(MSStoppingPlace *stoppingPlace)
Whether the stopping place should be included in the search (can be used to add an additional filter)
virtual void rememberBlockedStoppingPlace(SUMOVehicle &veh, const MSStoppingPlace *stoppingPlace, bool blocked)=0
store the blocked stopping place in the vehicle
virtual SUMOTime sawBlockedStoppingPlace(SUMOVehicle &veh, MSStoppingPlace *place, bool local)=0
ask the vehicle when it has seen the stopping place
StoppingPlaceParamSwitchMap_t myNormParams
std::map< std::string, bool > StoppingPlaceParamSwitchMap_t
std::map< std::string, double > StoppingPlaceParamMap_t
static void updateMaxValues(StoppingPlaceParamMap_t &stoppingPlaceValues, StoppingPlaceParamMap_t &maxValues)
keep track of the maximum values of each component
virtual SUMOAbstractRouter< MSEdge, SUMOVehicle > & getRouter(SUMOVehicle &veh, const Prohibitions &prohibited={})
Provide the router to use (MSNet::getRouterTT or MSRoutingEngine)
std::map< MSStoppingPlace *, StoppingPlaceParamMap_t, ComparatorIdLess > StoppingPlaceMap_t
virtual int getNumberStoppingPlaceReroutes(SUMOVehicle &veh)=0
ask how many times already the vehicle has been rerouted to another stopping place
virtual double getStoppingPlaceCapacity(MSStoppingPlace *stoppingPlace)=0
Return the number of places the StoppingPlace provides.
MSStoppingPlace * rerouteStoppingPlace(MSStoppingPlace *destStoppingPlace, const std::vector< StoppingPlaceVisible > &stoppingPlaceCandidates, const std::vector< double > &probs, SUMOVehicle &veh, bool &newDestination, ConstMSEdgeVector &newRoute, StoppingPlaceParamMap_t &scores, const Prohibitions &closedEdges={}, const int insertStopIndex=0, const bool keepCurrentStop=true)
main method to trigger the rerouting to the "best" StoppingPlace according to the custom evaluation f...
static double getTargetValue(const StoppingPlaceParamMap_t &absValues, const StoppingPlaceParamMap_t &maxValues, const StoppingPlaceParamMap_t &weights, const StoppingPlaceParamSwitchMap_t &norm, const StoppingPlaceParamSwitchMap_t &invert)
compute the scalar target function value by means of a linear combination of all components/weights a...
const MSRouteIterator determineRerouteOrigin(SUMOVehicle &veh, int insertStopIndex)
Determine the rerouting origin edge (not necessarily the current edge of the vehicle!...
void readEvaluationWeights(SUMOVehicle &veh, StoppingPlaceParamMap_t &stoppingPlaceParams, StoppingPlaceParamMap_t &stoppingPlaceDefaults, StoppingPlaceParamMap_t &maxValues)
Ask the vehicle about the relevant rerouting parameters and initiate the maximum value data structure...
virtual double getStoppingPlaceOccupancy(MSStoppingPlace *stoppingPlace)=0
Return the number of occupied places of the StoppingPlace.
double getWeight(SUMOVehicle &veh, const std::string param, const double defaultWeight, const bool warn=false)
read the value of a stopping place search param, e.g. a component weight factor
virtual StoppingPlaceParamMap_t collectWeights(SUMOVehicle &veh)
read target function weights for this vehicle
StoppingPlaceParamMap_t myEvalParams
StoppingPlaceParamSwitchMap_t myInvertParams
virtual void rememberStoppingPlaceScore(SUMOVehicle &veh, MSStoppingPlace *place, const std::string &score)=0
store the stopping place score in the vehicle
virtual void resetStoppingPlaceScores(SUMOVehicle &veh)=0
forget all stopping place score for this vehicle
virtual void setNumberStoppingPlaceReroutes(SUMOVehicle &veh, int value)=0
update the number of reroutes for the vehicle
std::map< const MSEdge *, double > Prohibitions
virtual bool validComponentValues(StoppingPlaceParamMap_t &stoppingPlaceValues)
Whether the stopping place should be discarded due to its results from the component evaluation (allo...
virtual bool evaluateCustomComponents(SUMOVehicle &veh, double brakeGap, bool newDestination, MSStoppingPlace *alternative, double occupancy, double prob, SUMOAbstractRouter< MSEdge, SUMOVehicle > &router, StoppingPlaceParamMap_t &stoppingPlaceValues, ConstMSEdgeVector &newRoute, ConstMSEdgeVector &stoppingPlaceApproach, StoppingPlaceParamMap_t &maxValues, StoppingPlaceParamMap_t &addInput)
Compute some custom target function components.
std::pair< MSStoppingPlace *, bool > StoppingPlaceVisible
virtual double getLastStepStoppingPlaceOccupancy(MSStoppingPlace *stoppingPlace)=0
Return the number of occupied places of the StoppingPlace from the previous time step.
virtual bool evaluateDestination(SUMOVehicle &veh, double brakeGap, bool newDestination, MSStoppingPlace *alternative, double occupancy, double prob, SUMOAbstractRouter< MSEdge, SUMOVehicle > &router, StoppingPlaceMap_t &stoppingPlaces, std::map< MSStoppingPlace *, ConstMSEdgeVector > &newRoutes, std::map< MSStoppingPlace *, ConstMSEdgeVector > &stoppingPlaceApproaches, StoppingPlaceParamMap_t &maxValues, StoppingPlaceParamMap_t &addInput, const int insertStopIndex=0, const bool keepCurrentStop=true)
compute the target function for a single alternative
MSStoppingPlaceRerouter()=delete
Constructor.
Representation of a vehicle.
Definition SUMOVehicle.h:62
virtual ConstMSEdgeVector::const_iterator getRerouteOrigin() const =0
Returns the starting point for reroutes (usually the current edge)
virtual const std::list< MSStop > & getStops() const =0
int size() const
Returns the number of stored items within the container.
std::map< const MSStoppingPlace *, StoppingPlaceMemoryEntry, ComparatorIdLess > StoppingPlaceMap
Definition of the map containing all visited stopping places.
void clear()
Removes all data about evaluated StoppingPlace items.
SUMOTime sawBlockedStoppingPlace(const MSStoppingPlace *stoppingPlace, bool local) const
Get the time the StoppingPlace was confirmed to be blocked.
StoppingPlaceMap myMap
The map from StoppingPlace to single evaluation.
void rememberStoppingPlaceScore(const MSStoppingPlace *stoppingPlace, const std::string &score)
score only needed when running with gui
StoppingPlaceMap::const_iterator end() const
Returns a reference to the end iterator for the internal map.
void rememberBlockedStoppingPlace(const MSStoppingPlace *stoppingPlace, bool local)
Store the time the StoppingPlace was confirmed to be blocked.
bool remove(MSStoppingPlace *id)
Removes an item.
StoppingPlaceMap::const_iterator begin() const
Returns a reference to the begin iterator for the internal map.
store information for a single stopping place
std::string score
SUMOTime blockedAtTimeLocal
SUMOTime blockedAtTime
StoppingPlaceMemoryEntry()