Line data Source code
1 : //* This file is part of the MOOSE framework 2 : //* https://mooseframework.inl.gov 3 : //* 4 : //* All rights reserved, see COPYRIGHT for full restrictions 5 : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT 6 : //* 7 : //* Licensed under LGPL 2.1, please see LICENSE for details 8 : //* https://www.gnu.org/licenses/lgpl-2.1.html 9 : 10 : #pragma once 11 : 12 : #include "GeneralReporter.h" 13 : #include "PMCMCBase.h" 14 : #include "LikelihoodFunctionBase.h" 15 : #include "LikelihoodInterface.h" 16 : #include "Distribution.h" 17 : 18 : /** 19 : * PMCMCDecision will help making sample accept/reject decisions in MCMC 20 : * schemes (for e.g., when performing Bayesian inference). 21 : */ 22 : class PMCMCDecision : public GeneralReporter, public LikelihoodInterface 23 : { 24 : public: 25 : static InputParameters validParams(); 26 : PMCMCDecision(const InputParameters & parameters); 27 : virtual void initialize() override; 28 600 : virtual void finalize() override {} 29 : virtual void execute() override; 30 : 31 : protected: 32 : /** 33 : * Compute the evidence (aka, betterness of the proposed sample vs the previous) 34 : * @param evidence The evidence vector to be filled 35 : * @param input_matrix The matrix of proposed inputs that are provided 36 : */ 37 : virtual void computeEvidence(std::vector<Real> & evidence, 38 : const DenseMatrix<Real> & input_matrix); 39 : 40 : /** 41 : * Compute the transition probability vector (after the computation of evidence) 42 : * @param tv The transition probability vector to be filled 43 : * @param evidence The vector of evidences provided 44 : */ 45 : virtual void computeTransitionVector(std::vector<Real> & tv, const std::vector<Real> & evidence); 46 : 47 : /** 48 : * Resample inputs given the transition vector (after transition vector computed) 49 : * @param req_inputs The vector of accepted samples to be filled 50 : * @param input_matrix The matrix of proposed inputs provided 51 : * @param tv The vector of transition probabilities provided 52 : * @param parallel_index The current parallel proposal index provided 53 : */ 54 : virtual void nextSamples(std::vector<Real> & req_inputs, 55 : DenseMatrix<Real> & input_matrix, 56 : const std::vector<Real> & tv, 57 : const unsigned int & parallel_index); 58 : 59 : /** 60 : * Compute the next set of seeds to facilitate proposals. 61 : * Set to empty in base to permit flexibility for MCMC samplers 62 : */ 63 500 : virtual void nextSeeds() {} 64 : 65 : /// Flag to specify if a pre-trained Gaussian process model is used 66 1200 : virtual bool usingGP() const { return false; } 67 : 68 : /// Model input data that is uncertain 69 : std::vector<std::vector<Real>> & _inputs; 70 : 71 : /// Transition probability matrix 72 : std::vector<Real> & _tpm; 73 : 74 : /// Model variance term 75 : std::vector<Real> & _variance; 76 : 77 : /// Model noise term to pass to Likelihoods object 78 : Real & _noise; 79 : 80 : /// Storage for the likelihood objects to be utilized 81 : std::vector<const LikelihoodFunctionBase *> _likelihoods; 82 : 83 : /// The MCMC sampler 84 : Sampler & _sampler; 85 : 86 : /// MCMC sampler base 87 : const PMCMCBase * const _pmcmc; 88 : 89 : /// Storage for the number of parallel proposals 90 : dof_id_type _props; 91 : 92 : /// Storage for the random numbers for decision making 93 : const std::vector<Real> & _rnd_vec; 94 : 95 : /// Storage for new proposed variance samples 96 : const std::vector<Real> & _new_var_samples; 97 : 98 : /// Storage for the priors 99 : const std::vector<const Distribution *> _priors; 100 : 101 : /// Storage for the prior over the variance 102 : const Distribution * _var_prior; 103 : 104 : /// Transfer the right outputs to the file 105 : std::vector<Real> * _outputs_required; 106 : 107 : /// Storage for the number of experimental configuration values 108 : dof_id_type _num_confg_values; 109 : 110 : /// Storage for the number of experimental configuration parameters 111 : dof_id_type _num_confg_params; 112 : 113 : /// Storage for previous inputs 114 : DenseMatrix<Real> _data_prev; 115 : 116 : /// Storage for previous variances 117 : std::vector<Real> _var_prev; 118 : 119 : /// Storage for previous outputs 120 : std::vector<Real> _outputs_prev; 121 : 122 : /// Current output values 123 : const std::vector<Real> * _output_value; 124 : 125 : /// Communicator that was split based on samples that have rows 126 : libMesh::Parallel::Communicator & _local_comm; 127 : 128 : /// Ensure that the MCMC algorithm proceeds in a sequential fashion 129 : int _check_step; 130 : };