SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RandomDistributor.h
Go to the documentation of this file.
1 /****************************************************************************/
8 // Represents a generic random distribution
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
11 // Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
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:
62  myProb(0)
63  {}
64 
67 
79  bool add(SUMOReal prob, T val, bool checkDuplicates = true) {
80  assert(prob >= 0);
81  myProb += prob;
82  if (checkDuplicates) {
83  for (size_t i = 0; i < myVals.size(); i++) {
84  if (val == myVals[i]) {
85  myProbs[i] += prob;
86  return false;
87  }
88  }
89  }
90  myVals.push_back(val);
91  myProbs.push_back(prob);
92  return true;
93  }
94 
102  T get(MTRand* which = 0) const {
103  if (myProb == 0) {
104  throw OutOfBoundsException();
105  }
106  SUMOReal prob = which == 0 ? RandHelper::rand(myProb) : which->rand(myProb);
107  for (size_t i = 0; i < myVals.size(); i++) {
108  if (prob < myProbs[i]) {
109  return myVals[i];
110  }
111  prob -= myProbs[i];
112  }
113  return myVals.back();
114  }
115 
123  return myProb;
124  }
125 
127  void clear() {
128  myProb = 0;
129  myVals.clear();
130  myProbs.clear();
131  }
132 
140  const std::vector<T>& getVals() const {
141  return myVals;
142  }
143 
151  const std::vector<SUMOReal>& getProbs() const {
152  return myProbs;
153  }
154 
155 private:
159  std::vector<T> myVals;
161  std::vector<SUMOReal> myProbs;
162 
163 };
164 
165 
166 #endif
167 
168 /****************************************************************************/
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:61
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:215