https://mooseframework.inl.gov
Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
CovarianceFunctionBase Class Referenceabstract

Base class for covariance functions that are used in Gaussian Processes. More...

#include <CovarianceFunctionBase.h>

Inheritance diagram for CovarianceFunctionBase:
[legend]

Public Types

typedef DataFileName DataFileParameterType
 

Public Member Functions

 CovarianceFunctionBase (const InputParameters &parameters)
 
virtual void computeCovarianceMatrix (RealEigenMatrix &K, const RealEigenMatrix &x, const RealEigenMatrix &xp, const bool is_self_covariance) const =0
 Generates the Covariance Matrix given two sets of points in the parameter space. More...
 
void loadHyperParamMap (const std::unordered_map< std::string, Real > &map, const std::unordered_map< std::string, std::vector< Real >> &vec_map)
 Load some hyperparameters into the local maps contained in this object. More...
 
void buildHyperParamMap (std::unordered_map< std::string, Real > &map, std::unordered_map< std::string, std::vector< Real >> &vec_map) const
 Populates the input maps with the owned hyperparameters. More...
 
virtual bool getTuningData (const std::string &name, unsigned int &size, Real &min, Real &max) const
 Get the default minimum and maximum and size of a hyperparameter. More...
 
void dependentCovarianceTypes (std::map< UserObjectName, std::string > &name_type_map) const
 Populate a map with the names and types of the dependent covariance functions. More...
 
const std::vector< UserObjectName > & dependentCovarianceNames () const
 Get the names of the dependent covariances. More...
 
virtual bool computedKdhyper (RealEigenMatrix &dKdhp, const RealEigenMatrix &x, const std::string &hyper_param_name, unsigned int ind) const
 Redirect dK/dhp for hyperparameter "hp". More...
 
virtual bool isTunable (const std::string &name) const
 Check if a given parameter is tunable. More...
 
unsigned int numOutputs () const
 Return the number of outputs assumed for this covariance function. More...
 
std::unordered_map< std::string, Real > & hyperParamMapReal ()
 Get the map of scalar parameters. More...
 
std::unordered_map< std::string, std::vector< Real > > & hyperParamMapVectorReal ()
 Get the map of vector parameters. More...
 
virtual bool enabled () const
 
std::shared_ptr< MooseObjectgetSharedPtr ()
 
std::shared_ptr< const MooseObjectgetSharedPtr () const
 
MooseAppgetMooseApp () const
 
const std::string & type () const
 
virtual const std::string & name () const
 
std::string typeAndName () const
 
std::string errorPrefix (const std::string &error_type) const
 
void callMooseError (std::string msg, const bool with_prefix) const
 
MooseObjectParameterName uniqueParameterName (const std::string &parameter_name) const
 
const InputParametersparameters () const
 
MooseObjectName uniqueName () const
 
const T & getParam (const std::string &name) const
 
std::vector< std::pair< T1, T2 > > getParam (const std::string &param1, const std::string &param2) const
 
const T * queryParam (const std::string &name) const
 
const T & getRenamedParam (const std::string &old_name, const std::string &new_name) const
 
getCheckedPointerParam (const std::string &name, const std::string &error_string="") const
 
bool isParamValid (const std::string &name) const
 
bool isParamSetByUser (const std::string &nm) const
 
void paramError (const std::string &param, Args... args) const
 
void paramWarning (const std::string &param, Args... args) const
 
void paramInfo (const std::string &param, Args... args) const
 
void connectControllableParams (const std::string &parameter, const std::string &object_type, const std::string &object_name, const std::string &object_parameter) const
 
void mooseError (Args &&... args) const
 
void mooseErrorNonPrefixed (Args &&... args) const
 
void mooseDocumentedError (const std::string &repo_name, const unsigned int issue_num, Args &&... args) const
 
void mooseWarning (Args &&... args) const
 
void mooseWarningNonPrefixed (Args &&... args) const
 
void mooseDeprecated (Args &&... args) const
 
void mooseInfo (Args &&... args) const
 
