SUMO - Simulation of Urban MObility
RandomDistributor.h
Go to the documentation of this file.
1 /****************************************************************************/
9 // Represents a generic random distribution
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2005-2017 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 #ifndef RandomDistributor_h
23 #define RandomDistributor_h
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <cassert>
36 #include <limits>
39 
40 
41 // ===========================================================================
42 // class definitions
43 // ===========================================================================
55 template<class T>
57 public:
61  myProb(0) {
62  }
63 
66 
78  bool add(T val, double prob, bool checkDuplicates = true) {
79  assert(prob >= 0);
80  myProb += prob;
81  if (checkDuplicates) {
82  for (int i = 0; i < (int)myVals.size(); i++) {
83  if (val == myVals[i]) {
84  myProbs[i] += prob;
85  return false;
86  }
87  }
88  }
89  myVals.push_back(val);
90  myProbs.push_back(prob);
91  return true;
92  }
93 
101  T get(MTRand* which = 0) const {
102  if (myProb == 0) {
103  throw OutOfBoundsException();
104  }
105  double prob = which == 0 ? RandHelper::rand(myProb) : which->rand(myProb);
106  for (int i = 0; i < (int)myVals.size(); i++) {
107  if (prob < myProbs[i]) {
108  return myVals[i];
109  }
110  prob -= myProbs[i];
111  }
112  return myVals.back();
113  }
114 
121  double getOverallProb() const {
122  return myProb;
123  }
124 
126  void clear() {
127  myProb = 0;
128  myVals.clear();
129  myProbs.clear();
130  }
131 
139  const std::vector<T>& getVals() const {
140  return myVals;
141  }
142 
150  const std::vector<double>& getProbs() const {
151  return myProbs;
152  }
153 
154 private:
156  double myProb;
158  std::vector<T> myVals;
160  std::vector<double> myProbs;
161 
162 };
163 
164 
165 #endif
166 
167 /****************************************************************************/
std::vector< double > myProbs
the corresponding probabilities
Represents a generic random distribution.
RandomDistributor()
Constructor for an empty distribution.
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.
static double rand()
Returns a random real number in [0, 1)
Definition: RandHelper.h:62
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.