BayesOpt
mean_atomic.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_ATOMIC_HPP_
25 #define _MEAN_ATOMIC_HPP_
26 
27 #include <boost/numeric/ublas/vector_proxy.hpp>
28 #include "mean_functors.hpp"
29 
30 namespace bayesopt
31 {
32 
39  {
40  public:
41  virtual int init(size_t input_dim)
42  {
43  n_inputs = input_dim;
44  return 0;
45  };
46  void setParameters(const vectord &theta)
47  {
48  if(theta.size() != n_params)
49  {
50  throw std::invalid_argument("Wrong number of mean function parameters");
51  }
52 
53  mParameters = theta;
54  };
55  vectord getParameters() {return mParameters;};
56  size_t nParameters() {return n_params;};
57  size_t nFeatures() {return n_features;};
58 
59  virtual ~AtomicFunction(){};
60 
61  protected:
62  size_t n_params;
63  size_t n_features;
64  vectord mParameters;
65  };
66 
67 
70  {
71  public:
72  int init(size_t input_dim)
73  {
74  n_inputs = input_dim;
75  n_params = 1;
76  n_features = 1;
77  return 0;
78  };
79  double getMean (const vectord& x) { return 0.0; };
80  vectord getFeatures(const vectord& x) { return zvectord(1); };
81  };
82 
85  {
86  public:
87  int init(size_t input_dim)
88  {
89  n_inputs = input_dim;
90  n_params = 1;
91  n_features = 1;
92  return 0;
93  };
94  double getMean (const vectord& x) { return 1.0; };
95  vectord getFeatures(const vectord& x) { return svectord(1,1.0); };
96  };
97 
98 
102  {
103  public:
104  int init(size_t input_dim)
105  {
106  n_inputs = input_dim;
107  n_params = 1;
108  n_features = 1;
109  return 0;
110  };
111  double getMean (const vectord& x) { return mParameters(0); };
112  vectord getFeatures(const vectord& x) { return svectord(1,1.0); };
113  };
114 
115 
119  {
120  public:
121  int init(size_t input_dim)
122  {
123  n_inputs = input_dim;
124  n_params = input_dim;
125  n_features = input_dim;
126  return 0;
127  };
128  double getMean (const vectord& x)
129  { return boost::numeric::ublas::inner_prod(x,mParameters); };
130  vectord getFeatures(const vectord& x) { return x; };
131  };
132 
133 
137  {
138  public:
139  int init(size_t input_dim)
140  {
141  n_inputs = input_dim;
142  n_params = input_dim + 1;
143  n_features = input_dim + 1;
144  return 0;
145  };
146  void setParameters(const vectord& params)
147  {
148  if(params.size() != n_params)
149  {
150  throw std::invalid_argument("Wrong number of mean function parameters");
151  }
152 
153  mConstParam = params(0);
154  mParameters = boost::numeric::ublas::project(params,
155  boost::numeric::ublas::range(1, params.size()));
156  };
157 
158  double getMean (const vectord& x)
159  { return boost::numeric::ublas::inner_prod(x,mParameters) + mConstParam; };
160 
161  vectord getFeatures(const vectord& x)
162  {
163  using boost::numeric::ublas::range;
164  using boost::numeric::ublas::project;
165  vectord res(x.size()+1);
166  res(0) = 1;
167  project(res,range(1,res.size())) = x;
168  return res;
169  };
170 
171  protected:
172  double mConstParam;
173  };
174 
176 
177 } //namespace bayesopt
178 
179 #endif
Abstract class for an atomic kernel.
Definition: mean_atomic.hpp:38
Constant function.
Namespace of the library interface.
Definition: using.dox:1
Constant zero function.
Definition: mean_atomic.hpp:69
Interface for mean functors.
Constant one function.
Definition: mean_atomic.hpp:84
Mean (parametric) functions.
Linear combination function.
Linear combination plus constant function.