BayesOpt
testfunctions.hpp
1 /*
2 -------------------------------------------------------------------------
3  This file is part of BayesOpt, an efficient C++ library for
4  Bayesian optimization.
5 
6  Copyright (C) 2011-2015 Ruben Martinez-Cantin <rmcantin@unizar.es>
7 
8  BayesOpt is free software: you can redistribute it and/or modify it
9  under the terms of the GNU Affero General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  BayesOpt is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU Affero General Public License for more details.
17 
18  You should have received a copy of the GNU Affero General Public License
19  along with BayesOpt. If not, see <http://www.gnu.org/licenses/>.
20 ------------------------------------------------------------------------
21 */
22 
23 #ifndef __TEST_FUNCTIONS_HPP__
24 #define __TEST_FUNCTIONS_HPP__
25 
26 #define _USE_MATH_DEFINES
27 #include <cmath>
28 #include <algorithm>
29 #include <boost/math/constants/constants.hpp>
30 #include <boost/numeric/ublas/assignment.hpp>
31 #include "bayesopt/bayesopt.hpp"
32 #include "specialtypes.hpp"
33 
34 
35 
37 {
38 public:
40  ContinuousModel(1,par) {}
41 
42  double evaluateSample(const vectord& xin)
43  {
44  if (xin.size() != 1)
45  {
46  std::cout << "WARNING: This only works for 1D inputs." << std::endl
47  << "WARNING: Using only first component." << std::endl;
48  }
49 
50  double x = xin(0);
51  return (x-0.3)*(x-0.3) + sin(20*x)*0.2;
52  };
53 
54  bool checkReachability(const vectord &query)
55  {return true;};
56 
57  void printOptimal()
58  {
59  std::cout << "Optimal:" << 0.23719 << std::endl;
60  }
61 
62 };
63 
64 
66 {
67 public:
69  ContinuousModel(2,par) {}
70 
71  double evaluateSample( const vectord& xin)
72  {
73  if (xin.size() != 2)
74  {
75  std::cout << "WARNING: This only works for 2D inputs." << std::endl
76  << "WARNING: Using only first two components." << std::endl;
77  }
78 
79  double x = xin(0) * 15 - 5;
80  double y = xin(1) * 15;
81 
82  return branin(x,y);
83  }
84 
85  double branin(double x, double y)
86  {
87  const double pi = boost::math::constants::pi<double>();
88  const double rpi = pi*pi;
89  return sqr(y-(5.1/(4*rpi))*sqr(x)
90  +5*x/pi-6)+10*(1-1/(8*pi))*cos(x)+10;
91  };
92 
93  bool checkReachability(const vectord &query)
94  {return true;};
95 
96  inline double sqr( double x ){ return x*x; };
97 
98  void printOptimal()
99  {
100  vectord sv(2);
101  sv(0) = 0.1238938; sv(1) = 0.818333;
102  std::cout << "Solutions: " << sv << "->"
103  << evaluateSample(sv) << std::endl;
104  sv(0) = 0.5427728; sv(1) = 0.151667;
105  std::cout << "Solutions: " << sv << "->"
106  << evaluateSample(sv) << std::endl;
107  sv(0) = 0.961652; sv(1) = 0.1650;
108  std::cout << "Solutions: " << sv << "->"
109  << evaluateSample(sv) << std::endl;
110  }
111 
112 };
113 
114 
116 {
117 public:
119  ContinuousModel(2,par) {}
120 
121  double evaluateSample( const vectord& x)
122  {
123  if (x.size() != 2)
124  {
125  std::cout << "WARNING: This only works for 2D inputs." << std::endl
126  << "WARNING: Using only first two components." << std::endl;
127  }
128  double x1_2 = x(0)*x(0);
129  double x2_2 = x(1)*x(1);
130 
131  double tmp1 = (4 - 2.1 * x1_2 + (x1_2*x1_2)/3) * x1_2;
132  double tmp2 = x(0)*x(1);
133  double tmp3 = (-4 + 4 * x2_2) * x2_2;
134  return tmp1 + tmp2 + tmp3;
135  }
136 
137  bool checkReachability(const vectord &query)
138  {return true;};
139 
140  inline double sqr( double x ){ return x*x; };
141 
142  void printOptimal()
143  {
144  vectord sv(2);
145  sv(0) = 0.0898; sv(1) = -0.7126;
146  std::cout << "Solutions: " << sv << "->"
147  << evaluateSample(sv) << std::endl;
148  sv(0) = -0.0898; sv(1) = 0.7126;
149  std::cout << "Solutions: " << sv << "->"
150  << evaluateSample(sv) << std::endl;
151  }
152 
153 };
154 
155 
156 
158 {
159 public:
161  ContinuousModel(6,par), mA(4,6), mC(4), mP(4,6)
162  {
163  mA <<= 10.0, 3.0, 17.0, 3.5, 1.7, 8.0,
164  0.05, 10.0, 17.0, 0.1, 8.0, 14.0,
165  3.0, 3.5, 1.7, 10.0, 17.0, 8.0,
166  17.0, 8.0, 0.05, 10.0, 0.1, 14.0;
167 
168  mC <<= 1.0, 1.2, 3.0, 3.2;
169 
170  mP <<= 0.1312, 0.1696, 0.5569, 0.0124, 0.8283, 0.5886,
171  0.2329, 0.4135, 0.8307, 0.3736, 0.1004, 0.9991,
172  0.2348, 0.1451, 0.3522, 0.2883, 0.3047, 0.6650,
173  0.4047, 0.8828, 0.8732, 0.5743, 0.1091, 0.0381;
174  }
175 
176  double evaluateSample( const vectord& xin)
177  {
178  double y = 0.0;
179  for(size_t i=0; i<4; ++i)
180  {
181  double sum = 0.0;
182  for(size_t j=0;j<6; ++j)
183  {
184  double val = xin(j)-mP(i,j);
185  sum -= mA(i,j)*val*val;
186  }
187  y -= mC(i)*std::exp(sum);
188  }
189  return y;
190  };
191 
192  bool checkReachability(const vectord &query)
193  {return true;};
194 
195  inline double sqr( double x ){ return x*x; };
196 
197  void printOptimal()
198  {
199  vectord sv(6);
200  sv <<= 0.20169, 0.150011, 0.476874, 0.275332, 0.311652, 0.6573;
201 
202  std::cout << "Solution: " << sv << "->"
203  << evaluateSample(sv) << std::endl;
204  }
205 
206 private:
207  matrixd mA;
208  vectord mC;
209  matrixd mP;
210 };
211 
212 
213 #endif
double evaluateSample(const vectord &x)
Function that defines the actual function to be optimized.
Boost vector and matrix types.
bool checkReachability(const vectord &query)
This function checks if the query is valid or not.
Bayesian optimization for functions in continuous input spaces.
Definition: bayesopt.hpp:78
ContinuousModel()
Default constructor forbidden.
bool checkReachability(const vectord &query)
This function checks if the query is valid or not.
BayesOpt main C++ interface.
bool checkReachability(const vectord &query)
This function checks if the query is valid or not.
bool checkReachability(const vectord &query)
This function checks if the query is valid or not.
double evaluateSample(const vectord &xin)
Function that defines the actual function to be optimized.
double evaluateSample(const vectord &xin)
Function that defines the actual function to be optimized.
double evaluateSample(const vectord &xin)
Function that defines the actual function to be optimized.