std::string getDataFileName (const std::string &param) const
 
std::string getDataFileNameByName (const std::string &relative_path) const
 
std::string getDataFilePath (const std::string &relative_path) const
 
const Parallel::Communicator & comm () const
 
processor_id_type n_processors () const
 
processor_id_type processor_id () const
 

Static Public Member Functions

static InputParameters validParams ()
 

Public Attributes

const ConsoleStream _console
 

Protected Member Functions

const RealaddRealHyperParameter (const std::string &name, const Real value, const bool is_tunable)
 Register a scalar hyperparameter to this covariance function. More...
 
const std::vector< Real > & addVectorRealHyperParameter (const std::string &name, const std::vector< Real > value, const bool is_tunable)
 Register a vector hyperparameter to this covariance function. More...
 
CovarianceFunctionBasegetCovarianceFunctionByName (const UserObjectName &name) const
 Lookup a CovarianceFunction object by name and return pointer. More...
 

Protected Attributes

std::unordered_map< std::string, Real_hp_map_real
 Map of real-valued hyperparameters. More...
 
std::unordered_map< std::string, std::vector< Real > > _hp_map_vector_real
 Map of vector-valued hyperparameters. More...
 
std::unordered_set< std::string > _tunable_hp
 list of tunable hyper-parameters More...
 
const unsigned int _num_outputs
 The number of outputs this covariance function is used to describe. More...
 
const std::vector< UserObjectName > _dependent_covariance_names
 The names of the dependent covariance functions. More...
 
std::vector< std::string > _dependent_covariance_types
 The types of the dependent covariance functions. More...
 
std::vector< CovarianceFunctionBase * > _covariance_functions
 Vector of pointers to the dependent covariance functions. More...
 
const bool & _enabled
 
MooseApp_app
 
const std::string _type
 
const std::string _name
 
const InputParameters_pars
 
Factory_factory
 
ActionFactory_action_factory
 
const Parallel::Communicator & _communicator
 

Detailed Description

Base class for covariance functions that are used in Gaussian Processes.

Definition at line 19 of file CovarianceFunctionBase.h.

Constructor & Destructor Documentation

◆ CovarianceFunctionBase()

CovarianceFunctionBase::CovarianceFunctionBase ( const InputParameters parameters)

Definition at line 26 of file CovarianceFunctionBase.C.

29  _num_outputs(getParam<unsigned int>("num_outputs")),
30  _dependent_covariance_names(getParam<std::vector<UserObjectName>>("covariance_functions"))
31 
32 {
33  // Fetch the dependent covariance functions
34  for (const auto & name : _dependent_covariance_names)
35  {
37  _dependent_covariance_types.push_back(_covariance_functions.back()->type());
38  }
39 }
std::vector< std::string > _dependent_covariance_types
The types of the dependent covariance functions.
MooseObject(const InputParameters &parameters)
std::vector< CovarianceFunctionBase * > _covariance_functions
Vector of pointers to the dependent covariance functions.
virtual const std::string & name() const
const unsigned int _num_outputs
The number of outputs this covariance function is used to describe.
const T & getParam(const std::string &name) const
const std::vector< UserObjectName > _dependent_covariance_names
The names of the dependent covariance functions.
const InputParameters & parameters() const
CovarianceInterface(const InputParameters &parameters)
CovarianceFunctionBase * getCovarianceFunctionByName(const UserObjectName &name) const
Lookup a CovarianceFunction object by name and return pointer.

Member Function Documentation

◆ addRealHyperParameter()

const Real & CovarianceFunctionBase::addRealHyperParameter ( const std::string &  name,
const Real  value,
const bool  is_tunable 
)
protected

Register a scalar hyperparameter to this covariance function.

Parameters
nameThe name of the parameter
valueThe initial value of the parameter
is_tunableIf the parameter is tunable during optimization

Definition at line 52 of file CovarianceFunctionBase.C.

