Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
MSDevice.h
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2007-2026 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/****************************************************************************/
20// Abstract in-vehicle device
21/****************************************************************************/
22#pragma once
23#include <config.h>
24
25#include <string>
26#include <vector>
27#include <map>
28#include <set>
29#include <random>
31#include <microsim/MSNet.h>
34#include <utils/common/Named.h>
38
39
40// ===========================================================================
41// class declarations
42// ===========================================================================
43class OutputDevice;
44class SUMOVehicle;
45class MSTransportable;
47class MSVehicleDevice;
49
50
51// ===========================================================================
52// class definitions
53// ===========================================================================
62class MSDevice : public Named {
63public:
67 static void insertOptions(OptionsCont& oc);
68
72 static bool checkOptions(OptionsCont& oc);
73
74
80 static void buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDevice*>& into);
81
87 static void buildTransportableDevices(MSTransportable& p, std::vector<MSTransportableDevice*>& into);
88
90 static std::string getDeviceName(const std::string& id);
91
93 return &myEquipmentRNG;
94 }
95
97 virtual const std::string deviceName() const = 0;
98
100 static void cleanupAll();
101
102 static const std::string LOADSTATE_DEVICENAMES;
103
104public:
109 MSDevice(const std::string& id) : Named(id) {
110 }
111
112
114 virtual ~MSDevice() { }
115
116
129 virtual void generateOutput(OutputDevice* /*tripinfoOut*/) const {
130 }
131
137 virtual void saveState(OutputDevice& out) const;
138
144 virtual void loadState(const SUMOSAXAttributes& attrs);
145
147 virtual std::string getParameter(const std::string& key) const {
148 throw InvalidArgument("Parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
149 }
150
152 virtual void setParameter(const std::string& key, const std::string& value) {
153 UNUSED_PARAMETER(value);
154 throw InvalidArgument("Setting parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
155 }
156
157protected:
160
167 static void insertDefaultAssignmentOptions(const std::string& deviceName, const std::string& optionsTopic, OptionsCont& oc, const bool isPerson = false);
168
169
176 template<class DEVICEHOLDER>
177 static bool equippedByDefaultAssignmentOptions(const OptionsCont& oc, const std::string& deviceName, DEVICEHOLDER& v, bool outputOptionSet, const bool isPerson = false);
179
180
181private:
183 static std::map<std::string, std::set<std::string> > myExplicitIDs;
184
187 std::string prefix;
188 std::string parameterKey;
189 std::string probabilityKey;
190 bool deterministic = false;
191 bool numberGiven = false;
192 double probability = 0;
193 bool nameGiven = false;
194 };
195 static const DefaultAssignment& getDefaultAssignment(const OptionsCont& oc, const std::string& deviceName, const bool isPerson);
196 static std::map<std::string, DefaultAssignment> myDefaultAssignments[2];
197
200
201
202private:
205
208
209};
210
211
212template<class DEVICEHOLDER> bool
213MSDevice::equippedByDefaultAssignmentOptions(const OptionsCont& oc, const std::string& deviceName, DEVICEHOLDER& v, bool outputOptionSet, const bool isPerson) {
214 const DefaultAssignment& defaults = getDefaultAssignment(oc, deviceName, isPerson);
215 const std::string& prefix = defaults.prefix;
216 // assignment by number
217 bool numberGiven = defaults.numberGiven;
218 double probability = defaults.probability;
219 // assignment by name
220 bool haveByName = false;
221 const bool nameGiven = defaults.nameGiven;
222 if (nameGiven) {
223 if (myExplicitIDs.find(deviceName) == myExplicitIDs.end()) {
224 myExplicitIDs[deviceName] = std::set<std::string>();
225 const std::vector<std::string> idList = OptionsCont::getOptions().getStringVector(prefix + ".explicit");
226 myExplicitIDs[deviceName].insert(idList.begin(), idList.end());
227 }
228 haveByName = myExplicitIDs[deviceName].count(v.getID()) > 0;
229 }
230 // assignment by abstract parameters
231 bool haveByParameter = false;
232 bool parameterGiven = false;
233 const std::string& key = defaults.parameterKey;
234 if (v.getParameter().hasParameter(key)) {
235 parameterGiven = true;
236 haveByParameter = StringUtils::toBool(v.getParameter().getParameter(key, "false"));
237 } else if (v.getVehicleType().getParameter().hasParameter(key)) {
238 parameterGiven = true;
239 haveByParameter = StringUtils::toBool(v.getVehicleType().getParameter().getParameter(key, "false"));
240 } else if (v.getVehicleType().getParameter().hasParameter(defaults.probabilityKey)) {
241 // override global options
242 numberGiven = true;
243 probability = StringUtils::toDouble(v.getVehicleType().getParameter().getParameter(defaults.probabilityKey, "0"));
244 }
245 //std::cout << " deviceName=" << deviceName << " holder=" << v.getID()
246 // << " nameGiven=" << nameGiven << " haveByName=" << haveByName
247 // << " parameterGiven=" << parameterGiven << " haveByParameter=" << haveByParameter
248 // << " numberGiven=" << numberGiven << " haveByNumber=" << haveByNumber
249 // << " outputOptionSet=" << outputOptionSet << "\n";
250 if (haveByName) {
251 return true;
252 } else if (parameterGiven) {
253 return haveByParameter;
254 } else if (numberGiven) {
255 if (defaults.deterministic) {
256 return MSNet::getInstance()->getVehicleControl().getQuota(probability) == 1;
257 } else if (probability > 0) {
258 if (v.getParameter().hasParameter(LOADSTATE_DEVICENAMES)) {
259 // replicate probabilistic assignment
260 const std::vector<std::string> lsdn = StringTokenizer(v.getParameter().getParameter(LOADSTATE_DEVICENAMES)).getVector();
261 return std::find(lsdn.begin(), lsdn.end(), deviceName) != lsdn.end();
262 } else {
263 return RandHelper::rand(&myEquipmentRNG) < probability;
264 }
265 } else {
266 return false;
267 }
268 } else {
269 return !nameGiven && outputOptionSet;
270 }
271}
Abstract in-vehicle / in-person device.
Definition MSDevice.h:62
static const std::string LOADSTATE_DEVICENAMES
Definition MSDevice.h:102
static const DefaultAssignment & getDefaultAssignment(const OptionsCont &oc, const std::string &deviceName, const bool isPerson)
Definition MSDevice.cpp:161
static SumoRNG * getEquipmentRNG()
Definition MSDevice.h:92
virtual void loadState(const SUMOSAXAttributes &attrs)
Loads the state of the device from the given description.
Definition MSDevice.cpp:217
static std::map< std::string, DefaultAssignment > myDefaultAssignments[2]
Definition MSDevice.h:196
virtual const std::string deviceName() const =0
return the name for this type of device
virtual void saveState(OutputDevice &out) const
Saves the state of the device.
Definition MSDevice.cpp:211
virtual void generateOutput(OutputDevice *) const
Called on vehicle deletion to extend tripinfo and other outputs.
Definition MSDevice.h:129
virtual ~MSDevice()
Destructor.
Definition MSDevice.h:114
MSDevice(const MSDevice &)
Invalidated copy constructor.
virtual 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 MSDevice.h:152
static std::string getDeviceName(const std::string &id)
extracts the deviceName from the id (which includes holder id) and is subject to special cases
Definition MSDevice.cpp:197
static void insertOptions(OptionsCont &oc)
Inserts options for building devices.
Definition MSDevice.cpp:78
static SumoRNG myEquipmentRNG
A random number generator used to choose from vtype/route distributions and computing the speed facto...
Definition MSDevice.h:199
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
Definition MSDevice.cpp:115
MSDevice(const std::string &id)
Constructor.
Definition MSDevice.h:109
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:181
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:213
virtual std::string getParameter(const std::string &key) const
try to retrieve the given parameter from this device. Throw exception for unsupported key
Definition MSDevice.h:147
static std::map< std::string, std::set< std::string > > myExplicitIDs
vehicles which explicitly carry a device, sorted by device, first
Definition MSDevice.h:183
static void cleanupAll()
perform cleanup for all devices
Definition MSDevice.cpp:150
MSDevice & operator=(const MSDevice &)
Invalidated assignment operator.
static void buildTransportableDevices(MSTransportable &p, std::vector< MSTransportableDevice * > &into)
Build devices for the given person, if needed.
Definition MSDevice.cpp:140
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition MSNet.cpp:201
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition MSNet.h:402
Abstract in-person device.
int getQuota(double frac=-1, int loaded=-1) const
Returns the number of instances of the current vehicle that shall be emitted considering that "frac" ...
Abstract in-vehicle device.
Base class for objects which have an id.
Definition Named.h:53
A storage for options typed value containers)
Definition OptionsCont.h:89
const StringVector & getStringVector(const std::string &name) const
Returns the list of string-value of the named option (only for Option_StringVector)
static OptionsCont & getOptions()
Retrieves the options.
Static storage of an output device and its base (abstract) implementation.
static double rand(SumoRNG *rng=nullptr)
Returns a random real number in [0, 1)
Encapsulated SAX-Attributes.
Representation of a vehicle.
Definition SUMOVehicle.h:63
std::vector< std::string > getVector()
return vector of strings
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
static bool toBool(const std::string &sData)
converts a string into the bool value described by it by calling the char-type converter
#define UNUSED_PARAMETER(x)
bool checkOptions()
the option values of a device's default assignment (all callers pass the global options,...
Definition MSDevice.h:186