Eclipse SUMO - Simulation of Urban MObility
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-2023 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 "MSRoutingEngine.h"
33
34
35// ===========================================================================
36// method definitions
37// ===========================================================================
38// ---------------------------------------------------------------------------
39// static initialisation methods
40// ---------------------------------------------------------------------------
41void
43 insertDefaultAssignmentOptions("rerouting", "Routing", oc, true);
44 oc.doRegister("person-device.rerouting.period", new Option_String("0", "TIME"));
45 oc.addSynonyme("person-device.rerouting.period", "person-device.routing.period", true);
46 oc.addDescription("person-device.rerouting.period", "Routing", TL("The period with which the person shall be rerouted"));
47}
48
49
50void
51MSTransportableDevice_Routing::buildDevices(MSTransportable& p, std::vector<MSTransportableDevice*>& into) {
53 if (p.getParameter().wasSet(VEHPARS_FORCE_REROUTE) || equippedByDefaultAssignmentOptions(oc, "rerouting", p, false, true)) {
54 // route computation is enabled
55 const SUMOTime period = string2time(oc.getString("person-device.rerouting.period"));
57 // build the device
58 into.push_back(new MSTransportableDevice_Routing(p, "routing_" + p.getID(), period));
59 }
60}
61
62
63// ---------------------------------------------------------------------------
64// MSTransportableDevice_Routing-methods
65// ---------------------------------------------------------------------------
67 : MSTransportableDevice(holder, id), myPeriod(period), myLastRouting(-1), myRerouteCommand(0) {
69 // if we don't update the edge weights, we might as well reroute now and hopefully use our threads better
70 const SUMOTime execTime = MSRoutingEngine::hasEdgeUpdates() ? holder.getParameter().depart : -1;
72 // the event will deschedule and destroy itself so it does not need to be stored
73 }
74}
75
76
78 // make the rerouting command invalid if there is one
79 if (myRerouteCommand != nullptr) {
81 }
82}
83
84
87 reroute(currentTime);
88 return myPeriod;
89}
90
91
92void
93MSTransportableDevice_Routing::reroute(const SUMOTime currentTime, const bool /* onInit */) {
95 //check whether the weights did change since the last reroute
97 return;
98 }
99 myLastRouting = currentTime;
100// MSRoutingEngine::reroute(myHolder, currentTime, onInit);
101}
102
103
104std::string
105MSTransportableDevice_Routing::getParameter(const std::string& key) const {
106 if (key == "period") {
107 return time2string(myPeriod);
108 }
109 throw InvalidArgument("Parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
110}
111
112
113void
114MSTransportableDevice_Routing::setParameter(const std::string& key, const std::string& value) {
115 double doubleValue;
116 try {
117 doubleValue = StringUtils::toDouble(value);
118 } catch (NumberFormatException&) {
119 throw InvalidArgument("Setting parameter '" + key + "' requires a number for device of type '" + deviceName() + "'");
120 }
121 if (key == "period") {
122 const SUMOTime oldPeriod = myPeriod;
123 myPeriod = TIME2STEPS(doubleValue);
124 if (myPeriod <= 0) {
126 } else if (oldPeriod <= 0) {
127 // re-schedule routing command
129 }
130 } else {
131 throw InvalidArgument("Setting parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
132 }
133}
134
135
136void
140 std::vector<std::string> internals;
141 internals.push_back(toString(myPeriod));
142 out.writeAttr(SUMO_ATTR_STATE, toString(internals));
143 out.closeTag();
144}
145
146
147void
149 std::istringstream bis(attrs.getString(SUMO_ATTR_STATE));
150 bis >> myPeriod;
151}
152
153
154/****************************************************************************/
long long int SUMOTime
Definition: GUI.h:36
#define TL(string)
Definition: MsgHandler.h:287
SUMOTime string2time(const std::string &r)
convert string to SUMOTime
Definition: SUMOTime.cpp:46
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 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:148
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:202
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:183
MSEventControl * getInsertionEvents()
Returns the event control for insertion events.
Definition: MSNet.h:493
static SUMOTime getLastAdaptation()
Information when the last edge weight adaptation occurred.
static bool hasEdgeUpdates()
returns whether any edge weight updates will take place
static void initWeightUpdate()
intialize period edge weight update
static void initEdgeWeights(SUMOVehicleClass svc)
initialize the edge weights if not done before
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.
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.
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.
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
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.
Definition: OptionsCont.cpp:76
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:60
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:254
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.
bool wasSet(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
void deschedule()
Marks this Command as being descheduled.