55 {
56  const auto prefixed_name = _name + ":" + name;
57  if (is_tunable)
58  _tunable_hp.insert(prefixed_name);
59  return _hp_map_real.emplace(prefixed_name, value).first->second;
60 }
std::unordered_map< std::string, Real > _hp_map_real
Map of real-valued hyperparameters.
std::unordered_set< std::string > _tunable_hp
list of tunable hyper-parameters
virtual const std::string & name() const
const std::string _name

◆ addVectorRealHyperParameter()

const std::vector< Real > & CovarianceFunctionBase::addVectorRealHyperParameter ( const std::string &  name,
const std::vector< Real value,
const bool  is_tunable 
)
protected

Register a vector hyperparameter to this covariance function.

Parameters
nameThe name of the parameter
valueThe initial value of the parameter
is_tunableIf the parameter is tunable during optimization

Definition at line 63 of file CovarianceFunctionBase.C.

Referenced by LMC::LMC().

66 {
67  const auto prefixed_name = _name + ":" + name;
68  if (is_tunable)
69  _tunable_hp.insert(prefixed_name);
70  return _hp_map_vector_real.emplace(prefixed_name, value).first->second;
71 }
std::unordered_set< std::string > _tunable_hp
list of tunable hyper-parameters
virtual const std::string & name() const
const std::string _name
std::unordered_map< std::string, std::vector< Real > > _hp_map_vector_real
Map of vector-valued hyperparameters.

◆ buildHyperParamMap()

void CovarianceFunctionBase::buildHyperParamMap ( std::unordered_map< std::string, Real > &  map,
std::unordered_map< std::string, std::vector< Real >> &  vec_map 
) const

Populates the input maps with the owned hyperparameters.

Parameters
mapMap of scalar hyperparameters that should be populated
vec_mapMap of vector hyperparameters that should be populated

Definition at line 115 of file CovarianceFunctionBase.C.

Referenced by StochasticTools::GaussianProcess::setupCovarianceMatrix(), and StochasticTools::GaussianProcess::tuneHyperParamsAdam().

118 {
119  // First, add the hyperparameters of the dependent covariance functions
120  for (const auto dependent_covar : _covariance_functions)
121  dependent_covar->buildHyperParamMap(map, vec_map);
122 
123  // At the end we just append the hyperparameters this object owns
124  for (const auto & iter : _hp_map_real)
125  map[iter.first] = iter.second;
126  for (const auto & iter : _hp_map_vector_real)
127  vec_map[iter.first] = iter.second;
128 }
std::unordered_map< std::string, Real > _hp_map_real
Map of real-valued hyperparameters.
std::vector< CovarianceFunctionBase * > _covariance_functions
Vector of pointers to the dependent covariance functions.
std::unordered_map< std::string, std::vector< Real > > _hp_map_vector_real
Map of vector-valued hyperparameters.

◆ computeCovarianceMatrix()

virtual void CovarianceFunctionBase::computeCovarianceMatrix ( RealEigenMatrix K,
const RealEigenMatrix x,
const RealEigenMatrix xp,
const bool  is_self_covariance 
) const
pure virtual

Generates the Covariance Matrix given two sets of points in the parameter space.

Parameters
KReference to a matrix which should be populated by the covariance entries
xReference to the first set of points
xpReference to the second set of points
is_self_covarianceSwitch to enable adding the noise variance to the diagonal of the covariance matrix

Implemented in LMC, ExponentialCovariance, MaternHalfIntCovariance, and SquaredExponentialCovariance.

Referenced by GaussianProcessSurrogate::evaluate(), StochasticTools::GaussianProcess::getLoss(), and StochasticTools::GaussianProcess::setupCovarianceMatrix().

◆ computedKdhyper()

bool CovarianceFunctionBase::computedKdhyper ( RealEigenMatrix dKdhp,
const RealEigenMatrix x,
const std::string &  hyper_param_name,
unsigned int  ind 
) const
virtual

Redirect dK/dhp for hyperparameter "hp".

Returns false is the parameter has not been found in this covariance object.

