BayesOpt
posterior_mcmc.hpp
Go to the documentation of this file.
1 
3 /*
4 -------------------------------------------------------------------------
5  This file is part of BayesOpt, an efficient C++ library for
6  Bayesian optimization.
7 
8  Copyright (C) 2011-2015 Ruben Martinez-Cantin <rmcantin@unizar.es>
9 
10  BayesOpt is free software: you can redistribute it and/or modify it
11  under the terms of the GNU Affero General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  BayesOpt is distributed in the hope that it will be useful, but
16  WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Affero General Public License for more details.
19 
20  You should have received a copy of the GNU Affero General Public License
21  along with BayesOpt. If not, see <http://www.gnu.org/licenses/>.
22 ------------------------------------------------------------------------
23 */
24 
25 
26 #ifndef _POSTERIOR_MCMC_HPP_
27 #define _POSTERIOR_MCMC_HPP_
28 
29 #include <boost/ptr_container/ptr_vector.hpp>
30 #include "criteria_functors.hpp"
31 #include "posteriormodel.hpp"
32 #include "mcmc_sampler.hpp"
33 
34 namespace bayesopt {
35 
36 
47  class MCMCModel: public PosteriorModel
48  {
49  public:
50 
51  typedef boost::ptr_vector<NonParametricProcess> GPVect;
52  typedef boost::ptr_vector<Criteria> CritVect;
53 
61  MCMCModel(size_t dim, Parameters params, randEngine& eng);
62 
63  virtual ~MCMCModel();
64 
65  void updateHyperParameters();
66  void fitSurrogateModel();
67  void updateSurrogateModel();
68 
69  double evaluateCriteria(const vectord& query);
70  void updateCriteria(const vectord& query);
71 
72  bool criteriaRequiresComparison();
73  void setFirstCriterium();
74  bool setNextCriterium(const vectord& prevResult);
75  std::string getBestCriteria(vectord& best);
76 
77  ProbabilityDistribution* getPrediction(const vectord& query);
78 
79  private:
80  void setSurrogateModel(randEngine& eng);
81  void setCriteria(randEngine& eng);
82 
83  private: // Members
84  size_t nParticles;
85  GPVect mGP;
86  CritVect mCrit;
87 
88  boost::scoped_ptr<MCMCSampler> kSampler;
89 
90  private: //Forbidden
91  MCMCModel();
92  MCMCModel(MCMCModel& copy);
93  };
94 
97  inline void MCMCModel::fitSurrogateModel()
98  {
99  for(GPVect::iterator it=mGP.begin(); it != mGP.end(); ++it)
100  it->fitSurrogateModel();
101  };
102 
103  inline void MCMCModel::updateSurrogateModel()
104  {
105  for(GPVect::iterator it=mGP.begin(); it != mGP.end(); ++it)
106  it->updateSurrogateModel();
107  };
108 
109  inline double MCMCModel::evaluateCriteria(const vectord& query)
110  {
111  double sum = 0.0;
112  for(CritVect::iterator it=mCrit.begin(); it != mCrit.end(); ++it)
113  {
114  sum += it->evaluate(query);
115  }
116  return sum/static_cast<double>(nParticles);
117  };
118 
119  inline void MCMCModel::updateCriteria(const vectord& query)
120  {
121  for(CritVect::iterator it=mCrit.begin(); it != mCrit.end(); ++it)
122  {
123  it->update(query);
124  }
125  };
126 
127 
128  inline bool MCMCModel::criteriaRequiresComparison()
129  {return mCrit[0].requireComparison(); };
130 
131  inline void MCMCModel::setFirstCriterium()
132  {
133  for(CritVect::iterator it=mCrit.begin(); it != mCrit.end(); ++it)
134  {
135  it->initialCriteria();
136  }
137  };
138 
139  // Although we change the criteria for all MCMC particles, we use
140  // only the first element to compute de Hedge algorithm, because it
141  // should be based on the average result, thus being common for all
142  // the particles.
143  inline bool MCMCModel::setNextCriterium(const vectord& prevResult)
144  {
145  bool rotated;
146  mCrit[0].pushResult(prevResult);
147  for(CritVect::iterator it=mCrit.begin(); it != mCrit.end(); ++it)
148  {
149  rotated = it->rotateCriteria();
150  }
151  return rotated;
152  };
153 
154  inline std::string MCMCModel::getBestCriteria(vectord& best)
155  { return mCrit[0].getBestCriteria(best); };
156 
157  inline
158  ProbabilityDistribution* MCMCModel::getPrediction(const vectord& query)
159  { return mGP[0].prediction(query); };
160 
161 
162 
163 } //namespace bayesopt
164 
165 
166 #endif
Namespace of the library interface.
Definition: using.dox:1
GPVect mGP
Pointer to surrogate model.
Abstract interface for posterior model/criteria.
Markov Chain Monte Carlo algorithms.
Abstract and factory modules for criteria.
Posterior model of nonparametric processes/criteria based on MCMC samples.
CritVect mCrit
Metacriteria model.
Bayesian optimization using different non-parametric processes as distributions over surrogate functi...