Eclipse SUMO - Simulation of Urban MObility
Distribution_Parameterized.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 // Copyright (C) 2001-2024 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 /****************************************************************************/
19 // A distribution described by parameters such as the mean value and std-dev
20 /****************************************************************************/
21 #include <config.h>
22 
23 #include <cassert>
26 #include <utils/common/ToString.h>
29 
31 
32 
33 // ===========================================================================
34 // method definitions
35 // ===========================================================================
38  Distribution("") {
39  myParameter = {0., 0.};
40  parse(description, true);
41 }
42 
43 
44 Distribution_Parameterized::Distribution_Parameterized(const std::string& id, double mean, double deviation) :
45  Distribution(id) {
46  myParameter.push_back(mean);
47  myParameter.push_back(deviation);
48 }
49 
50 
51 Distribution_Parameterized::Distribution_Parameterized(const std::string& id, double mean, double deviation, double min, double max) :
52  Distribution(id) {
53  myParameter.push_back(mean);
54  myParameter.push_back(deviation);
55  myParameter.push_back(min);
56  myParameter.push_back(max);
57 }
58 
59 
61 
62 
63 void
64 Distribution_Parameterized::parse(const std::string& description, const bool hardFail) {
65  try {
66  const std::string distName = description.substr(0, description.find('('));
67  if (distName == "norm" || distName == "normc") {
68  const std::vector<std::string> params = StringTokenizer(description.substr(distName.size() + 1, description.size() - distName.size() - 2), ',').getVector();
69  myParameter.resize(params.size());
70  std::transform(params.begin(), params.end(), myParameter.begin(), StringUtils::toDouble);
71  setID(distName);
72  } else {
73  myParameter[0] = StringUtils::toDouble(description);
74  }
75  if (myParameter.size() == 1) {
76  myParameter.push_back(0.);
77  }
78  } catch (...) {
79  // set default distribution parameterized
80  myParameter = {0., 0.};
81  if (hardFail) {
82  throw ProcessError(TL("Invalid format of distribution parameterized"));
83  } else {
84  WRITE_ERROR(TL("Invalid format of distribution parameterized"));
85  }
86  }
87 }
88 
89 bool
90 Distribution_Parameterized::isValidDescription(const std::string& description) {
91  try {
92  Distribution_Parameterized dummy(description);
93  std::string error;
94  bool valid = dummy.isValid(error);
95  if (!valid) {
96  WRITE_ERROR(error);
97  }
98  return valid;
99  } catch (...) {
100  WRITE_ERROR(TL("Invalid format of distribution parameterized"));
101  return false;
102  }
103 }
104 
105 
106 double
108  if (myParameter[1] <= 0.) {
109  return myParameter[0];
110  }
111  double val = RandHelper::randNorm(myParameter[0], myParameter[1], which);
112  if (myParameter.size() > 2) {
113  const double min = myParameter[2];
114  const double max = getMax();
115  while (val < min || val > max) {
116  val = RandHelper::randNorm(myParameter[0], myParameter[1], which);
117  }
118  }
119  return val;
120 }
121 
122 
123 double
125  if (myParameter[1] <= 0.) {
126  return myParameter[0];
127  }
128  return myParameter.size() > 3 ? myParameter[3] : std::numeric_limits<double>::infinity();
129 }
130 
131 
132 std::vector<double>&
134  return myParameter;
135 }
136 
137 
138 const std::vector<double>&
140  return myParameter;
141 }
142 
143 
144 std::string
145 Distribution_Parameterized::toStr(std::streamsize accuracy) const {
146  if (myParameter[1] < 0) {
147  // only write simple speedFactor
148  return toString(myParameter[0]);
149  } else {
150  return (myParameter[1] == 0.
151  ? myID + "(" + toString(myParameter[0], accuracy) + "," + toString(myParameter[1], accuracy) + ")"
152  : myID + "(" + joinToString(myParameter, ",", accuracy) + ")");
153  }
154 }
155 
156 
157 bool
159  if (myParameter.size() > 2 && myParameter[1] != 0) {
160  if (myParameter[0] > getMax()) {
161  error = "distribution mean " + toString(myParameter[0]) + " is larger than upper boundary " + toString(getMax());
162  return false;
163  }
164  if (myParameter[0] < myParameter[2]) {
165  error = "distribution mean " + toString(myParameter[0]) + " is smaller than lower boundary " + toString(myParameter[2]);
166  return false;
167  }
168  }
169  return true;
170 }
171 
172 
173 /****************************************************************************/
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:304
#define TL(string)
Definition: MsgHandler.h:315
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=gPrecision)
Definition: ToString.h:283
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
double sample(SumoRNG *which=0) const
Draw a sample of the distribution.
void parse(const std::string &description, const bool hardFail)
Overwrite by parsable distribution description.
virtual ~Distribution_Parameterized()
Destructor.
double getMax() const
Returns the maximum value of this distribution.
Distribution_Parameterized(const std::string &description)
Constructor for any temporary distribution parsed directly from the description.
std::string toStr(std::streamsize accuracy) const
Returns the string representation of this distribution.
static bool isValidDescription(const std::string &description)
validate input description
std::vector< double > & getParameter()
Returns the parameters of this distribution.
bool isValid(std::string &error)
check whether the distribution is valid
std::vector< double > myParameter
The distribution's parameters.
std::string myID
The name of the object.
Definition: Named.h:125
virtual void setID(const std::string &newID)
resets the id
Definition: Named.h:82
static double randNorm(double mean, double variance, SumoRNG *rng=nullptr)
Access to a random number from a normal distribution.
Definition: RandHelper.cpp:137
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