Parameters
dKdhpThe matrix which should be populated with the derivatives
xThe input vector for which the derivatives of the covariance matrix is computed
hyper_param_nameThe name of the hyperparameter
indThe index within the hyperparameter. 0 if it is a scalar parameter. If it is a vector parameter, it should be the index within the vector.

Reimplemented in ExponentialCovariance, MaternHalfIntCovariance, SquaredExponentialCovariance, and LMC.

Definition at line 42 of file CovarianceFunctionBase.C.

Referenced by StochasticTools::GaussianProcess::getGradient().

46 {
47  mooseError("Hyperparameter tuning not set up for this covariance function. Please define "
48  "computedKdhyper() to compute gradient.");
49 }
void mooseError(Args &&... args) const

◆ dependentCovarianceNames()

const std::vector<UserObjectName>& CovarianceFunctionBase::dependentCovarianceNames ( ) const
inline

Get the names of the dependent covariances.

Definition at line 62 of file CovarianceFunctionBase.h.

Referenced by StochasticTools::GaussianProcess::linkCovarianceFunction().

63  {
65  }
const std::vector< UserObjectName > _dependent_covariance_names
The names of the dependent covariance functions.

◆ dependentCovarianceTypes()

void CovarianceFunctionBase::dependentCovarianceTypes ( std::map< UserObjectName, std::string > &  name_type_map) const

Populate a map with the names and types of the dependent covariance functions.

Parameters
name_type_mapReference to the map which should be populated

Definition at line 163 of file CovarianceFunctionBase.C.

Referenced by StochasticTools::GaussianProcess::linkCovarianceFunction().

165 {
166  for (const auto dependent_covar : _covariance_functions)
167  {
168  dependent_covar->dependentCovarianceTypes(name_type_map);
169  name_type_map.insert(std::make_pair(dependent_covar->name(), dependent_covar->type()));
170  }
171 }
std::vector< CovarianceFunctionBase * > _covariance_functions
Vector of pointers to the dependent covariance functions.

◆ getCovarianceFunctionByName()

CovarianceFunctionBase * CovarianceInterface::getCovarianceFunctionByName ( const UserObjectName &  name) const
protectedinherited

Lookup a CovarianceFunction object by name and return pointer.

Definition at line 25 of file CovarianceInterface.C.

Referenced by ActiveLearningGaussianProcess::ActiveLearningGaussianProcess(), CovarianceFunctionBase(), GaussianProcessTrainer::GaussianProcessTrainer(), and GaussianProcessSurrogate::setupCovariance().

26 {
27  std::vector<CovarianceFunctionBase *> models;
29  .query()
30  .condition<AttribName>(name)
31  .condition<AttribSystem>("CovarianceFunction")
32  .queryInto(models);
33  if (models.empty())
34  mooseError("Unable to find a CovarianceFunction object with the name '" + name + "'");
35  return models[0];
36 }
void mooseError(Args &&... args)
TheWarehouse & theWarehouse() const
const std::string name
Definition: Setup.h:20
Query query()
FEProblemBase & _covar_feproblem
Reference to FEProblemBase instance.

◆ getTuningData()

bool CovarianceFunctionBase::getTuningData ( const std::string &  name,
unsigned int size,
Real min,
Real max 
) const
virtual

Get the default minimum and maximum and size of a hyperparameter.

Returns false is the parameter has not been found in this covariance object.

Parameters
nameThe name of the hyperparameter
sizeReference to an unsigned int that will contain the size of the hyperparameter (will be populated with 1 if it is scalar)
minReference to a number which will be populated by the maximum allowed value of the hyperparameter
maxReference to a number which will be populated by the minimum allowed value of the hyperparameter

Definition at line 131 of file CovarianceFunctionBase.C.

Referenced by StochasticTools::GaussianProcess::generateTuningMap().

