SUMO - Simulation of Urban MObility
MSDevice.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2013-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
18 // Abstract in-vehicle device
19 /****************************************************************************/
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #ifdef _MSC_VER
25 #include <windows_config.h>
26 #else
27 #include <config.h>
28 #endif
29 
32 #include <microsim/MSVehicle.h>
34 #include "MSDevice.h"
35 #include "MSDevice_Vehroutes.h"
36 #include "MSDevice_Tripinfo.h"
37 #include "MSDevice_Routing.h"
38 #include "MSDevice_Emissions.h"
39 #include "MSDevice_BTreceiver.h"
40 #include "MSDevice_BTsender.h"
41 #include "MSDevice_Example.h"
42 #include "MSDevice_Battery.h"
43 #include "MSDevice_SSM.h"
44 #include "MSDevice_Bluelight.h"
45 
46 
47 // ===========================================================================
48 // static member variables
49 // ===========================================================================
50 std::map<std::string, std::set<std::string> > MSDevice::myExplicitIDs;
51 std::mt19937 MSDevice::myEquipmentRNG;
52 
53 // ===========================================================================
54 // method definitions
55 // ===========================================================================
56 // ---------------------------------------------------------------------------
57 // static initialisation methods
58 // ---------------------------------------------------------------------------
59 void
69 }
70 
71 
72 bool
74  bool ok = true;
76  return ok;
77 }
78 
79 
80 void
81 MSDevice::buildVehicleDevices(SUMOVehicle& v, std::vector<MSDevice*>& into) {
92 }
93 
94 void
98 }
99 
100 void
101 MSDevice::insertDefaultAssignmentOptions(const std::string& deviceName, const std::string& optionsTopic, OptionsCont& oc) {
102  oc.doRegister("device." + deviceName + ".probability", new Option_Float(0.));
103  oc.addDescription("device." + deviceName + ".probability", optionsTopic, "The probability for a vehicle to have a '" + deviceName + "' device");
104 
105  oc.doRegister("device." + deviceName + ".explicit", new Option_String());
106  oc.addSynonyme("device." + deviceName + ".explicit", "device." + deviceName + ".knownveh", true);
107  oc.addDescription("device." + deviceName + ".explicit", optionsTopic, "Assign a '" + deviceName + "' device to named vehicles");
108 
109  oc.doRegister("device." + deviceName + ".deterministic", new Option_Bool(false));
110  oc.addDescription("device." + deviceName + ".deterministic", optionsTopic, "The '" + deviceName + "' devices are set deterministic using a fraction of 1000");
111 }
112 
113 
114 bool
116  // assignment by number
117  bool haveByNumber = false;
118  if (oc.exists("device." + deviceName + ".deterministic") && oc.getBool("device." + deviceName + ".deterministic")) {
119  haveByNumber = MSNet::getInstance()->getVehicleControl().getQuota(oc.getFloat("device." + deviceName + ".probability")) == 1;
120  } else {
121  if (oc.exists("device." + deviceName + ".probability") && oc.getFloat("device." + deviceName + ".probability") != 0) {
122  haveByNumber = RandHelper::rand(&myEquipmentRNG) <= oc.getFloat("device." + deviceName + ".probability");
123  }
124  }
125  // assignment by name
126  bool haveByName = false;
127  if (oc.exists("device." + deviceName + ".explicit") && oc.isSet("device." + deviceName + ".explicit")) {
128  if (myExplicitIDs.find(deviceName) == myExplicitIDs.end()) {
129  myExplicitIDs[deviceName] = std::set<std::string>();
130  const std::vector<std::string> idList = OptionsCont::getOptions().getStringVector("device." + deviceName + ".explicit");
131  myExplicitIDs[deviceName].insert(idList.begin(), idList.end());
132  }
133  haveByName = myExplicitIDs[deviceName].count(v.getID()) > 0;
134  }
135  // assignment by abstract parameters
136  bool haveByParameter = false;
137  bool parameterGiven = false;
138  const std::string key = "has." + deviceName + ".device";
139  if (v.getParameter().knowsParameter(key)) {
140  parameterGiven = true;
141  haveByParameter = TplConvert::_2bool(v.getParameter().getParameter(key, "false").c_str());
142  } else if (v.getVehicleType().getParameter().knowsParameter(key)) {
143  parameterGiven = true;
144  haveByParameter = TplConvert::_2bool(v.getVehicleType().getParameter().getParameter(key, "false").c_str());
145  }
146  return (haveByNumber && !parameterGiven) || haveByName || haveByParameter;
147 }
148 
149 
150 void
152  WRITE_WARNING("Device '" + getID() + "' cannot save state");
153 }
154 
155 
156 void
158 }
159 
160 
161 /****************************************************************************/
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:81
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into)
Build devices for the given vehicle, if needed.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into)
Build devices for the given vehicle, if needed.
static bool _2bool(const E *const data)
converts a 0-terminated char-type array into the boolean value described by it
Definition: TplConvert.h:388
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into)
Build devices for the given vehicle, if needed.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_SSM-options.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into)
Build devices for the given vehicle, if needed.
static std::map< std::string, std::set< std::string > > myExplicitIDs
vehicles which explicitly carry a device, sorted by device, first
Definition: MSDevice.h:192
static double rand(std::mt19937 *rng=0)
Returns a random real number in [0, 1)
Definition: RandHelper.h:64
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into)
Build devices for the given vehicle, if needed.
int getQuota(double frac=-1) const
Returns the number of instances of the current vehicle that shall be emitted considering that "frac" ...
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
const std::string & getID() const
Returns the id.
Definition: Named.h:74
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_Example-options.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:199
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:64
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into)
Build devices for the given vehicle, if needed.
Definition: MSDevice.cpp:81
void addSynonyme(const std::string &name1, const std::string &name2, bool isDeprecated=false)
Adds a synonyme for an options name (any order)
virtual const std::string deviceName() const =0
return the name for this type of device
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into)
Build devices for the given vehicle, if needed.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_Routing-options.
bool knowsParameter(const std::string &key) const
Returns whether the parameter is known.
Representation of a vehicle.
Definition: SUMOVehicle.h:66
Encapsulated SAX-Attributes.
virtual void saveState(OutputDevice &out) const
Saves the state of the device.
Definition: MSDevice.cpp:151
static std::mt19937 myEquipmentRNG
A random number generator used to choose from vtype/route distributions and computing the speed facto...
Definition: MSDevice.h:195
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:306
bool exists(const std::string &name) const
Returns the information whether the named option is known.
std::vector< std::string > getStringVector(const std::string &name) const
Returns the list of string-vector-value of the named option (only for Option_String) ...
static void insertDefaultAssignmentOptions(const std::string &deviceName, const std::string &optionsTopic, OptionsCont &oc)
Adds common command options that allow to assign devices to vehicles.
Definition: MSDevice.cpp:101
static void cleanupAll()
perform cleanup for all devices
Definition: MSDevice.cpp:95
const SUMOVTypeParameter & getParameter() const
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into)
Build devices for the given vehicle, if needed.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_BTsender-options.
static void cleanup()
resets counters
static bool checkOptions(OptionsCont &oc)
check device-specific options
Definition: MSDevice.cpp:73
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_Bluelight-options.
static bool equippedByDefaultAssignmentOptions(const OptionsCont &oc, const std::string &deviceName, SUMOVehicle &v)
Determines whether a vehicle should get a certain device.
Definition: MSDevice.cpp:115
virtual void loadState(const SUMOSAXAttributes &attrs)
Loads the state of the device from the given description.
Definition: MSDevice.cpp:157
virtual const SUMOVehicleParameter & getParameter() const =0
Returns the vehicle&#39;s parameter (including departure definition)
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_Example-options.
static void insertOptions()
Inserts MSDevice_Emissions-options.
A storage for options typed value containers)
Definition: OptionsCont.h:98
const std::string getParameter(const std::string &key, const std::string &defaultValue="") const
Returns the value for a given key.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into)
Build devices for the given vehicle, if needed.
static void insertOptions(OptionsCont &oc)
Inserts options for building devices.
Definition: MSDevice.cpp:60
static MSDevice_Vehroutes * buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into, int maxRoutes=std::numeric_limits< int >::max())
Build devices for the given vehicle, if needed.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:70
static void cleanup()
deletes the router instance
static bool checkOptions(OptionsCont &oc)
checks MSDevice_Routing-options
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_BTreceiver-options.
virtual const std::string & getID() const =0
Get the vehicle&#39;s ID.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into)
Build devices for the given vehicle, if needed.
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle&#39;s type.