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-2020 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 
82 double
83 Distribution_Parameterized::sample(std::mt19937* which) const {
84  if (myParameter[1] == 0.) {
85  return myParameter[0];
86  }
87  double val = RandHelper::randNorm(myParameter[0], myParameter[1], which);
88  if (myParameter.size() > 2) {
89  const double min = myParameter[2];
90  const double max = getMax();
91  while (val < min || val > max) {
92  val = RandHelper::randNorm(myParameter[0], myParameter[1], which);
93  }
94  }
95  return val;
96 }
97 
98 
99 double
101  if (myParameter[1] == 0.) {
102  return myParameter[0];
103  }
104  return myParameter.size() > 3 ? myParameter[3] : std::numeric_limits<double>::infinity();
105 }
106 
107 
108 std::vector<double>&
110  return myParameter;
111 }
112 
113 
114 const std::vector<double>&
116  return myParameter;
117 }
118 
119 
120 std::string
121 Distribution_Parameterized::toStr(std::streamsize accuracy) const {
122  if (myParameter[1] < 0) {
123  // only write simple speedFactor
124  return toString(myParameter[0]);
125  } else {
126  return (myParameter[1] == 0.
127  ? myID + "(" + toString(myParameter[0], accuracy) + "," + toString(myParameter[1], accuracy) + ")"
128  : myID + "(" + joinToString(myParameter, ",", accuracy) + ")");
129  }
130 }
131 
132 
133 bool
135  if (myParameter.size() > 2 && myParameter[1] != 0) {
136  if (myParameter[0] > getMax()) {
137  error = "distribution mean " + toString(myParameter[0]) + " is larger than upper boundary " + toString(getMax());
138  return false;
139  }
140  if (myParameter[0] < myParameter[2]) {
141  error = "distribution mean " + toString(myParameter[0]) + " is smaller than lower boundary " + toString(myParameter[2]);
142  return false;
143  }
144  }
145  return true;
146 }
147 
148 
149 /****************************************************************************/
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:284
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=gPrecision)
Definition: ToString.h:250
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:44
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.
double sample(std::mt19937 *which=0) const
Draw a sample of the distribution.
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:124
virtual void setID(const std::string &newID)
resets the id
Definition: Named.h:81
static double randNorm(double mean, double variance, std::mt19937 *rng=0)
Access to a random number from a normal distribution.
Definition: RandHelper.h:133
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