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.org/sumo
3 // Copyright (C) 2001-2022 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 // ===========================================================================
36 Distribution_Parameterized::Distribution_Parameterized(const std::string& id, double mean, double deviation) :
37  Distribution(id) {
38  myParameter.push_back(mean);
39  myParameter.push_back(deviation);
40 }
41 
42 
43 Distribution_Parameterized::Distribution_Parameterized(const std::string& id, double mean, double deviation, double min, double max) :
44  Distribution(id) {
45  myParameter.push_back(mean);
46  myParameter.push_back(deviation);
47  myParameter.push_back(min);
48  myParameter.push_back(max);
49 }
50 
51 
53 
54 
55 void
56 Distribution_Parameterized::parse(const std::string& description, const bool hardFail) {
57  try {
58  const std::string distName = description.substr(0, description.find('('));
59  if (distName == "norm" || distName == "normc") {
60  std::vector<std::string> params = StringTokenizer(description.substr(distName.size() + 1, description.size() - distName.size() - 2), ',').getVector();
61  myParameter.resize(params.size());
62  std::transform(params.begin(), params.end(), myParameter.begin(), StringUtils::toDouble);
63  setID(distName);
64  } else {
65  myParameter[0] = StringUtils::toDouble(description);
66  }
67  if (myParameter.size() == 1) {
68  myParameter.push_back(0.);
69  }
70  } catch (...) {
71  // set default distribution parameterized
72  myParameter = {0., 0.};
73  if (hardFail) {
74  throw ProcessError("Invalid format of distribution parameterized");
75  } else {
76  WRITE_ERROR("Invalid format of distribution parameterized");
77  }
78  }
79 }
80 
81 bool
82 Distribution_Parameterized::isValidDescription(const std::string& description) {
83  Distribution_Parameterized dummy("", 0, 0);
84  try {
85  dummy.parse(description, true);
86  std::string error;
87  bool valid = dummy.isValid(error);
88  if (!valid) {
89  WRITE_ERROR(error);
90  }
91  return valid;
92  } catch (...) {
93  WRITE_ERROR("Invalid format of distribution parameterized");
94  return false;
95  }
96 }
97 
98 
99 double
101  if (myParameter[1] <= 0.) {
102  return myParameter[0];
103  }
104  double val = RandHelper::randNorm(myParameter[0], myParameter[1], which);
105  if (myParameter.size() > 2) {
106  const double min = myParameter[2];
107  const double max = getMax();
108  while (val < min || val > max) {
109  val = RandHelper::randNorm(myParameter[0], myParameter[1], which);
110  }
111  }
112  return val;
113 }
114 
115 
116 double
118  if (myParameter[1] <= 0.) {
119  return myParameter[0];
120  }
121  return myParameter.size() > 3 ? myParameter[3] : std::numeric_limits<double>::infinity();
122 }
123 
124 
125 std::vector<double>&
127  return myParameter;
128 }
129 
130 
131 const std::vector<double>&
133  return myParameter;
134 }
135 
136 
137 std::string
138 Distribution_Parameterized::toStr(std::streamsize accuracy) const {
139  if (myParameter[1] < 0) {
140  // only write simple speedFactor
141  return toString(myParameter[0]);
142  } else {
143  return (myParameter[1] == 0.
144  ? myID + "(" + toString(myParameter[0], accuracy) + "," + toString(myParameter[1], accuracy) + ")"
145  : myID + "(" + joinToString(myParameter, ",", accuracy) + ")");
146  }
147 }
148 
149 
150 bool
152  if (myParameter.size() > 2 && myParameter[1] != 0) {
153  if (myParameter[0] > getMax()) {
154  error = "distribution mean " + toString(myParameter[0]) + " is larger than upper boundary " + toString(getMax());
155  return false;
156  }
157  if (myParameter[0] < myParameter[2]) {
158  error = "distribution mean " + toString(myParameter[0]) + " is smaller than lower boundary " + toString(myParameter[2]);
159  return false;
160  }
161  }
162  return true;
163 }
164 
165 
166 /****************************************************************************/
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:288
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=gPrecision)
Definition: ToString.h:269
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.
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
Distribution_Parameterized(const std::string &id, double mean, double deviation)
Constructor for standard normal distribution.
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:82
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