135 {
136  // First, check the dependent covariances
137  for (const auto dependent_covar : _covariance_functions)
138  if (dependent_covar->getTuningData(name, size, min, max))
139  return true;
140 
141  min = 1e-9;
142  max = 1e9;
143 
144  if (_hp_map_real.find(name) != _hp_map_real.end())
145  {
146  size = 1;
147  return true;
148  }
149  else if (_hp_map_vector_real.find(name) != _hp_map_vector_real.end())
150  {
151  const auto & vector_value = _hp_map_vector_real.find(name);
152  size = vector_value->second.size();
153  return true;
154  }
155  else
156  {
157  size = 0;
158  return false;
159  }
160 }
std::unordered_map< std::string, Real > _hp_map_real
Map of real-valued hyperparameters.
std::vector< CovarianceFunctionBase * > _covariance_functions
Vector of pointers to the dependent covariance functions.
virtual const std::string & name() const
auto max(const L &left, const R &right)
std::unordered_map< std::string, std::vector< Real > > _hp_map_vector_real
Map of vector-valued hyperparameters.
auto min(const L &left, const R &right)

◆ hyperParamMapReal()

std::unordered_map<std::string, Real>& CovarianceFunctionBase::hyperParamMapReal ( )
inline

Get the map of scalar parameters.

Definition at line 88 of file CovarianceFunctionBase.h.

88 { return _hp_map_real; }
std::unordered_map< std::string, Real > _hp_map_real
Map of real-valued hyperparameters.

◆ hyperParamMapVectorReal()

std::unordered_map<std::string, std::vector<Real> >& CovarianceFunctionBase::hyperParamMapVectorReal ( )
inline

Get the map of vector parameters.

Definition at line 91 of file CovarianceFunctionBase.h.

92  {
93  return _hp_map_vector_real;
94  }
std::unordered_map< std::string, std::vector< Real > > _hp_map_vector_real
Map of vector-valued hyperparameters.

◆ isTunable()

bool CovarianceFunctionBase::isTunable ( const std::string &  name) const
virtual

Check if a given parameter is tunable.

Parameters
Thename of the hyperparameter

Definition at line 74 of file CovarianceFunctionBase.C.

Referenced by StochasticTools::GaussianProcess::generateTuningMap().

