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 "StochasticToolsApp.h" 13 : #include "MooseObject.h" 14 : #include "CovarianceInterface.h" 15 : 16 : /** 17 : * Base class for covariance functions that are used in Gaussian Processes 18 : */ 19 : class CovarianceFunctionBase : public MooseObject, public CovarianceInterface 20 : { 21 : public: 22 : static InputParameters validParams(); 23 : CovarianceFunctionBase(const InputParameters & parameters); 24 : 25 : /// Generates the Covariance Matrix given two sets of points in the parameter space 26 : /// @param K Reference to a matrix which should be populated by the covariance entries 27 : /// @param x Reference to the first set of points 28 : /// @param xp Reference to the second set of points 29 : /// @param is_self_covariance Switch to enable adding the noise variance to the diagonal of the covariance matrix 30 : virtual void computeCovarianceMatrix(RealEigenMatrix & K, 31 : const RealEigenMatrix & x, 32 : const RealEigenMatrix & xp, 33 : const bool is_self_covariance) const = 0; 34 : 35 : /// Load some hyperparameters into the local maps contained in this object. 36 : /// @param map Input map of scalar hyperparameters 37 : /// @param vec_map Input map of vector hyperparameters 38 : void loadHyperParamMap(const std::unordered_map<std::string, Real> & map, 39 : const std::unordered_map<std::string, std::vector<Real>> & vec_map); 40 : 41 : /// Populates the input maps with the owned hyperparameters. 42 : /// @param map Map of scalar hyperparameters that should be populated 43 : /// @param vec_map Map of vector hyperparameters that should be populated 44 : void buildHyperParamMap(std::unordered_map<std::string, Real> & map, 45 : std::unordered_map<std::string, std::vector<Real>> & vec_map) const; 46 : 47 : /// Get the default minimum and maximum and size of a hyperparameter. 48 : /// Returns false is the parameter has not been found in this covariance object. 49 : /// @param name The name of the hyperparameter 50 : /// @param size Reference to an unsigned int that will contain the size of the 51 : /// hyperparameter (will be populated with 1 if it is scalar) 52 : /// @param min Reference to a number which will be populated by the maximum allowed value of the hyperparameter 53 : /// @param max Reference to a number which will be populated by the minimum allowed value of the hyperparameter 54 : virtual bool 55 : getTuningData(const std::string & name, unsigned int & size, Real & min, Real & max) const; 56 : 57 : /// Populate a map with the names and types of the dependent covariance functions 58 : /// @param name_type_map Reference to the map which should be populated 59 : void dependentCovarianceTypes(std::map<UserObjectName, std::string> & name_type_map) const; 60 : 61 : /// Get the names of the dependent covariances 62 : const std::vector<UserObjectName> & dependentCovarianceNames() const 63 : { 64 386 : return _dependent_covariance_names; 65 : } 66 : 67 : /// Redirect dK/dhp for hyperparameter "hp". 68 : /// Returns false is the parameter has not been found in this covariance object. 69 : /// @param dKdhp The matrix which should be populated with the derivatives 70 : /// @param x The input vector for which the derivatives of the covariance matrix 71 : /// is computed 72 : /// @param hyper_param_name The name of the hyperparameter 73 : /// @param ind The index within the hyperparameter. 0 if it is a scalar parameter. 74 : /// If it is a vector parameter, it should be the index within the vector. 75 : virtual bool computedKdhyper(RealEigenMatrix & dKdhp, 76 : const RealEigenMatrix & x, 77 : const std::string & hyper_param_name, 78 : unsigned int ind) const; 79 : 80 : /// Check if a given parameter is tunable 81 : /// @param The name of the hyperparameter 82 : virtual bool isTunable(const std::string & name) const; 83 : 84 : /// Return the number of outputs assumed for this covariance function 85 116590 : unsigned int numOutputs() const { return _num_outputs; } 86 : 87 : /// Get the map of scalar parameters 88 : std::unordered_map<std::string, Real> & hyperParamMapReal() { return _hp_map_real; } 89 : 90 : /// Get the map of vector parameters 91 : std::unordered_map<std::string, std::vector<Real>> & hyperParamMapVectorReal() 92 : { 93 : return _hp_map_vector_real; 94 : } 95 : 96 : protected: 97 : /// Register a scalar hyperparameter to this covariance function 98 : /// @param name The name of the parameter 99 : /// @param value The initial value of the parameter 100 : /// @param is_tunable If the parameter is tunable during optimization 101 : const Real & 102 : addRealHyperParameter(const std::string & name, const Real value, const bool is_tunable); 103 : 104 : /// Register a vector hyperparameter to this covariance function 105 : /// @param name The name of the parameter 106 : /// @param value The initial value of the parameter 107 : /// @param is_tunable If the parameter is tunable during optimization 108 : const std::vector<Real> & addVectorRealHyperParameter(const std::string & name, 109 : const std::vector<Real> value, 110 : const bool is_tunable); 111 : 112 : /// Map of real-valued hyperparameters 113 : std::unordered_map<std::string, Real> _hp_map_real; 114 : 115 : /// Map of vector-valued hyperparameters 116 : std::unordered_map<std::string, std::vector<Real>> _hp_map_vector_real; 117 : 118 : /// list of tunable hyper-parameters 119 : std::unordered_set<std::string> _tunable_hp; 120 : 121 : /// The number of outputs this covariance function is used to describe 122 : const unsigned int _num_outputs; 123 : 124 : /// The names of the dependent covariance functions 125 : const std::vector<UserObjectName> _dependent_covariance_names; 126 : 127 : /// The types of the dependent covariance functions 128 : std::vector<std::string> _dependent_covariance_types; 129 : 130 : /// Vector of pointers to the dependent covariance functions 131 : std::vector<CovarianceFunctionBase *> _covariance_functions; 132 : };