BayesOpt
kernel_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 _KERNEL_COMBINED_HPP_
26 #define _KERNEL_COMBINED_HPP_
27 
28 #include <boost/numeric/ublas/vector_proxy.hpp>
29 #include "kernel_functors.hpp"
30 
31 namespace bayesopt
32 {
33 
36 
40  class CombinedKernel : public Kernel
41  {
42  public:
43  virtual void init(size_t input_dim, Kernel* left, Kernel* right)
44  {
45  n_inputs = input_dim;
46  this->left = left;
47  this->right = right;
48  };
49 
50  void setHyperParameters(const vectord &theta)
51  {
52  using boost::numeric::ublas::subrange;
53 
54  size_t n_lhs = left->nHyperParameters();
55  size_t n_rhs = right->nHyperParameters();
56  if (theta.size() != n_lhs + n_rhs)
57  {
58  FILE_LOG(logERROR) << "Wrong number of kernel hyperparameters";
59  throw std::invalid_argument("Wrong number of kernel hyperparameters");
60  }
61  left->setHyperParameters(subrange(theta,0,n_lhs));
62  right->setHyperParameters(subrange(theta,n_lhs,n_lhs+n_rhs));
63  };
64 
65  vectord getHyperParameters()
66  {
67  using boost::numeric::ublas::subrange;
68 
69  size_t n_lhs = left->nHyperParameters();
70  size_t n_rhs = right->nHyperParameters();
71  vectord par(n_lhs + n_rhs);
72  subrange(par,0,n_lhs) = left->getHyperParameters();
73  subrange(par,n_lhs,n_lhs+n_rhs) = right->getHyperParameters();
74  return par;
75  };
76 
77  size_t nHyperParameters()
78  {
79  size_t n_lhs = left->nHyperParameters();
80  size_t n_rhs = right->nHyperParameters();
81  return n_lhs + n_rhs;
82  };
83 
84  virtual ~CombinedKernel()
85  {
86  delete left;
87  delete right;
88  };
89 
90  protected:
91  Kernel* left;
92  Kernel* right;
93  };
94 
96 
97 } //namespace bayesopt
98 
99 #endif
Kernel (covariance) functions.
Namespace of the library interface.
Definition: using.dox:1
Abstract class for combined kernel.
Interface for kernel functors.