75 {
76  // First, we check if the dependent covariances have the parameter
77  for (const auto dependent_covar : _covariance_functions)
78  if (dependent_covar->isTunable(name))
79  return true;
80 
81  if (_tunable_hp.find(name) != _tunable_hp.end())
82  return true;
83  else if (_hp_map_real.find(name) != _hp_map_real.end() ||
85  mooseError("We found hyperparameter ", name, " but it was not declared tunable!");
86 
87  return false;
88 }
std::unordered_map< std::string, Real > _hp_map_real
Map of real-valued hyperparameters.
std::unordered_set< std::string > _tunable_hp
list of tunable hyper-parameters
std::vector< CovarianceFunctionBase * > _covariance_functions
Vector of pointers to the dependent covariance functions.
virtual const std::string & name() const
std::unordered_map< std::string, std::vector< Real > > _hp_map_vector_real
Map of vector-valued hyperparameters.
void mooseError(Args &&... args) const

◆ loadHyperParamMap()

void CovarianceFunctionBase::loadHyperParamMap ( const std::unordered_map< std::string, Real > &  map,
const std::unordered_map< std::string, std::vector< Real >> &  vec_map 
)

Load some hyperparameters into the local maps contained in this object.

Parameters
mapInput map of scalar hyperparameters
vec_mapInput map of vector hyperparameters

Definition at line 91 of file CovarianceFunctionBase.C.

Referenced by LoadCovarianceDataAction::load(), and StochasticTools::GaussianProcess::tuneHyperParamsAdam().

94 {
95  // First, load the hyperparameters of the dependent covariance functions
96  for (const auto dependent_covar : _covariance_functions)
97  dependent_covar->loadHyperParamMap(map, vec_map);
98 
99  // Then we load the hyperparameters of this object
100  for (auto & iter : _hp_map_real)
101  {
102  const auto & map_iter = map.find(iter.first);
103  if (map_iter != map.end())
104  iter.second = map_iter->second;
105  }
106  for (auto & iter : _hp_map_vector_real)
107  {
108  const auto & map_iter = vec_map.find(iter.first);
109  if (map_iter != vec_map.end())
110  iter.second = map_iter->second;
111  }
112 }
std::unordered_map< std::string, Real > _hp_map_real
Map of real-valued hyperparameters.
std::vector< CovarianceFunctionBase * > _covariance_functions
Vector of pointers to the dependent covariance functions.
std::unordered_map< std::string, std::vector< Real > > _hp_map_vector_real
Map of vector-valued hyperparameters.

◆ numOutputs()

unsigned int CovarianceFunctionBase::numOutputs ( ) const
inline

Return the number of outputs assumed for this covariance function.

Definition at line 85 of file CovarianceFunctionBase.h.

Referenced by GaussianProcessSurrogate::evaluate(), GaussianProcessTrainer::GaussianProcessTrainer(), and StochasticTools::GaussianProcess::linkCovarianceFunction().

85 { return _num_outputs; }
const unsigned int _num_outputs
The number of outputs this covariance function is used to describe.

◆ validParams()

InputParameters CovarianceFunctionBase::validParams ( )
static

Definition at line 13 of file CovarianceFunctionBase.C.

Referenced by ExponentialCovariance::validParams(), SquaredExponentialCovariance::validParams(), MaternHalfIntCovariance::validParams(), and LMC::validParams().

14 {
16  params.addParam<std::vector<UserObjectName>>(
17  "covariance_functions", {}, "Covariance functions that this covariance function depends on.");
18  params.addParam<unsigned int>(
19  "num_outputs", 1, "The number of outputs expected for this covariance function.");
20  params.addClassDescription("Base class for covariance functions");
21  params.registerBase("CovarianceFunctionBase");
22  params.registerSystemAttributeName("CovarianceFunction");
23  return params;
24 }
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
void registerSystemAttributeName(const std::string &value)
void registerBase(const std::string &value)
void addClassDescription(const std::string &doc_string)
static InputParameters validParams()

Member Data Documentation

◆ _covariance_functions

std::vector<CovarianceFunctionBase *> CovarianceFunctionBase::_covariance_functions
protected

◆ _dependent_covariance_names

const std::vector<UserObjectName> CovarianceFunctionBase::_dependent_covariance_names
protected

The names of the dependent covariance functions.

Definition at line 125 of file CovarianceFunctionBase.h.

Referenced by CovarianceFunctionBase(), and dependentCovarianceNames().

◆ _dependent_covariance_types

std::vector<std::string> CovarianceFunctionBase::_dependent_covariance_types
protected

The types of the dependent covariance functions.

Definition at line 128 of file CovarianceFunctionBase.h.

Referenced by CovarianceFunctionBase().

◆ _hp_map_real

std::unordered_map<std::string, Real> CovarianceFunctionBase::_hp_map_real
protected

Map of real-valued hyperparameters.

Definition at line 113 of file CovarianceFunctionBase.h.

Referenced by addRealHyperParameter(), buildHyperParamMap(), getTuningData(), hyperParamMapReal(), isTunable(), and loadHyperParamMap().

◆ _hp_map_vector_real

std::unordered_map<std::string, std::vector<Real> > CovarianceFunctionBase::_hp_map_vector_real
protected

◆ _num_outputs

const unsigned int CovarianceFunctionBase::_num_outputs
protected

The number of outputs this covariance function is used to describe.

Definition at line 122 of file CovarianceFunctionBase.h.

Referenced by LMC::computeAGradient(), LMC::computeBMatrix(), LMC::computeCovarianceMatrix(), LMC::computedKdhyper(), LMC::LMC(), and numOutputs().

◆ _tunable_hp

std::unordered_set<std::string> CovarianceFunctionBase::_tunable_hp
protected

list of tunable hyper-parameters

Definition at line 119 of file CovarianceFunctionBase.h.

Referenced by addRealHyperParameter(), addVectorRealHyperParameter(), LMC::computedKdhyper(), and isTunable().


The documentation for this class was generated from the following files: