Doxygen 1.17.0
Toolkit for Adaptive Stochastic Modeling and Non-Intrusive ApproximatioN: Tasmanian v8.3
Loading...
Searching...
No Matches
tsgDreamState.hpp
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_STATE_HPP
32#define __TASMANIAN_DREAM_STATE_HPP
33
34#include "tsgDreamEnumerates.hpp"
35
43
51
52namespace TasDREAM{
53
58enum class NumChains : int {};
59
65
68
83public:
87 TasmanianDREAM(int cnum_chains, int cnum_dimensions);
90 : TasmanianDREAM(static_cast<int>(chains), static_cast<int>(dims)) {}
91
92 TasmanianDREAM(int cnum_chains, const TasGrid::TasmanianSparseGrid &grid);
95
97 static const char* getVersion(){ return TASMANIAN_VERSION_STRING; }
99 static const char* getLicense(){ return TASMANIAN_LICENSE; }
101 static int getVersionMajor(){ return TASMANIAN_VERSION_MAJOR; }
103 static int getVersionMinor(){ return TASMANIAN_VERSION_MINOR; }
104
106 int getNumDimensions() const{ return (int) num_dimensions; }
108 int getNumChains() const{ return (int) num_chains; }
110 size_t getNumHistory() const{ return pdf_history.size(); }
111
113 bool isStateReady() const{ return init_state; }
115 bool isPDFReady() const{ return init_values; }
116
118
122 void setState(const std::vector<double> &new_state);
123
125
130 void setState(std::function<void(double *)> update_state);
131
133
138 void setPDFvalues(const std::vector<double> &new_values);
139
141
146 void setPDFvalues(std::function<void(const std::vector<double> &state, std::vector<double> &values)> probability_distribution);
147
150
152
157 void getIJKdelta(size_t i, size_t j, size_t k, double w, std::vector<double> &x) const;
158
160
162 void getChainState(size_t i, double *x) const{ std::copy_n(state.begin() + i * num_dimensions, num_dimensions, x); }
163
165 const std::vector<double>& getChainState() const{ return state; }
166
168
170 double getPDFvalue(size_t i) const{ return pdf_values[i]; }
171
173
175 void expandHistory(int num_snapshots);
176
178
180 void saveStateHistory(size_t num_accepted);
181
183 const std::vector<double>& getHistory() const{ return history; }
184
186 const std::vector<double>& getHistoryPDF() const{ return pdf_history; }
187
189 void getHistoryMeanVariance(std::vector<double> &mean, std::vector<double> &var) const;
190
192 void getApproximateMode(std::vector<double> &mode) const;
193
195 std::vector<double> getApproximateMode() const{
196 std::vector<double> mode;
197 getApproximateMode(mode);
198 return mode;
199 }
200
203
205 double getAcceptanceRate() const{ return ((pdf_history.empty()) ? 0 : ((double) accepted) / ((double) pdf_history.size())); }
206
207 // file I/O
208private:
209 size_t num_chains, num_dimensions;
210 bool init_state, init_values;
211
212 size_t accepted;
213
214 std::vector<double> state, history;
215 std::vector<double> pdf_values, pdf_history;
216};
217
218}
219
220#endif
~TasmanianDREAM()
Default destructor, release all used memory.
double getPDFvalue(size_t i) const
Return the value of the probability_distribution of the i-th chain.
Definition tsgDreamState.hpp:170
void setState(std::function< void(double *)> update_state)
Set the current DREAM state by calling update_state on each vector.
static int getVersionMinor()
Return the minor version of the library.
Definition tsgDreamState.hpp:103
static int getVersionMajor()
Return the major version of the library.
Definition tsgDreamState.hpp:101
void saveStateHistory(size_t num_accepted)
Appends the current state to the history.
const std::vector< double > & getChainState() const
Return a const reference to the internal state vector.
Definition tsgDreamState.hpp:165
std::vector< double > getApproximateMode() const
Overload that returns the vector.
Definition tsgDreamState.hpp:195
int getNumDimensions() const
Return the number of dimensions.
Definition tsgDreamState.hpp:106
void getHistoryMeanVariance(std::vector< double > &mean, std::vector< double > &var) const
Compute the means and variance of the saved history.
void setPDFvalues(const std::vector< double > &new_values)
Set the current set of pdf values to the new_values.
const std::vector< double > & getHistory() const
Return a const reference to the internal state vector.
Definition tsgDreamState.hpp:183
bool isStateReady() const
Return true if the state has already been initialized with setState().
Definition tsgDreamState.hpp:113
void getApproximateMode(std::vector< double > &mode) const
Return the sample with highest probability, searchers within the history.
TasmanianDREAM()
Constructor for a null DREAM state, no chains and no dimensions (used for MPI purposes).
void getChainState(size_t i, double *x) const
Return the state of the i-th chain, store an array x of size at least num_dimensions.
Definition tsgDreamState.hpp:162
TasmanianDREAM(int cnum_chains, int cnum_dimensions)
Constructor for a DREAM state with the number of chains and dimensions.
static const char * getLicense()
Return human readable sting with the license, refer to the LICENSE file for details.
Definition tsgDreamState.hpp:99
const std::vector< double > & getHistoryPDF() const
Return a const reference to the internal state vector.
Definition tsgDreamState.hpp:186
void clearPDFvalues()
Erase the currently stored pdf values, useful when switching between log and non-log form sampling.
static const char * getVersion()
Return human readable string with the version.
Definition tsgDreamState.hpp:97
TasmanianDREAM(NumChains chains, NumDimensions dims)
Constructor for a DREAM state with the number of chains and dimensions.
Definition tsgDreamState.hpp:89
void expandHistory(int num_snapshots)
Allocate (expand) internal storage for the history snapshots, avoids reallocating data when saving a ...
double getAcceptanceRate() const
Returns the acceptance rate of the current history.
Definition tsgDreamState.hpp:205
size_t getNumHistory() const
Return the number of saved vectors in the history.
Definition tsgDreamState.hpp:110
void getIJKdelta(size_t i, size_t j, size_t k, double w, std::vector< double > &x) const
Used by the DREAM sampler, , where s indicates the current i, j, and k-th state vectors.
int getNumChains() const
Return the number of chains.
Definition tsgDreamState.hpp:108
void setPDFvalues(std::function< void(const std::vector< double > &state, std::vector< double > &values)> probability_distribution)
Set the current set of pdf values using the given probability_distribution.
bool isPDFReady() const
Return true if the pdf values have already been initialized with setPDF().
Definition tsgDreamState.hpp:115
TasmanianDREAM(int cnum_chains, const TasGrid::TasmanianSparseGrid &grid)
Constructor for a DREAM state with the number of chains and the dimension of the sparse grid.
void setState(const std::vector< double > &new_state)
Set the current DREAM state to the new_state.
void clearHistory()
Clear the stored history (does not touch the state).
The master-class that represents an instance of a Tasmanian sparse grid.
Definition TasmanianSparseGrid.hpp:293
NumChains
Strong type for number of chains in TasDREAM::TasmanianDREAM.
Definition tsgDreamState.hpp:58
NumDimensions
Strong type for the number of dimensions.
Definition TasmanianSparseGrid.hpp:2450
Encapsulates the Tasmanian DREAM module.
Definition TasmanianDREAM.hpp:80