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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // Represents a generic random distribution
20 /****************************************************************************/
21 #ifndef RandomDistributor_h
22 #define RandomDistributor_h
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <cassert>
35 #include <limits>
38 
39 
40 // ===========================================================================
41 // class definitions
42 // ===========================================================================
54 template<class T>
56 public:
60  myProb(0) {
61  }
62 
65 
77  bool add(T val, double prob, bool checkDuplicates = true) {
78  assert(prob >= 0);
79  myProb += prob;
80  if (checkDuplicates) {
81  for (int i = 0; i < (int)myVals.size(); i++) {
82  if (val == myVals[i]) {
83  myProbs[i] += prob;
84  return false;
85  }
86  }
87  }
88  myVals.push_back(val);
89  myProbs.push_back(prob);
90  return true;
91  }
92 
100  T get(std::mt19937* which = 0) const {
101  if (myProb == 0) {
102  throw OutOfBoundsException();
103  }
104  double prob = RandHelper::rand(myProb, which);
105  for (int i = 0; i < (int)myVals.size(); i++) {
106  if (prob < myProbs[i]) {
107  return myVals[i];
108  }
109  prob -= myProbs[i];
110  }
111  return myVals.back();
112  }
113 
120  double getOverallProb() const {
121  return myProb;
122  }
123 
125  void clear() {
126  myProb = 0;
127  myVals.clear();
128  myProbs.clear();
129  }
130 
138  const std::vector<T>& getVals() const {
139  return myVals;
140  }
141 
149  const std::vector<double>& getProbs() const {
150  return myProbs;
151  }
152 
153 private:
155  double myProb;
157  std::vector<T> myVals;
159  std::vector<double> myProbs;
160 
161 };
162 
163 
164 #endif
165 
166 /****************************************************************************/
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:64
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.