BayesOpt
bo_cont.cpp
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 #include <ctime>
24 #include "bayesopt/bayesopt.h" // For the C API
25 #include "bayesopt/bayesopt.hpp" // For the C++ API
26 #include <boost/numeric/ublas/assignment.hpp> // <<= op assigment
27 
28 
29 /* Function to be used for C-API testing */
30 double testFunction(unsigned int n, const double *x,
31  double *gradient, /* NULL if not needed */
32  void *func_data)
33 {
34  double f = 10.;
35  for (unsigned int i = 0; i < n; ++i)
36  {
37  f += (x[i] - .53) * (x[i] - .53);
38  }
39  return f;
40 }
41 
42 /* Class to be used for C++-API testing */
44 {
45  public:
46 
47  ExampleQuadratic(size_t dim,bayesopt::Parameters param):
48  ContinuousModel(dim,param) {}
49 
50  double evaluateSample( const vectord &Xi )
51  {
52  double x[100];
53  for (size_t i = 0; i < Xi.size(); ++i)
54  {
55  x[i] = Xi(i);
56  }
57  return testFunction(Xi.size(),x,NULL,NULL);
58  };
59 
60 
61  bool checkReachability( const vectord &query )
62  { return true; };
63 
64 };
65 
66 
67 int main(int nargs, char *args[])
68 {
69  int n = 10; // Number of dimensions
70  clock_t start, end;
71  double diff,diff2;
72 
73  // Common configuration
74  // See parameters.h for the available options.
75  // Some parameters did not need to be changed for default, but we have done it for
76  // illustrative purpose.
77  bayesopt::Parameters par = initialize_parameters_to_default();
78 
79  par.kernel.name = "kSum(kSEISO,kConst)";
80  par.kernel.hp_mean <<= 1.0, 1.0;
81  par.kernel.hp_std <<= 1.0, 1.0;
82 
83  par.mean.name = "mConst";
84  par.mean.coef_mean <<= 1.0;
85  par.mean.coef_std <<= 1.0;
86 
87 
88  par.surr_name = "sStudentTProcessJef";
89  par.noise = 1e-10;
90 
91  par.sc_type = SC_MAP;
92  par.l_type = L_EMPIRICAL;
93 
94  par.n_iterations = 100; // Number of iterations
95  par.random_seed = 0;
96  par.n_init_samples = 15;
97  par.n_iter_relearn = 0;
98 
99  /*******************************************/
100  std::cout << "Running C++ interface" << std::endl;
101 
102  ExampleQuadratic opt(n,par);
103  vectord result(n);
104 
105  // Run C++ interface
106  start = clock();
107  opt.optimize(result);
108  end = clock();
109  diff = (double)(end-start) / (double)CLOCKS_PER_SEC;
110 
111  /*******************************************/
112  std::cout << "Running C inferface" << std::endl;
113 
114  // Prepare C interface
115  double low[128], up[128], xmin[128], fmin[128];
116 
117  // Lower and upper bounds
118  for (int i = 0; i < n; ++i)
119  {
120  low[i] = 0.;
121  up[i] = 1.;
122  }
123 
124  // Run C interface
125  start = clock();
126  bayes_optimization(n,&testFunction,NULL,low,up,xmin,fmin,par.generate_bopt_params());
127  end = clock();
128  diff2 = (double)(end-start) / (double)CLOCKS_PER_SEC;
129  /*******************************************/
130 
131 
132  // Results
133  std::cout << "Final result C++: " << result << std::endl;
134  std::cout << "Elapsed time in C++: " << diff << " seconds" << std::endl;
135 
136  std::cout << "Final result C: [" << n <<"](" << xmin[0];
137  for (int i = 1; i < n; ++i )
138  {
139  std::cout << "," << xmin[i];
140  }
141  std::cout << ")" << std::endl;
142  std::cout << "Elapsed time in C: " << diff2 << " seconds" << std::endl;
143 
144 }
145 
KernelParameters kernel
Kernel parameters.
Definition: parameters.hpp:107
std::string name
Name of the mean function.
Definition: parameters.hpp:53
BAYESOPT_API int bayes_optimization(int nDim, eval_func f, void *f_data, const double *lb, const double *ub, double *x, double *minf, bopt_params parameters)
C wrapper for the Bayesian optimization algorithm.
Definition: bayesoptwpr.cpp:98
vectord coef_std
Basis function coefficients (std)
Definition: parameters.hpp:55
MeanParameters mean
Mean (parametric function) parameters.
Definition: parameters.hpp:108
vectord hp_mean
Kernel hyperparameters prior (mean, log space)
Definition: parameters.hpp:43
learning_type l_type
Type of learning for the kernel params.
Definition: parameters.hpp:96
Bayesian optimization for functions in continuous input spaces.
Definition: bayesopt.hpp:78
ContinuousModel()
Default constructor forbidden.
vectord coef_mean
Basis function coefficients (mean)
Definition: parameters.hpp:54
size_t n_init_samples
Number of samples before optimization.
Definition: parameters.hpp:68
BayesOpt main C++ interface.
BayesOpt wrapper for C interface.
size_t n_iterations
Maximum BayesOpt evaluations (budget)
Definition: parameters.hpp:66
void optimize(vectord &bestPoint)
Execute the optimization process of the function defined in evaluateSample.
double evaluateSample(const vectord &Xi)
Function that defines the actual function to be optimized.
Definition: bo_cont.cpp:50
vectord hp_std
Kernel hyperparameters prior (st dev, log space)
Definition: parameters.hpp:44
std::string surr_name
Name of the surrogate function.
Definition: parameters.hpp:85
double noise
Variance of observation noise (and nugget)
Definition: parameters.hpp:88
std::string name
Name of the kernel function.
Definition: parameters.hpp:42
score_type sc_type
Score type for kernel hyperparameters (ML,MAP,etc)
Definition: parameters.hpp:95
int random_seed
>=0 -> Fixed seed, <0 -> Time based (variable).
Definition: parameters.hpp:74
bool checkReachability(const vectord &query)
This function checks if the query is valid or not.
Definition: bo_cont.cpp:61
size_t n_iter_relearn
Number of samples before relearn kernel.
Definition: parameters.hpp:69