Doxygen 1.9.1
Toolkit for Adaptive Stochastic Modeling and Non-Intrusive ApproximatioN: Tasmanian v8.1
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 
52 namespace TasDREAM{
53 
56 
71 public:
75  TasmanianDREAM(int cnum_chains, int cnum_dimensions);
77  TasmanianDREAM(int cnum_chains, const TasGrid::TasmanianSparseGrid &grid);
80 
82  static const char* getVersion(){ return TASMANIAN_VERSION_STRING; }
84  static const char* getLicense(){ return TASMANIAN_LICENSE; }
86  static int getVersionMajor(){ return TASMANIAN_VERSION_MAJOR; }
88  static int getVersionMinor(){ return TASMANIAN_VERSION_MINOR; }
89 
91  int getNumDimensions() const{ return (int) num_dimensions; }
93  int getNumChains() const{ return (int) num_chains; }
95  size_t getNumHistory() const{ return pdf_history.size(); }
96 
98  bool isStateReady() const{ return init_state; }
100  bool isPDFReady() const{ return init_values; }
101 
103 
107  void setState(const std::vector<double> &new_state);
108 
110 
115  void setState(std::function<void(double *)> update_state);
116 
118 
123  void setPDFvalues(const std::vector<double> &new_values);
124 
126 
131  void setPDFvalues(std::function<void(const std::vector<double> &state, std::vector<double> &values)> probability_distribution);
132 
135 
137 
142  void getIJKdelta(size_t i, size_t j, size_t k, double w, std::vector<double> &x) const;
143 
145 
147  void getChainState(size_t i, double *x) const{ std::copy_n(state.begin() + i * num_dimensions, num_dimensions, x); }
148 
150  const std::vector<double>& getChainState() const{ return state; }
151 
153 
155  double getPDFvalue(size_t i) const{ return pdf_values[i]; }
156 
158 
160  void expandHistory(int num_snapshots);
161 
163 
165  void saveStateHistory(size_t num_accepted);
166 
168  const std::vector<double>& getHistory() const{ return history; }
169 
171  const std::vector<double>& getHistoryPDF() const{ return pdf_history; }
172 
174  void getHistoryMeanVariance(std::vector<double> &mean, std::vector<double> &var) const;
175 
177  void getApproximateMode(std::vector<double> &mode) const;
178 
180  std::vector<double> getApproximateMode() const{
181  std::vector<double> mode;
182  getApproximateMode(mode);
183  return mode;
184  }
185 
187  void clearHistory();
188 
190  double getAcceptanceRate() const{ return ((pdf_history.empty()) ? 0 : ((double) accepted) / ((double) pdf_history.size())); }
191 
192  // file I/O
193 private:
194  size_t num_chains, num_dimensions;
195  bool init_state, init_values;
196 
197  size_t accepted;
198 
199  std::vector<double> state, history;
200  std::vector<double> pdf_values, pdf_history;
201 };
202 
203 }
204 
205 #endif
Contains the current state and the history of the DREAM chains.
Definition: tsgDreamState.hpp:70
~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:155
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:88
const std::vector< double > & getHistory() const
Return a const reference to the internal state vector.
Definition: tsgDreamState.hpp:168
static int getVersionMajor()
Return the major version of the library.
Definition: tsgDreamState.hpp:86
void saveStateHistory(size_t num_accepted)
Appends the current state to the history.
int getNumDimensions() const
Return the number of dimensions.
Definition: tsgDreamState.hpp:91
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.
std::vector< double > getApproximateMode() const
Overload that returns the vector.
Definition: tsgDreamState.hpp:180
bool isStateReady() const
Return true if the state has already been initialized with setState().
Definition: tsgDreamState.hpp:98
void getApproximateMode(std::vector< double > &mode) const
Return the sample with highest probability, searchers within the history.
static const char * getVersion()
Return human readable string with the version.
Definition: tsgDreamState.hpp:82
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:147
TasmanianDREAM(int cnum_chains, int cnum_dimensions)
Constructor for a DREAM state with the number of chains and dimensions.
void clearPDFvalues()
Erase the currently stored pdf values, useful when switching between log and non-log form sampling.
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:190
size_t getNumHistory() const
Return the number of saved vectors in the history.
Definition: tsgDreamState.hpp:95
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:93
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.
const std::vector< double > & getHistoryPDF() const
Return a const reference to the internal state vector.
Definition: tsgDreamState.hpp:171
bool isPDFReady() const
Return true if the pdf values have already been initialized with setPDF().
Definition: tsgDreamState.hpp:100
const std::vector< double > & getChainState() const
Return a const reference to the internal state vector.
Definition: tsgDreamState.hpp:150
static const char * getLicense()
Return human readable sting with the license, refer to the LICENSE file for details.
Definition: tsgDreamState.hpp:84
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
Encapsulates the Tasmanian DREAM module.
Definition: TasmanianDREAM.hpp:80