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 /****************************************************************************/
17 // Represents a generic random distribution
18 /****************************************************************************/
19 #ifndef RandomDistributor_h
20 #define RandomDistributor_h
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <config.h>
27 
28 #include <cassert>
29 #include <limits>
32 
33 
34 // ===========================================================================
35 // class definitions
36 // ===========================================================================
48 template<class T>
50 public:
54  myProb(0) {
55  }
56 
59 
71  bool add(T val, double prob, bool checkDuplicates = true) {
72  assert(prob >= 0);
73  myProb += prob;
74  if (checkDuplicates) {
75  for (int i = 0; i < (int)myVals.size(); i++) {
76  if (val == myVals[i]) {
77  myProbs[i] += prob;
78  return false;
79  }
80  }
81  }
82  myVals.push_back(val);
83  myProbs.push_back(prob);
84  return true;
85  }
86 
92  bool remove(T val) {
93  for (int i = 0; i < (int)myVals.size(); i++) {
94  if (myVals[i] == val) {
95  myProb -= myProbs[i];
96  myProbs.erase(myProbs.begin() + i);
97  myVals.erase(myVals.begin() + i);
98  return true;
99  }
100  }
101  return false;
102  }
103 
111  T get(std::mt19937* which = 0) const {
112  if (myProb == 0) {
113  throw OutOfBoundsException();
114  }
115  double prob = RandHelper::rand(myProb, which);
116  for (int i = 0; i < (int)myVals.size(); i++) {
117  if (prob < myProbs[i]) {
118  return myVals[i];
119  }
120  prob -= myProbs[i];
121  }
122  return myVals.back();
123  }
124 
131  double getOverallProb() const {
132  return myProb;
133  }
134 
136  void clear() {
137  myProb = 0;
138  myVals.clear();
139  myProbs.clear();
140  }
141 
149  const std::vector<T>& getVals() const {
150  return myVals;
151  }
152 
160  const std::vector<double>& getProbs() const {
161  return myProbs;
162  }
163 
164 private:
166  double myProb;
168  std::vector<T> myVals;
170  std::vector<double> myProbs;
171 
172 };
173 
174 
175 #endif
176 
177 /****************************************************************************/
std::vector< double > myProbs
the corresponding probabilities
Represents a generic random distribution.
RandomDistributor()
Constructor for an empty distribution.
static double rand(std::mt19937 *rng=0)
Returns a random real number in [0, 1)
Definition: RandHelper.h:60
std::vector< T > myVals
the members
const std::vector< T > & getVals() const
Returns the members of the distribution.
const std::vector< double > & getProbs() const
Returns the probabilities assigned to the members of the distribution.
~RandomDistributor()
Destructor.
double myProb
the total probability
void clear()
Clears the distribution.
double getOverallProb() const
Return the sum of the probabilites assigned to the members.
bool add(T val, double prob, bool checkDuplicates=true)
Adds a value with an assigned probability to the distribution.