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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
15 // A distribution described by parameters such as the mean value and std-dev
16 /****************************************************************************/
17 
18 
19 // ===========================================================================
20 // included modules
21 // ===========================================================================
22 #include <config.h>
23 
24 #include <cassert>
27 #include <utils/common/ToString.h>
30 
32 
33 
34 // ===========================================================================
35 // method definitions
36 // ===========================================================================
37 Distribution_Parameterized::Distribution_Parameterized(const std::string& id, double mean, double deviation) :
38  Distribution(id) {
39  myParameter.push_back(mean);
40  myParameter.push_back(deviation);
41 }
42 
43 
44 Distribution_Parameterized::Distribution_Parameterized(const std::string& id, double mean, double deviation, double min, double max) :
45  Distribution(id) {
46  myParameter.push_back(mean);
47  myParameter.push_back(deviation);
48  myParameter.push_back(min);
49  myParameter.push_back(max);
50 }
51 
52 
54 
55 
56 void
57 Distribution_Parameterized::parse(const std::string& description, const bool hardFail) {
58  try {
59  const std::string distName = description.substr(0, description.find('('));
60  if (distName == "norm" || distName == "normc") {
61  std::vector<std::string> params = StringTokenizer(description.substr(distName.size() + 1, description.size() - distName.size() - 2), ',').getVector();
62  myParameter.resize(params.size());
63  std::transform(params.begin(), params.end(), myParameter.begin(), StringUtils::toDouble);
64  setID(distName);
65  } else {
66  myParameter[0] = StringUtils::toDouble(description);
67  }
68  if (myParameter.size() == 1) {
69  myParameter.push_back(0.);
70  }
71  } catch (...) {
72  // set default distribution parameterized
73  myParameter = {0., 0.};
74  if (hardFail) {
75  throw ProcessError("Invalid format of distribution parameterized");
76  } else {
77  WRITE_ERROR("Invalid format of distribution parameterized");
78  }
79  }
80 }
81 
82 
83 double
84 Distribution_Parameterized::sample(std::mt19937* which) const {
85  if (myParameter[1] == 0.) {
86  return myParameter[0];
87  }
88  double val = RandHelper::randNorm(myParameter[0], myParameter[1], which);
89  if (myParameter.size() > 2) {
90  const double min = myParameter[2];
91  const double max = getMax();
92  while (val < min || val > max) {
93  val = RandHelper::randNorm(myParameter[0], myParameter[1], which);
94  }
95  }
96  return val;
97 }
98 
99 
100 double
102  if (myParameter[1] == 0.) {
103  return myParameter[0];
104  }
105  return myParameter.size() > 3 ? myParameter[3] : std::numeric_limits<double>::infinity();
106 }
107 
108 
109 std::vector<double>&
111  return myParameter;
112 }
113 
114 
115 const std::vector<double>&
117  return myParameter;
118 }
119 
120 
121 std::string
122 Distribution_Parameterized::toStr(std::streamsize accuracy) const {
123  if (myParameter[1] < 0) {
124  // only write simple speedFactor
125  return toString(myParameter[0]);
126  } else {
127  return (myParameter[1] == 0.
128  ? myID + "(" + toString(myParameter[0], accuracy) + "," + toString(myParameter[1], accuracy) + ")"
129  : myID + "(" + joinToString(myParameter, ",", accuracy) + ")");
130  }
131 }
132 
133 
134 bool
136  if (myParameter.size() > 2 && myParameter[1] != 0) {
137  if (myParameter[0] > getMax()) {
138  error = "distribution mean " + toString(myParameter[0]) + " is larger than upper boundary " + toString(getMax());
139  return false;
140  }
141  if (myParameter[0] < myParameter[2]) {
142  error = "distribution mean " + toString(myParameter[0]) + " is smaller than lower boundary " + toString(myParameter[2]);
143  return false;
144  }
145  }
146  return true;
147 }
148 
149 /****************************************************************************/
150 
ToString.h
Distribution
Definition: Distribution.h:37
Distribution_Parameterized::~Distribution_Parameterized
virtual ~Distribution_Parameterized()
Destructor.
Definition: Distribution_Parameterized.cpp:53
Distribution_Parameterized::sample
double sample(std::mt19937 *which=0) const
Draw a sample of the distribution.
Definition: Distribution_Parameterized.cpp:84
StringUtils::toDouble
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
Definition: StringUtils.cpp:345
MsgHandler.h
Distribution_Parameterized::isValid
bool isValid(std::string &error)
check whether the distribution is valid
Definition: Distribution_Parameterized.cpp:135
RandHelper::randNorm
static double randNorm(double mean, double variance, std::mt19937 *rng=0)
Access to a random number from a normal distribution.
Definition: RandHelper.h:135
Distribution_Parameterized::myParameter
std::vector< double > myParameter
The distribution's parameters.
Definition: Distribution_Parameterized.h:83
Distribution_Parameterized::getParameter
std::vector< double > & getParameter()
Returns the parameters of this distribution.
Definition: Distribution_Parameterized.cpp:110
StringTokenizer
Definition: StringTokenizer.h:61
Distribution_Parameterized::getMax
double getMax() const
Returns the maximum value of this distribution.
Definition: Distribution_Parameterized.cpp:101
ProcessError
Definition: UtilExceptions.h:39
Distribution_Parameterized.h
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:47
StringUtils.h
Distribution_Parameterized::toStr
std::string toStr(std::streamsize accuracy) const
Returns the string representation of this distribution.
Definition: Distribution_Parameterized.cpp:122
Distribution_Parameterized::Distribution_Parameterized
Distribution_Parameterized(const std::string &id, double mean, double deviation)
Constructor for standard normal distribution.
Definition: Distribution_Parameterized.cpp:37
joinToString
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=gPrecision)
Definition: ToString.h:246
StringTokenizer::getVector
std::vector< std::string > getVector()
return vector of strings
Definition: StringTokenizer.cpp:191
config.h
RandHelper.h
StringTokenizer.h
Distribution_Parameterized::parse
void parse(const std::string &description, const bool hardFail)
Overwrite by parsable distribution description.
Definition: Distribution_Parameterized.cpp:57
Named::myID
std::string myID
The name of the object.
Definition: Named.h:133
WRITE_ERROR
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:283
Named::setID
void setID(const std::string &newID)
resets the id
Definition: Named.h:84