LCOV - code coverage report
Current view: top level - include/utils - GaussianProcess.h (source / functions) Hit Total Coverage
Test: idaholab/moose stochastic_tools: #32971 (54bef8) with base c6cf66 Lines: 24 24 100.0 %
Date: 2026-05-29 20:40:35 Functions: 0 0 -
Legend: Lines: hit not hit

          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 "Standardizer.h"
      13             : #include <Eigen/Dense>
      14             : 
      15             : #include "CovarianceFunctionBase.h"
      16             : 
      17             : namespace StochasticTools
      18             : {
      19             : 
      20             : /**
      21             :  * Utility class dedicated to hold structures and functions commont to
      22             :  * Gaussian Processes. It can be used to standardize parameters, manipulate
      23             :  * covariance data and compute additional stored matrices.
      24             :  */
      25             : class GaussianProcess
      26             : {
      27             : public:
      28             :   GaussianProcess();
      29             : 
      30             :   /**
      31             :    * Initializes the most important structures in the Gaussian Process: the
      32             :    * covariance function and a tuning map which is used if the user requires
      33             :    * parameter tuning.
      34             :    * @param covariance_function Pointer to the covariance function that
      35             :    *                            needs to be used for the Gaussian Process.
      36             :    * @param params_to_tune List of parameters which need to be tuned.
      37             :    * @param min List of lower bounds for the parameter tuning.
      38             :    * @param max List of upper bounds for parameter tuning.
      39             :    */
      40             :   void initialize(CovarianceFunctionBase * covariance_function,
      41             :                   const std::vector<std::string> & params_to_tune,
      42             :                   const std::vector<Real> & min = std::vector<Real>(),
      43             :                   const std::vector<Real> & max = std::vector<Real>());
      44             : 
      45             :   /// Structure containing the optimization options for
      46             :   /// hyperparameter-tuning
      47             :   struct GPOptimizerOptions
      48             :   {
      49             :     /// Default constructor
      50             :     GPOptimizerOptions();
      51             :     /**
      52             :      * Construct a new GPOptimizerOptions object using
      53             :      * input parameters that will control the optimization
      54             :      * @param show_every_nth_iteration To show the loss value at every n-th iteration, if set to 0,
      55             :      * nothing is displayed
      56             :      * @param num_iter The number of iterations we want in the optimization of the GP
      57             :      * @param batch_size The number of samples in each batch
      58             :      * @param learning_rate The learning rate for parameter updates
      59             :      * @param b1 Tuning constant for the Adam algorithm
      60             :      * @param b2 Tuning constant for the Adam algorithm
      61             :      * @param eps Tuning constant for the Adam algorithm
      62             :      * @param lambda Tuning constant for the Adam algorithm
      63             :      */
      64             :     GPOptimizerOptions(const bool show_every_nth_iteration = 1,
      65             :                        const unsigned int num_iter = 1000,
      66             :                        const unsigned int batch_size = 0,
      67             :                        const Real learning_rate = 1e-3,
      68             :                        const Real b1 = 0.9,
      69             :                        const Real b2 = 0.999,
      70             :                        const Real eps = 1e-7,
      71             :                        const Real lambda = 0.0);
      72             : 
      73             :     /// Switch to enable verbose output for parameter tuning at every n-th iteration
      74             :     const unsigned int show_every_nth_iteration = false;
      75             :     /// The number of iterations for Adam optimizer
      76             :     const unsigned int num_iter = 1000;
      77             :     /// The batch isize for Adam optimizer
      78             :     const unsigned int batch_size = 0;
      79             :     /// The learning rate for Adam optimizer
      80             :     const Real learning_rate = 1e-3;
      81             :     /// Tuning parameter from the paper
      82             :     const Real b1 = 0.9;
      83             :     /// Tuning parameter from the paper
      84             :     const Real b2 = 0.999;
      85             :     /// Tuning parameter from the paper
      86             :     const Real eps = 1e-7;
      87             :     /// Tuning parameter from the paper
      88             :     const Real lambda = 0.0;
      89             :   };
      90             :   /**
      91             :    * Sets up the covariance matrix given data and optimization options.
      92             :    * @param training_params The training parameter values (x values) for the
      93             :    *                        covariance matrix.
      94             :    * @param training_data The training data (y values) for the inversion of the
      95             :    *                      covariance matrix.
      96             :    * @param opts The optimizer options.
      97             :    */
      98             :   void setupCovarianceMatrix(const RealEigenMatrix & training_params,
      99             :                              const RealEigenMatrix & training_data,
     100             :                              const GPOptimizerOptions & opts);
     101             : 
     102             :   /**
     103             :    * Sets up the Cholesky decomposition and inverse action of the covariance matrix.
     104             :    * @param input The vector/matrix which right multiples the inverse of the covariance matrix.
     105             :    */
     106             :   void setupStoredMatrices(const RealEigenMatrix & input);
     107             : 
     108             :   /**
     109             :    * Finds and links the covariance function to this object. Used mainly in the
     110             :    * covariance data action.
     111             :    * @param covariance_function Pointer to the covariance function that
     112             :    *                            needs to be used for the Gaussian Process.
     113             :    */
     114             :   void linkCovarianceFunction(CovarianceFunctionBase * covariance_function);
     115             : 
     116             :   /**
     117             :    * Sets up the tuning map which is used if the user requires parameter tuning.
     118             :    * @param params_to_tune List of parameters which need to be tuned.
     119             :    * @param min List of lower bounds for the parameter tuning.
     120             :    * @param max List of upper bounds for parameter tuning.
     121             :    */
     122             :   void generateTuningMap(const std::vector<std::string> & params_to_tune,
     123             :                          const std::vector<Real> & min = std::vector<Real>(),
     124             :                          const std::vector<Real> & max = std::vector<Real>());
     125             : 
     126             :   /**
     127             :    * Standardizes the vector of input parameters (x values).
     128             :    * @param parameters The vector/matrix of input data.
     129             :    * @param keep_moments If previously computed or new moments are to be used.
     130             :    */
     131             :   void standardizeParameters(RealEigenMatrix & parameters, bool keep_moments = false);
     132             : 
     133             :   /**
     134             :    * Standardizes the vector of responses (y values).
     135             :    * @param data The vector/matrix of input data.
     136             :    * @param keep_moments If previously computed or new moments are to be used.
     137             :    */
     138             :   void standardizeData(RealEigenMatrix & data, bool keep_moments = false);
     139             : 
     140             :   // Tune hyperparameters using Adam
     141             :   void tuneHyperParamsAdam(const RealEigenMatrix & training_params,
     142             :                            const RealEigenMatrix & training_data,
     143             :                            const GPOptimizerOptions & opts);
     144             : 
     145             :   // Computes the loss function
     146             :   Real getLoss(RealEigenMatrix & inputs, RealEigenMatrix & outputs);
     147             : 
     148             :   // Computes Gradient of the loss function
     149             :   std::vector<Real> getGradient(RealEigenMatrix & inputs) const;
     150             : 
     151             :   /// Function used to convert the hyperparameter maps in this object to
     152             :   /// vectors
     153             :   void mapToVec(
     154             :       const std::unordered_map<std::string, std::tuple<unsigned int, unsigned int, Real, Real>> &
     155             :           tuning_data,
     156             :       const std::unordered_map<std::string, Real> & scalar_map,
     157             :       const std::unordered_map<std::string, std::vector<Real>> & vector_map,
     158             :       std::vector<Real> & vec) const;
     159             : 
     160             :   /// Function used to convert the vectors back to hyperparameter maps
     161             :   void vecToMap(
     162             :       const std::unordered_map<std::string, std::tuple<unsigned int, unsigned int, Real, Real>> &
     163             :           tuning_data,
     164             :       std::unordered_map<std::string, Real> & scalar_map,
     165             :       std::unordered_map<std::string, std::vector<Real>> & vector_map,
     166             :       const std::vector<Real> & vec) const;
     167             : 
     168             :   /// @{
     169             :   /**
     170             :    * Get constant reference to the contained structures
     171             :    */
     172      151925 :   const StochasticTools::Standardizer & getParamStandardizer() const { return _param_standardizer; }
     173      303850 :   const StochasticTools::Standardizer & getDataStandardizer() const { return _data_standardizer; }
     174             :   const RealEigenMatrix & getK() const { return _K; }
     175      151925 :   const RealEigenMatrix & getKResultsSolve() const { return _K_results_solve; }
     176      151925 :   const Eigen::LLT<RealEigenMatrix> & getKCholeskyDecomp() const { return _K_cho_decomp; }
     177      455888 :   const CovarianceFunctionBase & getCovarFunction() const { return *_covariance_function; }
     178          14 :   const CovarianceFunctionBase * getCovarFunctionPtr() const { return _covariance_function; }
     179          14 :   const std::string & getCovarType() const { return _covar_type; }
     180          14 :   const std::string & getCovarName() const { return _covar_name; }
     181             :   const std::vector<UserObjectName> & getDependentCovarNames() const
     182             :   {
     183          14 :     return _dependent_covar_names;
     184             :   }
     185             :   const std::map<UserObjectName, std::string> & getDependentCovarTypes() const
     186             :   {
     187             :     return _dependent_covar_types;
     188             :   }
     189             :   const unsigned int & getCovarNumOutputs() const { return _num_outputs; }
     190             :   const unsigned int & getNumTunableParams() const { return _num_tunable; }
     191          14 :   const std::unordered_map<std::string, Real> & getHyperParamMap() const { return _hyperparam_map; }
     192             :   const std::unordered_map<std::string, std::vector<Real>> & getHyperParamVectorMap() const
     193             :   {
     194          14 :     return _hyperparam_vec_map;
     195             :   }
     196         108 :   const std::vector<Real> & getLengthScales() const { return _length_scales; }
     197             :   ///@}
     198             : 
     199             :   /// @{
     200             :   /**
     201             :    * Get non-constant reference to the contained structures (if they need to be modified from the
     202             :    * utside)
     203             :    */
     204          38 :   StochasticTools::Standardizer & paramStandardizer() { return _param_standardizer; }
     205          38 :   StochasticTools::Standardizer & dataStandardizer() { return _data_standardizer; }
     206          38 :   RealEigenMatrix & K() { return _K; }
     207          38 :   RealEigenMatrix & KResultsSolve() { return _K_results_solve; }
     208          38 :   Eigen::LLT<RealEigenMatrix> & KCholeskyDecomp() { return _K_cho_decomp; }
     209             :   CovarianceFunctionBase * covarFunctionPtr() { return _covariance_function; }
     210             :   CovarianceFunctionBase & covarFunction() { return *_covariance_function; }
     211          38 :   std::string & covarType() { return _covar_type; }
     212          38 :   std::string & covarName() { return _covar_name; }
     213          38 :   std::map<UserObjectName, std::string> & dependentCovarTypes() { return _dependent_covar_types; }
     214          38 :   std::vector<UserObjectName> & dependentCovarNames() { return _dependent_covar_names; }
     215          38 :   unsigned int & covarNumOutputs() { return _num_outputs; }
     216             :   std::unordered_map<std::string, std::tuple<unsigned int, unsigned int, Real, Real>> & tuningData()
     217             :   {
     218             :     return _tuning_data;
     219             :   }
     220          38 :   std::unordered_map<std::string, Real> & hyperparamMap() { return _hyperparam_map; }
     221             :   std::unordered_map<std::string, std::vector<Real>> & hyperparamVectorMap()
     222             :   {
     223          38 :     return _hyperparam_vec_map;
     224             :   }
     225             :   std::vector<Real> & lengthScales() { return _length_scales; }
     226             :   ///@}
     227             : 
     228             : protected:
     229             :   /// Covariance function object
     230             :   CovarianceFunctionBase * _covariance_function = nullptr;
     231             : 
     232             :   /// Contains tuning inforation. Index of hyperparam, size, and min/max bounds
     233             :   std::unordered_map<std::string, std::tuple<unsigned int, unsigned int, Real, Real>> _tuning_data;
     234             : 
     235             :   /// Number of tunable hyperparameters
     236             :   unsigned int _num_tunable;
     237             : 
     238             :   /// Type of covariance function used for this GP
     239             :   std::string _covar_type;
     240             : 
     241             :   /// The name of the covariance function used in this GP
     242             :   std::string _covar_name;
     243             : 
     244             :   /// The names of the covariance functions the used covariance function depends on
     245             :   std::vector<UserObjectName> _dependent_covar_names;
     246             : 
     247             :   /// The types of the covariance functions the used covariance function depends on
     248             :   std::map<UserObjectName, std::string> _dependent_covar_types;
     249             : 
     250             :   /// The number of outputs of the GP
     251             :   unsigned int _num_outputs;
     252             : 
     253             :   /// Scalar hyperparameters. Stored for use in surrogate
     254             :   std::unordered_map<std::string, Real> _hyperparam_map;
     255             : 
     256             :   /// Vector hyperparameters. Stored for use in surrogate
     257             :   std::unordered_map<std::string, std::vector<Real>> _hyperparam_vec_map;
     258             : 
     259             :   /// Standardizer for use with params (x)
     260             :   StochasticTools::Standardizer _param_standardizer;
     261             : 
     262             :   /// Standardizer for use with data (y)
     263             :   StochasticTools::Standardizer _data_standardizer;
     264             : 
     265             :   /// An _n_sample by _n_sample covariance matrix constructed from the selected kernel function
     266             :   RealEigenMatrix _K;
     267             : 
     268             :   /// A solve of Ax=b via Cholesky.
     269             :   RealEigenMatrix _K_results_solve;
     270             : 
     271             :   /// Cholesky decomposition Eigen object
     272             :   Eigen::LLT<RealEigenMatrix> _K_cho_decomp;
     273             : 
     274             :   /// Paramaters (x) used for training, along with statistics
     275             :   const RealEigenMatrix * _training_params;
     276             : 
     277             :   /// Data (y) used for training
     278             :   const RealEigenMatrix * _training_data;
     279             : 
     280             :   /// The batch size for Adam optimization
     281             :   unsigned int _batch_size;
     282             : 
     283             :   /// To return the GP length scales for active learning
     284             :   std::vector<Real> _length_scales;
     285             : };
     286             : 
     287             : } // StochasticTools namespac
     288             : 
     289             : template <>
     290             : void dataStore(std::ostream & stream, Eigen::LLT<RealEigenMatrix> & decomp, void * context);
     291             : template <>
     292             : void dataLoad(std::istream & stream, Eigen::LLT<RealEigenMatrix> & decomp, void * context);
     293             : 
     294             : template <>
     295             : void dataStore(std::ostream & stream, StochasticTools::GaussianProcess & gp_utils, void * context);
     296             : template <>
     297             : void dataLoad(std::istream & stream, StochasticTools::GaussianProcess & gp_utils, void * context);

Generated by: LCOV version 1.14