SUMO - Simulation of Urban MObility
MSPersonDevice_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-2018 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 /****************************************************************************/
19 // A device that performs vehicle rerouting based on current edge speeds
20 /****************************************************************************/
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include <microsim/MSNet.h>
32 #include "MSRoutingEngine.h"
33 #include "MSPersonDevice_Routing.h"
34 
35 
36 // ===========================================================================
37 // method definitions
38 // ===========================================================================
39 // ---------------------------------------------------------------------------
40 // static initialisation methods
41 // ---------------------------------------------------------------------------
42 void
44  insertDefaultAssignmentOptions("rerouting", "Routing", oc, true);
45  oc.doRegister("person-device.rerouting.period", new Option_String("0", "TIME"));
46  oc.addSynonyme("person-device.rerouting.period", "person-device.routing.period", true);
47  oc.addDescription("person-device.rerouting.period", "Routing", "The period with which the person shall be rerouted");
48 }
49 
50 
51 void
52 MSPersonDevice_Routing::buildDevices(MSTransportable& p, std::vector<MSPersonDevice*>& into) {
54  if (p.getParameter().wasSet(VEHPARS_FORCE_REROUTE) || equippedByDefaultAssignmentOptions(oc, "rerouting", p, false)) {
55  // route computation is enabled
56  const SUMOTime period = string2time(oc.getString("person-device.rerouting.period"));
58  // build the device
59  into.push_back(new MSPersonDevice_Routing(p, "routing_" + p.getID(), period));
60  }
61 }
62 
63 
64 // ---------------------------------------------------------------------------
65 // MSPersonDevice_Routing-methods
66 // ---------------------------------------------------------------------------
68  : MSPersonDevice(holder, id), myPeriod(period), myLastRouting(-1), myRerouteCommand(0) {
70  // if we don't update the edge weights, we might as well reroute now and hopefully use our threads better
71  const SUMOTime execTime = MSRoutingEngine::hasEdgeUpdates() ? holder.getParameter().depart : -1;
73  // the event will deschedule and destroy itself so it does not need to be stored
74  }
75 }
76 
77 
79  // make the rerouting command invalid if there is one
80  if (myRerouteCommand != nullptr) {
82  }
83 }
84 
85 
88  reroute(currentTime);
89  return myPeriod;
90 }
91 
92 
93 void
94 MSPersonDevice_Routing::reroute(const SUMOTime currentTime, const bool /* onInit */) {
96  //check whether the weights did change since the last reroute
98  return;
99  }
100  myLastRouting = currentTime;
101 // MSRoutingEngine::reroute(myHolder, currentTime, onInit);
102 }
103 
104 
105 std::string
106 MSPersonDevice_Routing::getParameter(const std::string& key) const {
107  if (key == "period") {
108  return time2string(myPeriod);
109  }
110  throw InvalidArgument("Parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
111 }
112 
113 
114 void
115 MSPersonDevice_Routing::setParameter(const std::string& key, const std::string& value) {
116  double doubleValue;
117  try {
118  doubleValue = StringUtils::toDouble(value);
119  } catch (NumberFormatException&) {
120  throw InvalidArgument("Setting parameter '" + key + "' requires a number for device of type '" + deviceName() + "'");
121  }
122  if (key == "period") {
123  const SUMOTime oldPeriod = myPeriod;
124  myPeriod = TIME2STEPS(doubleValue);
125  if (myPeriod <= 0) {
127  } else if (oldPeriod <= 0) {
128  // re-schedule routing command
130  }
131  } else {
132  throw InvalidArgument("Setting parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
133  }
134 }
135 
136 
137 void
140  out.writeAttr(SUMO_ATTR_ID, getID());
141  std::vector<std::string> internals;
142  internals.push_back(toString(myPeriod));
143  out.writeAttr(SUMO_ATTR_STATE, toString(internals));
144  out.closeTag();
145 }
146 
147 
148 void
150  std::istringstream bis(attrs.getString(SUMO_ATTR_STATE));
151  bis >> myPeriod;
152 }
153 
154 
155 /****************************************************************************/
static void buildDevices(MSTransportable &p, std::vector< MSPersonDevice *> &into)
Build devices for the given person, if needed.
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:75
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:256
Abstract in-person device.
long long int SUMOTime
Definition: SUMOTime.h:36
const int VEHPARS_FORCE_REROUTE
static void initWeightUpdate()
intialize period edge weight update
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 time2string(SUMOTime t)
Definition: SUMOTime.cpp:65
WrappingCommand< MSPersonDevice_Routing > * myRerouteCommand
The (optional) command responsible for rerouting.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:165
const std::string & getID() const
Returns the id.
Definition: Named.h:78
#define TIME2STEPS(x)
Definition: SUMOTime.h:60
const SUMOVehicleParameter & getParameter() const
static void initEdgeWeights()
initialize the edge weights if not done before
SUMOTime myPeriod
The period with which a vehicle shall be rerouted.
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
virtual std::string getString(int id) const =0
Returns the string-value of the named (by its enum-value) attribute.
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:96
The state of a link.
static bool hasEdgeUpdates()
returns whether any routing actions take place
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:49
Encapsulated SAX-Attributes.
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter ...
#define SIMSTEP
Definition: SUMOTime.h:64
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
SUMOTime depart
The vehicle&#39;s departure time.
virtual void addEvent(Command *operation, SUMOTime execTimeStep=-1)
Adds an Event.
std::string getParameter(const std::string &key) const
try to retrieve the given parameter from this device. Throw exception for unsupported key ...
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:42
const std::string & getID() const
returns the id of the transportable
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:121
static SUMOTime getLastAdaptation()
Information when the last edge weight adaptation occurred.
void deschedule()
Marks this Command as being descheduled.
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:208
static void insertOptions(OptionsCont &oc)
Inserts MSPersonDevice_Routing-options.
A storage for options typed value containers)
Definition: OptionsCont.h:92
MSEventControl * getInsertionEvents()
Returns the event control for insertion events.
Definition: MSNet.h:429
void loadState(const SUMOSAXAttributes &attrs)
Loads the state of the device from the given description.
bool wasSet(int what) const
Returns whether the given parameter was set.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:64
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
SUMOTime myLastRouting
The last time a routing took place.
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
void reroute(const SUMOTime currentTime, const bool onInit=false)
initiate the rerouting, create router / thread pool on first use
SUMOTime wrappedRerouteCommandExecute(SUMOTime currentTime)
Performs rerouting after a period.
MSPersonDevice_Routing(MSTransportable &holder, const std::string &id, SUMOTime period)
Constructor.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
const std::string deviceName() const
return the name for this type of device
void saveState(OutputDevice &out) const
Saves the state of the device.