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