Eclipse SUMO - Simulation of Urban MObility
RandomDistributor.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2005-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 // Represents a generic random distribution
17 /****************************************************************************/
18 #ifndef RandomDistributor_h
19 #define RandomDistributor_h
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include <cassert>
28 #include <limits>
31 
32 
33 // ===========================================================================
34 // class definitions
35 // ===========================================================================
47 template<class T>
49 public:
53  myProb(0) {
54  }
55 
58 
70  bool add(T val, double prob, bool checkDuplicates = true) {
71  myProb += prob;
72  assert(myProb >= 0);
73  if (checkDuplicates) {
74  for (int i = 0; i < (int)myVals.size(); i++) {
75  if (val == myVals[i]) {
76  myProbs[i] += prob;
77  assert(myProbs[i] >= 0);
78  return false;
79  }
80  }
81  } else {
82  assert(prob >= 0);
83  }
84  myVals.push_back(val);
85  myProbs.push_back(prob);
86  return true;
87  }
88 
94  bool remove(T val) {
95  for (int i = 0; i < (int)myVals.size(); i++) {
96  if (myVals[i] == val) {
97  myProb -= myProbs[i];
98  myProbs.erase(myProbs.begin() + i);
99  myVals.erase(myVals.begin() + i);
100  return true;
101  }
102  }
103  return false;
104  }
105 
113  T get(std::mt19937* which = 0) const {
114  if (myProb == 0) {
115  throw OutOfBoundsException();
116  }
117  double prob = RandHelper::rand(myProb, which);
118  for (int i = 0; i < (int)myVals.size(); i++) {
119  if (prob < myProbs[i]) {
120  return myVals[i];
121  }
122  prob -= myProbs[i];
123  }
124  return myVals.back();
125  }
126 
133  double getOverallProb() const {
134  return myProb;
135  }
136 
138  void clear() {
139  myProb = 0;
140  myVals.clear();
141  myProbs.clear();
142  }
143 
151  const std::vector<T>& getVals() const {
152  return myVals;
153  }
154 
162  const std::vector<double>& getProbs() const {
163  return myProbs;
164  }
165 
166 private:
168  double myProb;
170  std::vector<T> myVals;
172  std::vector<double> myProbs;
173 
174 };
175 
176 
177 #endif
178 
179 /****************************************************************************/
RandomDistributor::add
bool add(T val, double prob, bool checkDuplicates=true)
Adds a value with an assigned probability to the distribution.
Definition: RandomDistributor.h:70
RandomDistributor::RandomDistributor
RandomDistributor()
Constructor for an empty distribution.
Definition: RandomDistributor.h:52
RandomDistributor::myVals
std::vector< T > myVals
the members
Definition: RandomDistributor.h:170
RandomDistributor::get
T get(std::mt19937 *which=0) const
Draw a sample of the distribution.
Definition: RandomDistributor.h:113
RandHelper::rand
static double rand(std::mt19937 *rng=0)
Returns a random real number in [0, 1)
Definition: RandHelper.h:53
RandomDistributor::~RandomDistributor
~RandomDistributor()
Destructor.
Definition: RandomDistributor.h:57
RandomDistributor::remove
bool remove(T val)
Removes a value with an assigned probability from the distribution.
Definition: RandomDistributor.h:94
OutOfBoundsException
Definition: UtilExceptions.h:134
UtilExceptions.h
RandomDistributor::clear
void clear()
Clears the distribution.
Definition: RandomDistributor.h:138
RandomDistributor::getProbs
const std::vector< double > & getProbs() const
Returns the probabilities assigned to the members of the distribution.
Definition: RandomDistributor.h:162
RandomDistributor
Represents a generic random distribution.
Definition: RandomDistributor.h:48
RandomDistributor::myProb
double myProb
the total probability
Definition: RandomDistributor.h:168
RandomDistributor::myProbs
std::vector< double > myProbs
the corresponding probabilities
Definition: RandomDistributor.h:172
config.h
RandHelper.h
RandomDistributor::getOverallProb
double getOverallProb() const
Return the sum of the probabilites assigned to the members.
Definition: RandomDistributor.h:133
RandomDistributor::getVals
const std::vector< T > & getVals() const
Returns the members of the distribution.
Definition: RandomDistributor.h:151