BayesOpt
mean_combined.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 #ifndef _MEAN_COMBINED_HPP_
26 #define _MEAN_COMBINED_HPP_
27 
28 #include <boost/numeric/ublas/vector_proxy.hpp>
29 #include "mean_functors.hpp"
30 
31 namespace bayesopt
32 {
33 
42  {
43  public:
44  virtual int init(size_t input_dim,
45  ParametricFunction* left,
46  ParametricFunction* right)
47  {
48  n_inputs = input_dim;
49  this->left = left;
50  this->right = right;
51  return 0;
52  };
53  void setParameters(const vectord &theta)
54  {
55  using boost::numeric::ublas::subrange;
56 
57  size_t n_lhs = left->nParameters();
58  size_t n_rhs = right->nParameters();
59  if (theta.size() != n_lhs + n_rhs)
60  {
61  throw std::invalid_argument("Wrong number of mean function parameters");
62  }
63 
64  left->setParameters(subrange(theta,0,n_lhs));
65  right->setParameters(subrange(theta,n_lhs,n_lhs+n_rhs));
66  };
67 
68  vectord getParameters()
69  {
70  using boost::numeric::ublas::subrange;
71 
72  size_t n_lhs = left->nParameters();
73  size_t n_rhs = right->nParameters();
74  vectord par(n_lhs + n_rhs);
75  subrange(par,0,n_lhs) = left->getParameters();
76  subrange(par,n_lhs,n_lhs+n_rhs) = right->getParameters();
77  return par;
78  };
79 
80  size_t nParameters()
81  {
82  size_t n_lhs = left->nParameters();
83  size_t n_rhs = right->nParameters();
84  return n_lhs + n_rhs;
85  };
86 
87  virtual ~CombinedFunction()
88  {
89  delete left;
90  delete right;
91  };
92 
93  protected:
94  ParametricFunction* left;
95  ParametricFunction* right;
96  };
97 
98 
101  {
102  public:
103  double getMean(const vectord &x)
104  {
105  return left->getMean(x) + right->getMean(x);
106  };
107 
108  vectord getFeatures(const vectord &x)
109  {
110  using boost::numeric::ublas::subrange;
111 
112  size_t n_lhf = left->nFeatures();
113  size_t n_rhf = right->nFeatures();
114  vectord feat(n_lhf + n_rhf);
115  subrange(feat,0,n_lhf) = left->getFeatures(x);
116  subrange(feat,n_lhf,n_lhf+n_rhf) = right->getFeatures(x);
117  return feat;
118  };
119 
120  size_t nFeatures()
121  {
122  size_t n_lhf = left->nFeatures();
123  size_t n_rhf = right->nFeatures();
124  return n_lhf + n_rhf;
125  };
126 
127  };
128 
130 
131 } //namespace bayesopt
132 
133 
134 
135 #endif
Namespace of the library interface.
Definition: using.dox:1
Interface for mean functors.
Sum of two kernels.
Abstract class for combined functions.
Mean (parametric) functions.