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 
33 #include <utils/common/ToString.h>
36 
37 
38 // ===========================================================================
39 // method definitions
40 // ===========================================================================
42  : Distribution("") {
43  const std::string distName = description.substr(0, description.find('('));
44  if (distName == "norm" || distName == "normc") {
45  std::vector<std::string> params = StringTokenizer(description.substr(distName.size() + 1, description.size() - distName.size() - 2), ',').getVector();
46  myParameter.resize(params.size());
47  std::transform(params.begin(), params.end(), myParameter.begin(), TplConvert::_str2double);
48  setID(distName);
49  } else {
50  myParameter.push_back(TplConvert::_str2double(description));
51  }
52  assert(!myParameter.empty());
53  if (myParameter.size() == 1) {
54  myParameter.push_back(0.);
55  }
56 }
57 
58 
60  double mean, double deviation)
61  : Distribution(id) {
62  myParameter.push_back(mean);
63  myParameter.push_back(deviation);
64 }
65 
66 
68  double mean, double deviation, double min, double max)
69  : Distribution(id) {
70  myParameter.push_back(mean);
71  myParameter.push_back(deviation);
72  myParameter.push_back(min);
73  myParameter.push_back(max);
74 }
75 
76 
78 
79 
80 double
82  if (myParameter[1] == 0.) {
83  return myParameter[0];
84  }
85  double val = which == 0 ? RandHelper::randNorm(myParameter[0], myParameter[1]) : which->randNorm(myParameter[0], myParameter[1]);
86  if (myParameter.size() > 2) {
87  const double min = myParameter[2];
88  const double max = getMax();
89  while (val < min || val > max) {
90  val = which == 0 ? RandHelper::randNorm(myParameter[0], myParameter[1]) : which->randNorm(myParameter[0], myParameter[1]);
91  }
92  }
93  return val;
94 }
95 
96 
97 double
99  if (myParameter[1] == 0.) {
100  return myParameter[0];
101  }
102  return myParameter.size() > 3 ? myParameter[3] : std::numeric_limits<double>::infinity();
103 }
104 
105 
106 std::string
107 Distribution_Parameterized::toStr(std::streamsize accuracy) const {
108  return myParameter[1] == 0. ? toString(myParameter[0]) : myID + "(" + joinToString(myParameter, ",", accuracy) + ")";
109 }
110 
111 
112 /****************************************************************************/
113 
std::vector< double > myParameter
The distribution&#39;s parameters.
Distribution_Parameterized(const std::string &description)
Constructor for parsable distribution description.
#define min(a, b)
Definition: polyfonts.c:66
double getMax() const
Returns the maximum value of this distribution.
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:348
#define max(a, b)
Definition: polyfonts.c:65
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:56
std::string toStr(std::streamsize accuracy) const
Returns the string representation of this 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
double randNorm(const double &mean=0.0, const double &variance=0.0)
double sample(MTRand *which=0) const
Draw a sample of the distribution.
static double randNorm(double mean, double variance, MTRand *rng=0)
Access to a random number from a normal distribution.
Definition: RandHelper.h:97
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=gPrecision)
Definition: ToString.h:228