BayesOpt
mean_functors.hpp
Go to the documentation of this file.
1 
2 /*
3 -------------------------------------------------------------------------
4  This file is part of BayesOpt, an efficient C++ library for
5  Bayesian optimization.
6 
7  Copyright (C) 2011-2015 Ruben Martinez-Cantin <rmcantin@unizar.es>
8 
9  BayesOpt is free software: you can redistribute it and/or modify it
10  under the terms of the GNU Affero General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  BayesOpt is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Affero General Public License for more details.
18 
19  You should have received a copy of the GNU Affero General Public License
20  along with BayesOpt. If not, see <http://www.gnu.org/licenses/>.
21 ------------------------------------------------------------------------
22 */
23 
24 #ifndef _MEAN_FUNCTORS_HPP_
25 #define _MEAN_FUNCTORS_HPP_
26 
27 #include <map>
28 #include <boost/scoped_ptr.hpp>
29 #include <boost/numeric/ublas/matrix_proxy.hpp>
30 #include "bayesopt/parameters.hpp"
31 #include "specialtypes.hpp"
32 
33 namespace bayesopt
34 {
35 
40 
43  {
44  public:
45  virtual int init(size_t input_dim) {return 0;};
46  virtual int init(size_t input_dim, ParametricFunction* left,
47  ParametricFunction* right) {return 0;};
48 
49  virtual void setParameters(const vectord& params) = 0;
50  virtual vectord getParameters() = 0;
51  virtual size_t nParameters() = 0;
52 
53  virtual double getMean(const vectord& x) = 0;
54  virtual vectord operator()(const vecOfvec& x)
55  {
56  vectord result(x.size());
57  std::vector<vectord>::const_iterator x_it = x.begin();
58  std::vector<vectord>::const_iterator x_end = x.end();
59  vectord::iterator res_it = result.begin();
60  while(x_it != x_end)
61  {
62  *res_it++ = getMean(*x_it++);
63  }
64  return result;
65  };
66 
67 
68  virtual size_t nFeatures() = 0;
69  virtual vectord getFeatures(const vectord& x) = 0;
70  virtual matrixd getAllFeatures(const vecOfvec& x)
71  {
72  size_t nf = nFeatures();
73  matrixd result(nf,x.size());
74 
75  for(size_t ii=0; ii< x.size(); ++ii)
76  {
77  column(result,ii) = getFeatures(x[ii]);
78  }
79 
80  return result;
81  };
82 
83  virtual ~ParametricFunction(){};
84 
85  protected:
86  size_t n_inputs;
87  };
88 
89 
90  template <typename MeanType> ParametricFunction * create_func()
91  {
92  return new MeanType();
93  }
94 
95 
104  {
105  public:
106  MeanFactory ();
107  virtual ~MeanFactory () {};
108 
109  ParametricFunction* create(std::string name, size_t input_dim);
110 
111  private:
112  typedef ParametricFunction* (*create_func_definition)();
113  std::map<std::string , MeanFactory::create_func_definition> registry;
114  };
115 
116  class MeanModel
117  {
118  public:
119  MeanModel(size_t dim, Parameters parameters);
120  virtual ~MeanModel() {};
121 
122  ParametricFunction* getMeanFunc();
123 
124  void setParameters(const vectord &theta);
125  vectord getParameters();
126  size_t nParameters();
127 
128  vectord getFeatures(const vectord& x);
129  void getFeatures(const vectord& x, vectord& kx);
130  size_t nFeatures();
131 
132  void setPoints(const vecOfvec &x);
133  void addNewPoint(const vectord &x);
134 
135  vectord muTimesFeat();
136  double muTimesFeat(const vectord& x);
137 
147  void setMean (const vectord &muv, const vectord &smu,
148  std::string m_name, size_t dim);
149 
151  void setMean (MeanParameters mean, size_t dim);
152 
153  matrixd mFeatM;
154 
155  private:
156  vectord mMu;
157  vectord mS_Mu;
158 
159  boost::scoped_ptr<ParametricFunction> mMean;
160  };
161 
162 
163 
164  inline ParametricFunction* MeanModel::getMeanFunc()
165  { return mMean.get(); }
166 
167  inline void MeanModel::setParameters(const vectord &theta)
168  { mMean->setParameters(theta); };
169 
170  inline vectord MeanModel::getParameters(){return mMean->getParameters();};
171  inline size_t MeanModel::nParameters(){return mMean->nParameters();};
172 
173  inline vectord MeanModel::getFeatures(const vectord& x)
174  { return mMean->getFeatures(x); }
175 
176  inline void MeanModel::getFeatures(const vectord& x, vectord& kx)
177  { kx = mMean->getFeatures(x); }
178 
179  inline size_t MeanModel::nFeatures()
180  { return mMean->nFeatures(); }
181 
182  inline void MeanModel::setPoints(const vecOfvec &x)
183  { mFeatM = mMean->getAllFeatures(x); };
184 
185  inline void MeanModel::addNewPoint(const vectord &x)
186  {
187  using boost::numeric::ublas::column;
188 
189  mFeatM.resize(mFeatM.size1(),mFeatM.size2()+1);
190  column(mFeatM,mFeatM.size2()-1) = mMean->getFeatures(x);
191  }
192 
193  inline vectord MeanModel::muTimesFeat()
194  { return boost::numeric::ublas::prod(mMu,mFeatM); }
195 
196  inline double MeanModel::muTimesFeat(const vectord& x)
197  { return boost::numeric::ublas::inner_prod(mMu,mMean->getFeatures(x));}
198 
199 
200 
202 
203 } //namespace bayesopt
204 
205 
206 #endif
Boost vector and matrix types.
Namespace of the library interface.
Definition: using.dox:1
Factory model for parametric functions This factory is based on the libgp library by Manuel Blum http...
Interface for mean functors.
vectord mS_Mu
Variance of the params of the mean function W=mS_Mu*I.
boost::scoped_ptr< ParametricFunction > mMean
Pointer to mean function.
matrixd mFeatM
Value of the mean features at the input points.
Parameter definitions.
vectord mMu
Mean of the parameters of the mean function.