Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
MSTransportableDevice_Routing.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2007-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/****************************************************************************/
22// A device that performs vehicle rerouting based on current edge speeds
23/****************************************************************************/
24#include <config.h>
25
26#include <microsim/MSNet.h>
31#include <microsim/MSEdge.h>
32
33#include "MSRoutingEngine.h"
35
36
37// ===========================================================================
38// method definitions
39// ===========================================================================
40// ---------------------------------------------------------------------------
41// static initialisation methods
42// ---------------------------------------------------------------------------
43void
45 insertDefaultAssignmentOptions("rerouting", "Routing", oc, true);
46 oc.doRegister("person-device.rerouting.period", new Option_String("0", "TIME"));
47 oc.addSynonyme("person-device.rerouting.period", "person-device.routing.period", true);
48 oc.addDescription("person-device.rerouting.period", "Routing", TL("The period with which the person shall be rerouted"));
49
50 oc.doRegister("person-device.rerouting.mode", new Option_String("0"));
51 oc.addDescription("person-device.rerouting.mode", "Routing", TL("Set routing flags (8 ignores temporary blockages)"));
52
53 oc.doRegister("person-device.rerouting.scope", new Option_String("stage"));
54 oc.addDescription("person-device.rerouting.scope", "Routing", TL("Which part of the person plan is to be replaced (stage, sequence, or trip)"));
55}
56
57
58void
59MSTransportableDevice_Routing::buildDevices(MSTransportable& p, std::vector<MSTransportableDevice*>& into) {
61 if (p.getParameter().wasSet(VEHPARS_FORCE_REROUTE) || equippedByDefaultAssignmentOptions(oc, "rerouting", p, false, true)) {
62 // route computation is enabled
63 const SUMOTime period = p.getTimeParam("person-device.rerouting.period");
64 if (period > 0) {
66 // build the device
67 into.push_back(new MSTransportableDevice_Routing(p, "routing_" + p.getID(), period));
68 }
69 }
70}
71
72
73// ---------------------------------------------------------------------------
74// MSTransportableDevice_Routing-methods
75// ---------------------------------------------------------------------------
77 : MSTransportableDevice(holder, id), myPeriod(period), myLastRouting(-1), myRerouteCommand(0) {
78 myScope = holder.getStringParam("person-device.rerouting.scope");
79 // no need for initial routing here, personTrips trigger it themselves
82 // the event will deschedule and destroy itself so it does not need to be stored
83}
84
85
87 // make the rerouting command invalid if there is one
88 if (myRerouteCommand != nullptr) {
90 }
91}
92
93
99
100
101void
102MSTransportableDevice_Routing::reroute(const SUMOTime currentTime, const bool onInit) {
104 //check whether the weights did change since the last reroute
106 return;
107 }
108 myLastRouting = currentTime;
109 MSRoutingEngine::reroute(myHolder, currentTime, "person-device.rerouting", onInit);
110}
111
112
113std::string
114MSTransportableDevice_Routing::getParameter(const std::string& key) const {
115 if (key == "period") {
116 return time2string(myPeriod);
117 }
118 throw InvalidArgument("Parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
119}
120
121
122void
123MSTransportableDevice_Routing::setParameter(const std::string& key, const std::string& value) {
124 double doubleValue;
125 try {
126 doubleValue = StringUtils::toDouble(value);
127 } catch (NumberFormatException&) {
128 throw InvalidArgument("Setting parameter '" + key + "' requires a number for device of type '" + deviceName() + "'");
129 }
130 if (key == "period") {
131 const SUMOTime oldPeriod = myPeriod;
132 myPeriod = TIME2STEPS(doubleValue);
133 if (myPeriod <= 0) {
135 } else if (oldPeriod <= 0) {
136 // re-schedule routing command
138 }
139 } else {
140 throw InvalidArgument("Setting parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
141 }
142}
143
144
145void
149 std::vector<std::string> internals;
150 internals.push_back(toString(myPeriod));
151 out.writeAttr(SUMO_ATTR_STATE, toString(internals));
152 out.closeTag();
153}
154
155
156void
158 std::istringstream bis(attrs.getString(SUMO_ATTR_STATE));
159 bis >> myPeriod;
160}
161
162
163/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
#define TL(string)
Definition MsgHandler.h:315
std::string time2string(SUMOTime t, bool humanReadable)
convert SUMOTime to string (independently of global format setting)
Definition SUMOTime.cpp:69
#define SIMSTEP
Definition SUMOTime.h:61
#define TIME2STEPS(x)
Definition SUMOTime.h:57
@ SVC_PEDESTRIAN
pedestrian
const long long int VEHPARS_FORCE_REROUTE
@ SUMO_TAG_DEVICE
@ SUMO_ATTR_ID
@ SUMO_ATTR_STATE
The state of a link.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:46
static void insertDefaultAssignmentOptions(const std::string &deviceName, const std::string &optionsTopic, OptionsCont &oc, const bool isPerson=false)
Adds common command options that allow to assign devices to vehicles.
Definition MSDevice.cpp:155
static bool equippedByDefaultAssignmentOptions(const OptionsCont &oc, const std::string &deviceName, DEVICEHOLDER &v, bool outputOptionSet, const bool isPerson=false)
Determines whether a vehicle should get a certain device.
Definition MSDevice.h:195
virtual void addEvent(Command *operation, SUMOTime execTimeStep=-1)
Adds an Event.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition MSNet.cpp:185
MSEventControl * getInsertionEvents()
Returns the event control for insertion events.
Definition MSNet.h:491
static void reroute(SUMOVehicle &vehicle, const SUMOTime currentTime, const std::string &info, const bool onInit=false, const bool silent=false, const MSEdgeVector &prohibited=MSEdgeVector())
initiate the rerouting, create router / thread pool on first use
static SUMOTime getLastAdaptation()
Information when the last edge weight adaptation occurred.
static void initWeightUpdate()
intialize period edge weight update
static void initEdgeWeights(SUMOVehicleClass svc)
initialize the edge weights if not done before
A device that performs person rerouting based on current edge speeds.
static void buildDevices(MSTransportable &p, std::vector< MSTransportableDevice * > &into)
Build devices for the given person, if needed.
static void insertOptions(OptionsCont &oc)
Inserts MSTransportableDevice_Routing-options.
const std::string deviceName() const
return the name for this type of device
SUMOTime myLastRouting
The last time a routing took place.
void loadState(const SUMOSAXAttributes &attrs)
Loads the state of the device from the given description.
MSTransportableDevice_Routing(MSTransportable &holder, const std::string &id, SUMOTime period)
Constructor.
void saveState(OutputDevice &out) const
Saves the state of the device.
SUMOTime wrappedRerouteCommandExecute(SUMOTime currentTime)
Performs rerouting after a period.
std::string myScope
The range of the plan which shall be replaced.
void setParameter(const std::string &key, const std::string &value)
try to set the given parameter for this device. Throw exception for unsupported key
std::string getParameter(const std::string &key) const
try to retrieve the given parameter from this device. Throw exception for unsupported key
WrappingCommand< MSTransportableDevice_Routing > * myRerouteCommand
The (optional) command responsible for rerouting.
SUMOTime myPeriod
The period with which a vehicle shall be rerouted.
void reroute(const SUMOTime currentTime, const bool onInit=false)
initiate the rerouting, create router / thread pool on first use
Abstract in-person device.
MSTransportable & myHolder
The person that stores the device.
const SUMOVehicleParameter & getParameter() const
Returns the vehicle's parameter (including departure definition)
const std::string & getID() const
Returns the id.
Definition Named.h:74
A storage for options typed value containers)
Definition OptionsCont.h:89
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
void addSynonyme(const std::string &name1, const std::string &name2, bool isDeprecated=false)
Adds a synonyme for an options name (any order)
void doRegister(const std::string &name, Option *o)
Adds an option under the given name.
static OptionsCont & getOptions()
Retrieves the options.
Static storage of an output device and its base (abstract) implementation.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
Encapsulated SAX-Attributes.
virtual std::string getString(int id, bool *isPresent=nullptr) const =0
Returns the string-value of the named (by its enum-value) attribute.
std::string getStringParam(const std::string &paramName, const bool required=false, const std::string &deflt="") const
Retrieve a string parameter for the traffic object.
SUMOTime getTimeParam(const std::string &paramName, const bool required=false, const SUMOTime deflt=SUMOTime_MIN) const
Retrieve a time parameter for the traffic object.
bool wasSet(long long int what) const
Returns whether the given parameter was set.
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
A wrapper for a Command function.
void deschedule()
Marks this Command as being descheduled.