SUMO - Simulation of Urban MObility
Distribution_Parameterized.cpp
Go to the documentation of this file.
1 /****************************************************************************/
8 // A distribution described by parameters such as the mean value and std-dev
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
11 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <cassert>
35 #include <utils/common/ToString.h>
38 
39 
40 // ===========================================================================
41 // method definitions
42 // ===========================================================================
44  double mean, double deviation)
45  : Distribution(id) {
46  myParameter.push_back(mean);
47  myParameter.push_back(deviation);
48 }
49 
50 
52  double mean, double deviation, double min, double max)
53  : Distribution(id) {
54  myParameter.push_back(mean);
55  myParameter.push_back(deviation);
56  myParameter.push_back(min);
57  myParameter.push_back(max);
58 }
59 
60 
62 
63 
64 void
65 Distribution_Parameterized::parse(const std::string& description) {
66  const std::string distName = description.substr(0, description.find('('));
67  if (distName == "norm" || distName == "normc") {
68  std::vector<std::string> params = StringTokenizer(description.substr(distName.size() + 1, description.size() - distName.size() - 2), ',').getVector();
69  myParameter.resize(params.size());
70  std::transform(params.begin(), params.end(), myParameter.begin(), TplConvert::_str2double);
71  setID(distName);
72  } else {
73  myParameter[0] = TplConvert::_str2double(description);
74  }
75  assert(!myParameter.empty());
76  if (myParameter.size() == 1) {
77  myParameter.push_back(0.);
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::string
109 Distribution_Parameterized::toStr(std::streamsize accuracy) const {
110  return myParameter[1] == 0. ? toString(myParameter[0]) : myID + "(" + joinToString(myParameter, ",", accuracy) + ")";
111 }
112 
113 
114 /****************************************************************************/
115 
std::vector< double > myParameter
The distribution&#39;s parameters.
#define min(a, b)
Definition: polyfonts.c:66
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:364
static double randNorm(double mean, double variance, std::mt19937 *rng=0)
Access to a random number from a normal distribution.
Definition: RandHelper.h:137
#define max(a, b)
Definition: polyfonts.c:65
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:56
double sample(std::mt19937 *which=0) const
Draw a sample of the 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:136
void setID(const std::string &newID)
resets the id
Definition: Named.h:74
std::string toStr(std::streamsize accuracy) const
Returns the string representation of this distribution.
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=gPrecision)
Definition: ToString.h:237