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.org/sumo
3 // Copyright (C) 2007-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
18 // A device that performs vehicle rerouting based on current edge speeds
19 /****************************************************************************/
20 
21 // ===========================================================================
22 // included modules
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 // ---------------------------------------------------------------------------
41 void
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", "The period with which the person shall be rerouted");
47 }
48 
49 
50 void
51 MSTransportableDevice_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 
92 void
93 MSTransportableDevice_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 
104 std::string
105 MSTransportableDevice_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 
113 void
114 MSTransportableDevice_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 
136 void
139  out.writeAttr(SUMO_ATTR_ID, getID());
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 
147 void
149  std::istringstream bis(attrs.getString(SUMO_ATTR_STATE));
150  bis >> myPeriod;
151 }
152 
153 
154 /****************************************************************************/
MSTransportableDevice_Routing::myPeriod
SUMOTime myPeriod
The period with which a vehicle shall be rerouted.
Definition: MSTransportableDevice_Routing.h:142
SUMOVehicleParameter::wasSet
bool wasSet(int what) const
Returns whether the given parameter was set.
Definition: SUMOVehicleParameter.h:312
MSTransportableDevice_Routing::insertOptions
static void insertOptions(OptionsCont &oc)
Inserts MSTransportableDevice_Routing-options.
Definition: MSTransportableDevice_Routing.cpp:42
MSTransportableDevice_Routing::setParameter
void setParameter(const std::string &key, const std::string &value)
try to set the given parameter for this device. Throw exception for unsupported key
Definition: MSTransportableDevice_Routing.cpp:114
MSEventControl::addEvent
virtual void addEvent(Command *operation, SUMOTime execTimeStep=-1)
Adds an Event.
Definition: MSEventControl.cpp:52
MSNet.h
SUMOSAXAttributes::getString
virtual std::string getString(int id) const =0
Returns the string-value of the named (by its enum-value) attribute.
OutputDevice
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:63
MSRoutingEngine::getLastAdaptation
static SUMOTime getLastAdaptation()
Information when the last edge weight adaptation occurred.
Definition: MSRoutingEngine.h:77
OptionsCont.h
StringUtils::toDouble
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
Definition: StringUtils.cpp:345
VEHPARS_FORCE_REROUTE
const int VEHPARS_FORCE_REROUTE
Definition: SUMOVehicleParameter.h:62
OptionsCont::getString
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
Definition: OptionsCont.cpp:201
WrappingCommand::deschedule
void deschedule()
Marks this Command as being descheduled.
Definition: WrappingCommand.h:78
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:34
MSTransportableDevice_Routing::buildDevices
static void buildDevices(MSTransportable &p, std::vector< MSTransportableDevice * > &into)
Build devices for the given person, if needed.
Definition: MSTransportableDevice_Routing.cpp:51
WrappingCommand< MSTransportableDevice_Routing >
MSTransportableDevice_Routing::loadState
void loadState(const SUMOSAXAttributes &attrs)
Loads the state of the device from the given description.
Definition: MSTransportableDevice_Routing.cpp:148
OptionsCont::getOptions
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:57
MSTransportableDevice_Routing::deviceName
const std::string deviceName() const
return the name for this type of device
Definition: MSTransportableDevice_Routing.h:87
MSTransportableDevice_Routing.h
SUMO_ATTR_ID
@ SUMO_ATTR_ID
Definition: SUMOXMLDefinitions.h:378
MSTransportable
Definition: MSTransportable.h:58
SUMOVehicleParameter::depart
SUMOTime depart
Definition: SUMOVehicleParameter.h:482
OutputDevice::closeTag
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
Definition: OutputDevice.cpp:253
NumberFormatException
Definition: UtilExceptions.h:95
OutputDevice::writeAttr
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:255
MSRoutingEngine::initEdgeWeights
static void initEdgeWeights()
initialize the edge weights if not done before
Definition: MSRoutingEngine.cpp:93
OptionsCont::addDescription
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
Definition: OptionsCont.cpp:469
MSDevice::insertDefaultAssignmentOptions
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:126
TIME2STEPS
#define TIME2STEPS(x)
Definition: SUMOTime.h:58
OptionsCont::addSynonyme
void addSynonyme(const std::string &name1, const std::string &name2, bool isDeprecated=false)
Adds a synonyme for an options name (any order)
Definition: OptionsCont.cpp:95
MSTransportableDevice_Routing::getParameter
std::string getParameter(const std::string &key) const
try to retrieve the given parameter from this device. Throw exception for unsupported key
Definition: MSTransportableDevice_Routing.cpp:105
OptionsCont::doRegister
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:74
SUMO_TAG_DEVICE
@ SUMO_TAG_DEVICE
Definition: SUMOXMLDefinitions.h:271
time2string
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:67
Option_String
Definition: Option.h:399
OptionsCont
A storage for options typed value containers)
Definition: OptionsCont.h:89
SIMSTEP
#define SIMSTEP
Definition: SUMOTime.h:62
MSRoutingEngine.h
MSTransportableDevice
Abstract in-person device.
Definition: MSTransportableDevice.h:52
string2time
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:44
MSRoutingEngine::initWeightUpdate
static void initWeightUpdate()
intialize period edge weight update
Definition: MSRoutingEngine.cpp:69
MSTransportableDevice_Routing::reroute
void reroute(const SUMOTime currentTime, const bool onInit=false)
initiate the rerouting, create router / thread pool on first use
Definition: MSTransportableDevice_Routing.cpp:93
MSTransportableDevice_Routing::myRerouteCommand
WrappingCommand< MSTransportableDevice_Routing > * myRerouteCommand
The (optional) command responsible for rerouting.
Definition: MSTransportableDevice_Routing.h:148
OutputDevice::openTag
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
Definition: OutputDevice.cpp:239
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:47
SUMO_ATTR_STATE
@ SUMO_ATTR_STATE
The state of a link.
Definition: SUMOXMLDefinitions.h:708
MSRoutingEngine::hasEdgeUpdates
static bool hasEdgeUpdates()
returns whether any routing actions take place
Definition: MSRoutingEngine.h:72
MSNet::getInstance
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
MSDevice::equippedByDefaultAssignmentOptions
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:203
InvalidArgument
Definition: UtilExceptions.h:56
MSTransportable.h
MSNet::getInsertionEvents
MSEventControl * getInsertionEvents()
Returns the event control for insertion events.
Definition: MSNet.h:449
MSTransportableDevice_Routing::wrappedRerouteCommandExecute
SUMOTime wrappedRerouteCommandExecute(SUMOTime currentTime)
Performs rerouting after a period.
Definition: MSTransportableDevice_Routing.cpp:86
MSTransportable::getID
const std::string & getID() const
returns the id of the transportable
Definition: MSTransportable.cpp:699
SUMOSAXAttributes.h
config.h
MSTransportableDevice_Routing::myLastRouting
SUMOTime myLastRouting
The last time a routing took place.
Definition: MSTransportableDevice_Routing.h:145
MSEventControl.h
MSTransportableDevice_Routing::MSTransportableDevice_Routing
MSTransportableDevice_Routing(MSTransportable &holder, const std::string &id, SUMOTime period)
Constructor.
Definition: MSTransportableDevice_Routing.cpp:66
MSTransportableDevice_Routing::saveState
void saveState(OutputDevice &out) const
Saves the state of the device.
Definition: MSTransportableDevice_Routing.cpp:137
SUMOSAXAttributes
Encapsulated SAX-Attributes.
Definition: SUMOSAXAttributes.h:56
MSTransportable::getParameter
const SUMOVehicleParameter & getParameter() const
Definition: MSTransportable.h:602
Named::getID
const std::string & getID() const
Returns the id.
Definition: Named.h:76
MSTransportableDevice_Routing::~MSTransportableDevice_Routing
~MSTransportableDevice_Routing()
Destructor.
Definition: MSTransportableDevice_Routing.cpp:77