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-2024 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
136 MSStoppingPlaceRerouter(SumoXMLTag stoppingType, std::string paramPrefix = "", bool checkValidity = false, bool checkVisibility = true, StoppingPlaceParamMap_t addEvalParams = {}, StoppingPlaceParamSwitchMap_t addInvertParams = {});
137
138 // Destructor
140
154 MSStoppingPlace* reroute(std::vector<StoppingPlaceVisible>& stoppingPlaceCandidates, const std::vector<double>& probs, SUMOVehicle& veh,
155 bool& newDestination, ConstMSEdgeVector& newRoute, StoppingPlaceParamMap_t& scores, const MSEdgeVector& closedEdges = {},
156 const int insertStopIndex = 0, const bool keepCurrentStop = true);
175 virtual bool evaluateDestination(SUMOVehicle& veh, double brakeGap, bool newDestination,
176 MSStoppingPlace* alternative, double occupancy, double prob,
178 std::map<MSStoppingPlace*, ConstMSEdgeVector>& newRoutes,
179 std::map<MSStoppingPlace*, ConstMSEdgeVector>& stoppingPlaceApproaches,
180 StoppingPlaceParamMap_t& maxValues,
181 StoppingPlaceParamMap_t& addInput,
182 const int insertStopIndex = 0,
183 const bool keepCurrentStop = true);
184
200 virtual bool evaluateCustomComponents(SUMOVehicle& veh, double brakeGap, bool newDestination,
201 MSStoppingPlace* alternative, double occupancy, double prob,
203 ConstMSEdgeVector& newRoute,
204 ConstMSEdgeVector& stoppingPlaceApproach,
205 StoppingPlaceParamMap_t& maxValues,
206 StoppingPlaceParamMap_t& addInput);
207
209 virtual bool validComponentValues(StoppingPlaceParamMap_t& stoppingPlaceValues);
210
212 virtual bool useStoppingPlace(MSStoppingPlace* stoppingPlace);
213
215 virtual SUMOAbstractRouter<MSEdge, SUMOVehicle>& getRouter(SUMOVehicle& veh, const MSEdgeVector& prohibited = {});
216
218 virtual double getStoppingPlaceOccupancy(MSStoppingPlace* stoppingPlace) = 0;
219
221 virtual double getLastStepStoppingPlaceOccupancy(MSStoppingPlace* stoppingPlace) = 0;
222
224 virtual double getStoppingPlaceCapacity(MSStoppingPlace* stoppingPlace) = 0;
225
227 virtual void rememberBlockedStoppingPlace(SUMOVehicle& veh, const MSStoppingPlace* stoppingPlace, bool blocked) = 0;
228
230 virtual void rememberStoppingPlaceScore(SUMOVehicle& veh, MSStoppingPlace* place, const std::string& score) = 0;
231
233 virtual void resetStoppingPlaceScores(SUMOVehicle& veh) = 0;
234
236 virtual SUMOTime sawBlockedStoppingPlace(SUMOVehicle& veh, MSStoppingPlace* place, bool local) = 0;
237
240
242 virtual void setNumberStoppingPlaceReroutes(SUMOVehicle& veh, int value) = 0;
243
246
255 double getWeight(SUMOVehicle& veh, const std::string param, const double defaultWeight, const bool warn = false);
256
262 static void updateMaxValues(StoppingPlaceParamMap_t& stoppingPlaceValues, StoppingPlaceParamMap_t& maxValues);
263
273 static double getTargetValue(const StoppingPlaceParamMap_t& absValues, const StoppingPlaceParamMap_t& maxValues, const StoppingPlaceParamMap_t& weights, const StoppingPlaceParamSwitchMap_t& norm, const StoppingPlaceParamSwitchMap_t& invert);
274
275protected:
277 void readEvaluationWeights(SUMOVehicle& veh, StoppingPlaceParamMap_t& stoppingPlaceParams, StoppingPlaceParamMap_t& stoppingPlaceDefaults, StoppingPlaceParamMap_t& maxValues) {
278 for (auto& it : stoppingPlaceParams) {
279 const double value = getWeight(veh, it.first, stoppingPlaceDefaults[it.first]);
280 it.second = value;
281 if (value > maxValues[it.first]) {
282 maxValues[it.first] = value;
283 }
284 }
285 }
286
288 const MSRouteIterator determineRerouteOrigin(SUMOVehicle& veh, int insertStopIndex) {
289 const int stopCount = (int)veh.getStops().size();
290 if (insertStopIndex == 0 || stopCount == 0) {
291 return veh.getRerouteOrigin();
292 }
293 // if the stop index is too high cap to route end
294 const int stopIndex = MIN2(insertStopIndex, stopCount - 1);
295 std::list<MSStop>::const_iterator it = veh.getStops().begin();
296 std::advance(it, stopIndex);
297 MSStop relevantStop = *it;
298 return relevantStop.edge;
299 }
300
301private:
302
305
306protected:
308 const std::string myParamPrefix;
314};
long long int SUMOTime
Definition GUI.h:36
std::vector< const MSEdge * > ConstMSEdgeVector
Definition MSEdge.h:74
std::vector< MSEdge * > MSEdgeVector
Definition MSEdge.h:73
ConstMSEdgeVector::const_iterator MSRouteIterator
Definition MSRoute.h:57
#define SIMSTEP
Definition SUMOTime.h:61
SumoXMLTag
Numbers representing SUMO-XML - element names.
T MIN2(T a, T b)
Definition StdDefs.h:76
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 MSEdgeVector &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.
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...
MSStoppingPlace * reroute(std::vector< StoppingPlaceVisible > &stoppingPlaceCandidates, const std::vector< double > &probs, SUMOVehicle &veh, bool &newDestination, ConstMSEdgeVector &newRoute, StoppingPlaceParamMap_t &scores, const MSEdgeVector &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...
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
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()