BayesOpt
posterior_mcmc.cpp
1 /*
2 -------------------------------------------------------------------------
3  This file is part of BayesOpt, an efficient C++ library for
4  Bayesian optimization.
5 
6  Copyright (C) 2011-2015 Ruben Martinez-Cantin <rmcantin@unizar.es>
7 
8  BayesOpt is free software: you can redistribute it and/or modify it
9  under the terms of the GNU Affero General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  BayesOpt is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU Affero General Public License for more details.
17 
18  You should have received a copy of the GNU Affero General Public License
19  along with BayesOpt. If not, see <http://www.gnu.org/licenses/>.
20 ------------------------------------------------------------------------
21 */
22 #include "log.hpp"
23 #include "posterior_mcmc.hpp"
24 
25 namespace bayesopt
26 {
27  MCMCModel::MCMCModel(size_t dim, Parameters parameters,
28  randEngine& eng):
29  PosteriorModel(dim,parameters,eng), nParticles(10)
30  {
31  //TODO: Take nParticles from parameters
32 
33  // Configure Surrogate and Criteria Functions
34  setSurrogateModel(eng);
35  setCriteria(eng);
36 
37  // Seting MCMC for kernel hyperparameters...
38  // We use the first GP as the "walker" to get the particles. Then,
39  // we will use a whole vector of GPs to avoid recomputing the
40  // kernel matrices after every data point.
41  size_t nhp = mGP[0].nHyperParameters();
42  kSampler.reset(new MCMCSampler(&mGP[0],nhp,eng));
43 
44  kSampler->setNParticles(nParticles);
45  kSampler->setNBurnOut(100);
46  }
47 
48  MCMCModel::~MCMCModel()
49  { } // Default destructor
50 
51 
52  void MCMCModel::updateHyperParameters()
53  {
54  // We take the initial point as the last particle from previous update.
55  size_t last = mGP.size()-1;
56  vectord lastTheta = mGP[last].getHyperParameters();
57 
58  FILE_LOG(logDEBUG) << "Initial kernel parameters: " << lastTheta;
59  kSampler->run(lastTheta);
60  for(size_t i = 0; i<nParticles; ++i)
61  {
62  mGP[i].setHyperParameters(kSampler->getParticle(i));
63  }
64  FILE_LOG(logDEBUG) << "Final kernel parameters: " << lastTheta;
65  };
66 
67 
68  void MCMCModel::setSurrogateModel(randEngine& eng)
69  {
70  for(size_t i = 0; i<nParticles; ++i)
71  {
73  mData,mMean,eng));
74  }
75  } // setSurrogateModel
76 
77  void MCMCModel::setCriteria(randEngine& eng)
78  {
79  CriteriaFactory mCFactory;
80 
81  for(size_t i = 0; i<nParticles; ++i)
82  {
83  mCrit.push_back(mCFactory.create(mParameters.crit_name,&mGP[i]));
84  mCrit[i].setRandomEngine(eng);
85 
86  if (mCrit[i].nParameters() == mParameters.crit_params.size())
87  {
88  mCrit[i].setParameters(mParameters.crit_params);
89  }
90  else // If the number of parameters is different, use default.
91  {
92  if (mParameters.crit_params.size() != 0)
93  {
94  FILE_LOG(logERROR) << "Expected " << mCrit[i].nParameters()
95  << " parameters. Got "
96  << mParameters.crit_params.size() << " instead.";
97  }
98  FILE_LOG(logINFO) << "Using default parameters for criteria.";
99  }
100  }
101  } // setCriteria
102 
103 
104 
105 
106 }// namespace bayesopt
Posterior distribution on GPs based on MCMC over kernel parameters.
Factory model for criterion functions This factory is based on the libgp library by Manuel Blum https...
static NonParametricProcess * create(size_t dim, Parameters parameters, const Dataset &data, MeanModel &mean, randEngine &eng)
Factory model generator for surrogate models.
Namespace of the library interface.
Definition: using.dox:1
Parameters mParameters
Configuration parameters.
vectord crit_params
Criterion hyperparameters (if needed)
Definition: parameters.hpp:111
Criteria * create(std::string name, NonParametricProcess *proc)
Factory model for criterion functions This function is based on the libgp library by Manuel Blum http...
size_t mDims
Number of dimensions.
GPVect mGP
Pointer to surrogate model.
Markov Chain Monte Carlo sampler.
std::string crit_name
Name of the criterion.
Definition: parameters.hpp:110
Modules and helper macros for logging.
Dataset mData
Dataset (x-> inputs, y-> labels/output)
CritVect mCrit
Metacriteria model.
Bayesian optimization using different non-parametric processes as distributions over surrogate functi...