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-2018 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 
94  T get(std::mt19937* which = 0) const {
95  if (myProb == 0) {
96  throw OutOfBoundsException();
97  }
98  double prob = RandHelper::rand(myProb, which);
99  for (int i = 0; i < (int)myVals.size(); i++) {
100  if (prob < myProbs[i]) {
101  return myVals[i];
102  }
103  prob -= myProbs[i];
104  }
105  return myVals.back();
106  }
107 
114  double getOverallProb() const {
115  return myProb;
116  }
117 
119  void clear() {
120  myProb = 0;
121  myVals.clear();
122  myProbs.clear();
123  }
124 
132  const std::vector<T>& getVals() const {
133  return myVals;
134  }
135 
143  const std::vector<double>& getProbs() const {
144  return myProbs;
145  }
146 
147 private:
149  double myProb;
151  std::vector<T> myVals;
153  std::vector<double> myProbs;
154 
155 };
156 
157 
158 #endif
159 
160 /****************************************************************************/
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:61
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.