Doxygen 1.9.8
Toolkit for Adaptive Stochastic Modeling and Non-Intrusive ApproximatioN: Tasmanian v8.2
 
Loading...
Searching...
No Matches
tsgDreamCoreRandom.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017, Miroslav Stoyanov
3 *
4 * This file is part of
5 * Toolkit for Adaptive Stochastic Modeling And Non-Intrusive ApproximatioN: TASMANIAN
6 *
7 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
12 * and the following disclaimer in the documentation and/or other materials provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
20 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
21 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * UT-BATTELLE, LLC AND THE UNITED STATES GOVERNMENT MAKE NO REPRESENTATIONS AND DISCLAIM ALL WARRANTIES, BOTH EXPRESSED AND IMPLIED.
25 * THERE ARE NO EXPRESS OR IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, OR THAT THE USE OF THE SOFTWARE WILL NOT INFRINGE ANY PATENT,
26 * COPYRIGHT, TRADEMARK, OR OTHER PROPRIETARY RIGHTS, OR THAT THE SOFTWARE WILL ACCOMPLISH THE INTENDED RESULTS OR THAT THE SOFTWARE OR ITS USE WILL NOT RESULT IN INJURY OR DAMAGE.
27 * THE USER ASSUMES RESPONSIBILITY FOR ALL LIABILITIES, PENALTIES, FINES, CLAIMS, CAUSES OF ACTION, AND COSTS AND EXPENSES, CAUSED BY, RESULTING FROM OR ARISING OUT OF,
28 * IN WHOLE OR IN PART THE USE, STORAGE OR DISPOSAL OF THE SOFTWARE.
29 */
30
31#ifndef __TASMANIAN_DREAM_CORE_RANDOM_HPP
32#define __TASMANIAN_DREAM_CORE_RANDOM_HPP
33
54namespace TasDREAM{
55
59
61inline double tsgCoreUniform01(){ return ((double) rand()) / ((double) RAND_MAX); }
62
65
67inline void applyUniformUpdate(std::vector<double> &x, double magnitude, std::function<double(void)> get_random01 = tsgCoreUniform01){
68 if (magnitude == 0.0) return;
69 for(auto &v : x) v += magnitude * (2.0 * get_random01() -1.0);
70}
71
74
77inline void applyGaussianUpdate(std::vector<double> &x, double magnitude, std::function<double(void)> get_random01 = tsgCoreUniform01){
78 if (magnitude == 0.0) return;
79 bool tictoc = false;
80 double g = 0.0;
81 for(auto &v : x){
82 tictoc = !tictoc;
83 if (tictoc){
84 double r = magnitude * std::sqrt(-2.0 * std::log(get_random01())), t = 2.0 * DreamMaths::pi * get_random01(); // radius and angle
85 v += r * std::cos(t);
86 g = r * std::sin(t);
87 }else{
88 v += g;
89 }
90 }
91}
92
95
99inline void genUniformSamples(const std::vector<double> &lower, const std::vector<double> &upper, int num_samples, std::vector<double> &x, std::function<double(void)> get_random01 = tsgCoreUniform01){
100 if (lower.size() != upper.size()) throw std::runtime_error("ERROR: genUniformSamples() requires lower and upper vectors with matching size.");
101 if (x.size() != lower.size() * num_samples) x.resize(lower.size() * num_samples);
102 for(auto &v : x) v = get_random01();
103
104 std::vector<double> length(lower.size());
105 std::transform(lower.begin(), lower.end(), upper.begin(), length.begin(), [&](double l, double u)->double{ return (u - l); });
106
107 auto ix = x.begin();
108 while(ix != x.end()){
109 auto ilow = lower.begin();
110 for(auto l : length){
111 *ix *= l;
112 *ix++ += *ilow++;
113 }
114 }
115}
116
121inline std::vector<double> genUniformSamples(const std::vector<double> &lower, const std::vector<double> &upper,
122 int num_samples, std::function<double(void)> get_random01 = tsgCoreUniform01){
123 std::vector<double> x;
124 genUniformSamples(lower, upper, num_samples, x, get_random01);
125 return x;
126}
127
130
134inline void genGaussianSamples(const std::vector<double> &means, const std::vector<double> &deviations,
135 int num_samples, std::vector<double> &x, std::function<double(void)> get_random01 = tsgCoreUniform01){
136 if (means.size() != deviations.size()) throw std::runtime_error("ERROR: genGaussianSamples() means and deviations vectors must have the same size.");
137 if (x.size() != means.size() * num_samples) x.resize(means.size() * num_samples);
138
139 std::fill_n(x.data(), x.size(), 0.0);
140 applyGaussianUpdate(x, 1.0, get_random01);
141
142 auto ix = x.begin();
143 while(ix != x.end()){
144 auto im = means.begin();
145 for(auto s : deviations){
146 *ix *= s;
147 *ix++ += *im++;
148 }
149 }
150}
151
156inline std::vector<double> genGaussianSamples(const std::vector<double> &means, const std::vector<double> &deviations,
157 int num_samples, std::function<double(void)> get_random01 = tsgCoreUniform01){
158 std::vector<double> x;
159 genGaussianSamples(means, deviations, num_samples, x, get_random01);
160 return x;
161}
162
163}
164
165#endif
constexpr double pi
Dream copy of TasGrid::Maths::pi.
Definition tsgDreamEnumerates.hpp:184
void genUniformSamples(const std::vector< double > &lower, const std::vector< double > &upper, int num_samples, std::vector< double > &x, std::function< double(void)> get_random01=tsgCoreUniform01)
Generate uniform random samples in the hypercube defined by lower and upper limits.
Definition tsgDreamCoreRandom.hpp:99
void applyGaussianUpdate(std::vector< double > &x, double magnitude, std::function< double(void)> get_random01=tsgCoreUniform01)
Add a correction to every entry in x, sue Gaussian distribution with zero mean and standard deviation...
Definition tsgDreamCoreRandom.hpp:77
double tsgCoreUniform01()
Default random sampler, using rand() divided by RAND_MAX.
Definition tsgDreamCoreRandom.hpp:61
void genGaussianSamples(const std::vector< double > &means, const std::vector< double > &deviations, int num_samples, std::vector< double > &x, std::function< double(void)> get_random01=tsgCoreUniform01)
Generate standard normal samples with given means and standard deviations.
Definition tsgDreamCoreRandom.hpp:134
void applyUniformUpdate(std::vector< double > &x, double magnitude, std::function< double(void)> get_random01=tsgCoreUniform01)
Add a correction to every entry in x, use uniform samples over (-magnitude, magnitude).
Definition tsgDreamCoreRandom.hpp:67
Encapsulates the Tasmanian DREAM module.
Definition TasmanianDREAM.hpp:80