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-2016 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:
63  myProb(0) {
64  }
65 
68 
80  bool add(SUMOReal prob, T val, bool checkDuplicates = true) {
81  assert(prob >= 0);
82  myProb += prob;
83  if (checkDuplicates) {
84  for (int i = 0; i < (int)myVals.size(); i++) {
85  if (val == myVals[i]) {
86  myProbs[i] += prob;
87  return false;
88  }
89  }
90  }
91  myVals.push_back(val);
92  myProbs.push_back(prob);
93  return true;
94  }
95 
103  T get(MTRand* which = 0) const {
104  if (myProb == 0) {
105  throw OutOfBoundsException();
106  }
107  SUMOReal prob = which == 0 ? RandHelper::rand(myProb) : which->rand(myProb);
108  for (int i = 0; i < (int)myVals.size(); i++) {
109  if (prob < myProbs[i]) {
110  return myVals[i];
111  }
112  prob -= myProbs[i];
113  }
114  return myVals.back();
115  }
116 
124  return myProb;
125  }
126 
128  void clear() {
129  myProb = 0;
130  myVals.clear();
131  myProbs.clear();
132  }
133 
141  const std::vector<T>& getVals() const {
142  return myVals;
143  }
144 
152  const std::vector<SUMOReal>& getProbs() const {
153  return myProbs;
154  }
155 
156 private:
160  std::vector<T> myVals;
162  std::vector<SUMOReal> myProbs;
163 
164 };
165 
166 
167 #endif
168 
169 /****************************************************************************/
const std::vector< SUMOReal > & getProbs() const
Returns the probabilities assigned to the members of the distribution.
Represents a generic random distribution.
RandomDistributor()
Constructor for an empty distribution.
bool add(SUMOReal prob, T val, bool checkDuplicates=true)
Adds a value with an assigned probability to the distribution.
std::vector< T > myVals
the members (acts as a ring buffer if myMaximumSize is reached)
static SUMOReal rand()
Returns a random real number in [0, 1)
Definition: RandHelper.h:62
const std::vector< T > & getVals() const
Returns the members of the distribution.
SUMOReal myProb
the total probability
~RandomDistributor()
Destructor.
std::vector< SUMOReal > myProbs
the corresponding probabilities (acts as a ring buffer if myMaximumSize is reached) ...
void clear()
Clears the distribution.
SUMOReal getOverallProb() const
Return the sum of the probabilites assigned to the members.
#define SUMOReal
Definition: config.h:213