Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
RandomDistributor.h
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2005-2024 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
20// Represents a generic random distribution
21/****************************************************************************/
22#pragma once
23#include <config.h>
24
25#include <cassert>
26#include <limits>
29
30
31// ===========================================================================
32// class definitions
33// ===========================================================================
45template<class T>
47public:
51 myProb(0) {
52 }
53
56
68 bool add(T val, double prob, bool checkDuplicates = true) {
69 myProb += prob;
70 assert(myProb >= 0);
71 if (checkDuplicates) {
72 for (int i = 0; i < (int)myVals.size(); i++) {
73 if (val == myVals[i]) {
74 myProbs[i] += prob;
75 assert(myProbs[i] >= 0);
76 return false;
77 }
78 }
79 } else {
80 assert(prob >= 0);
81 }
82 myVals.push_back(val);
83 myProbs.push_back(prob);
84 return true;
85 }
86
92 bool remove(T val) {
93 for (int i = 0; i < (int)myVals.size(); i++) {
94 if (myVals[i] == val) {
95 myProb -= myProbs[i];
96 myProbs.erase(myProbs.begin() + i);
97 myVals.erase(myVals.begin() + i);
98 return true;
99 }
100 }
101 return false;
102 }
103
111 T get(SumoRNG* which = nullptr) const {
112 if (myProb == 0) {
113 throw OutOfBoundsException();
114 }
115 double prob = RandHelper::rand(myProb, which);
116 for (int i = 0; i < (int)myVals.size(); i++) {
117 if (prob < myProbs[i]) {
118 return myVals[i];
119 }
120 prob -= myProbs[i];
121 }
122 return myVals.back();
123 }
124
131 double getOverallProb() const {
132 return myProb;
133 }
134
136 void clear() {
137 myProb = 0;
138 myVals.clear();
139 myProbs.clear();
140 }
141
149 const std::vector<T>& getVals() const {
150 return myVals;
151 }
152
160 const std::vector<double>& getProbs() const {
161 return myProbs;
162 }
163
164private:
166 double myProb;
168 std::vector<T> myVals;
170 std::vector<double> myProbs;
171
172};
static double rand(SumoRNG *rng=nullptr)
Returns a random real number in [0, 1)
Represents a generic random distribution.
double getOverallProb() const
Return the sum of the probabilites assigned to the members.
~RandomDistributor()
Destructor.
double myProb
the total probability
std::vector< double > myProbs
the corresponding probabilities
T get(SumoRNG *which=nullptr) const
Draw a sample of the distribution.
bool add(T val, double prob, bool checkDuplicates=true)
Adds a value with an assigned probability to the distribution.
const std::vector< T > & getVals() const
Returns the members of the distribution.
bool remove(T val)
Removes a value with an assigned probability from the distribution.
void clear()
Clears the distribution.
const std::vector< double > & getProbs() const
Returns the probabilities assigned to the members of the distribution.
std::vector< T > myVals
the members
RandomDistributor()
Constructor for an empty distribution.