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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
18 // A distribution described by parameters such as the mean value and std-dev
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #ifdef _MSC_VER
26 #include <windows_config.h>
27 #else
28 #include <config.h>
29 #endif
30 
31 #include <cassert>
34 #include <utils/common/ToString.h>
37 
38 
39 // ===========================================================================
40 // method definitions
41 // ===========================================================================
43  double mean, double deviation)
44  : Distribution(id) {
45  myParameter.push_back(mean);
46  myParameter.push_back(deviation);
47 }
48 
49 
51  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) {
65  const std::string distName = description.substr(0, description.find('('));
66  if (distName == "norm" || distName == "normc") {
67  std::vector<std::string> params = StringTokenizer(description.substr(distName.size() + 1, description.size() - distName.size() - 2), ',').getVector();
68  myParameter.resize(params.size());
69  std::transform(params.begin(), params.end(), myParameter.begin(), TplConvert::_str2double);
70  setID(distName);
71  } else {
72  myParameter[0] = TplConvert::_str2double(description);
73  }
74  assert(!myParameter.empty());
75  if (myParameter.size() == 1) {
76  myParameter.push_back(0.);
77  }
78 }
79 
80 
81 double
82 Distribution_Parameterized::sample(std::mt19937* which) const {
83  if (myParameter[1] == 0.) {
84  return myParameter[0];
85  }
86  double val = RandHelper::randNorm(myParameter[0], myParameter[1], which);
87  if (myParameter.size() > 2) {
88  const double min = myParameter[2];
89  const double max = getMax();
90  while (val < min || val > max) {
91  val = RandHelper::randNorm(myParameter[0], myParameter[1], which);
92  }
93  }
94  return val;
95 }
96 
97 
98 double
100  if (myParameter[1] == 0.) {
101  return myParameter[0];
102  }
103  return myParameter.size() > 3 ? myParameter[3] : std::numeric_limits<double>::infinity();
104 }
105 
106 
107 std::string
108 Distribution_Parameterized::toStr(std::streamsize accuracy) const {
109  return myParameter[1] == 0. ? toString(myParameter[0]) : myID + "(" + joinToString(myParameter, ",", accuracy) + ")";
110 }
111 
112 
113 /****************************************************************************/
114 
std::vector< double > myParameter
The distribution&#39;s parameters.
double getMax() const
Returns the maximum value of this distribution.
void parse(const std::string &description)
Overwrite by parsable distribution description.
virtual ~Distribution_Parameterized()
Destructor.
static double _str2double(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter ...
Definition: TplConvert.h:363
static double randNorm(double mean, double variance, std::mt19937 *rng=0)
Access to a random number from a normal distribution.
Definition: RandHelper.h:136
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:55
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()
std::string myID
The name of the object.
Definition: Named.h:135
void setID(const std::string &newID)
resets the id
Definition: Named.h:73
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:236