BayesOpt
bayesoptwpr.cpp
1 /*
2 -----------------------------------------------------------------------------
3  This file is part of BayesOptimization, an efficient C++ library for
4  Bayesian optimization.
5 
6  Copyright (C) 2011-2015 Ruben Martinez-Cantin <rmcantin@unizar.es>
7 
8  BayesOptimization is free software: you can redistribute it and/or modify
9  it 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  BayesOptimization is distributed in the hope that it will be useful,
14  but 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 BayesOptimization. If not, see <http://www.gnu.org/licenses/>.
20 -----------------------------------------------------------------------------
21 */
22 
23 #include "bayesopt/bayesopt.h"
24 #include "bayesopt/bayesopt.hpp"
25 
26 #include "log.hpp"
27 #include "ublas_extra.hpp"
28 #include "specialtypes.hpp"
29 
30 static const int BAYESOPT_FAILURE = -1; /* generic failure code */
31 static const int BAYESOPT_INVALID_ARGS = -2;
32 static const int BAYESOPT_OUT_OF_MEMORY = -3;
33 static const int BAYESOPT_RUNTIME_ERROR = -4;
34 
39 {
40  public:
41 
42  CContinuousModel(size_t dim, bopt_params params):
43  ContinuousModel(dim,params) {};
44 
45  virtual ~CContinuousModel(){};
46 
47  double evaluateSample( const vectord &Xi )
48  {
49  int n = static_cast<int>(Xi.size());
50  return mF(n,&Xi[0],NULL,mOtherData);
51  };
52 
53  void set_eval_funct(eval_func f)
54  { mF = f; }
55 
56 
57  void save_other_data(void* other_data)
58  { mOtherData = other_data; }
59 
60 protected:
61  void* mOtherData;
62  eval_func mF;
63 };
64 
69 {
70  public:
71 
72  CDiscreteModel(const vecOfvec &validX, bopt_params params):
73  DiscreteModel(validX, params)
74  {};
75 
76  CDiscreteModel(const vectori &categories, bopt_params params):
77  DiscreteModel(categories, params)
78  {};
79 
80  double evaluateSample( const vectord &Xi )
81  {
82  int n = static_cast<int>(Xi.size());
83  return mF(n,&Xi[0],NULL,mOtherData);
84  };
85 
86  void set_eval_funct(eval_func f)
87  { mF = f; }
88 
89 
90  void save_other_data(void* other_data)
91  { mOtherData = other_data; }
92 
93 protected:
94  void* mOtherData;
95  eval_func mF;
96 };
97 
98 int bayes_optimization(int nDim, eval_func f, void* f_data,
99  const double *lb, const double *ub,
100  double *x, double *minf, bopt_params parameters)
101 {
102  vectord result(nDim);
103 
104  vectord lowerBound = bayesopt::utils::array2vector(lb,nDim);
105  vectord upperBound = bayesopt::utils::array2vector(ub,nDim);
106 
107  try
108  {
109  CContinuousModel optimizer(nDim, parameters);
110 
111  optimizer.set_eval_funct(f);
112  optimizer.save_other_data(f_data);
113  optimizer.setBoundingBox(lowerBound,upperBound);
114 
115  optimizer.optimize(result);
116  std::copy(result.begin(), result.end(), x);
117 
118  *minf = optimizer.getValueAtMinimum();
119  }
120  catch (std::bad_alloc& e)
121  {
122  FILE_LOG(logERROR) << e.what();
123  return BAYESOPT_OUT_OF_MEMORY;
124  }
125  catch (std::invalid_argument& e)
126  {
127  FILE_LOG(logERROR) << e.what();
128  return BAYESOPT_INVALID_ARGS;
129  }
130  catch (std::runtime_error& e)
131  {
132  FILE_LOG(logERROR) << e.what();
133  return BAYESOPT_RUNTIME_ERROR;
134  }
135  catch (...)
136  {
137  FILE_LOG(logERROR) << "Unknown error";
138  return BAYESOPT_FAILURE;
139  }
140  return 0; /* everything ok*/
141 };
142 
143 int bayes_optimization_disc(int nDim, eval_func f, void* f_data,
144  double *valid_x, size_t n_points,
145  double *x, double *minf, bopt_params parameters)
146 {
147  vectord result(nDim);
148  vectord input(nDim);
149  vecOfvec xSet;
150 
151  for(size_t i = 0; i<n_points;++i)
152  {
153  for(int j = 0; j<nDim; ++j)
154  {
155  input(j) = valid_x[i*nDim+j];
156  }
157  xSet.push_back(input);
158  }
159 
160  if(parameters.n_init_samples > n_points)
161  {
162  parameters.n_init_samples = n_points;
163  parameters.n_iterations = 0;
164  }
165 
166  try
167  {
168  CDiscreteModel optimizer(xSet,parameters);
169 
170  optimizer.set_eval_funct(f);
171  optimizer.save_other_data(f_data);
172  optimizer.optimize(result);
173 
174  std::copy(result.begin(), result.end(), x);
175 
176  *minf = optimizer.getValueAtMinimum();
177  }
178  catch (std::bad_alloc& e)
179  {
180  FILE_LOG(logERROR) << e.what();
181  return BAYESOPT_OUT_OF_MEMORY;
182  }
183  catch (std::invalid_argument& e)
184  {
185  FILE_LOG(logERROR) << e.what();
186  return BAYESOPT_INVALID_ARGS;
187  }
188  catch (std::runtime_error& e)
189  {
190  FILE_LOG(logERROR) << e.what();
191  return BAYESOPT_RUNTIME_ERROR;
192  }
193  catch (...)
194  {
195  FILE_LOG(logERROR) << "Unknown error";
196  return BAYESOPT_FAILURE;
197  }
198 
199  return 0; /* everything ok*/
200 }
201 
202 
203 int bayes_optimization_categorical(int nDim, eval_func f, void* f_data,
204  int *categories, double *x,
205  double *minf, bopt_params parameters)
206 {
207  vectord result(nDim);
208  vectori cat(nDim);
209 
210  std::copy(categories,categories+nDim,cat.begin());
211 
212  try
213  {
214  CDiscreteModel optimizer(cat,parameters);
215 
216  optimizer.set_eval_funct(f);
217  optimizer.save_other_data(f_data);
218  optimizer.optimize(result);
219 
220  std::copy(result.begin(), result.end(), x);
221 
222  *minf = optimizer.getValueAtMinimum();
223  }
224  catch (std::bad_alloc& e)
225  {
226  FILE_LOG(logERROR) << e.what();
227  return BAYESOPT_OUT_OF_MEMORY;
228  }
229  catch (std::invalid_argument& e)
230  {
231  FILE_LOG(logERROR) << e.what();
232  return BAYESOPT_INVALID_ARGS;
233  }
234  catch (std::runtime_error& e)
235  {
236  FILE_LOG(logERROR) << e.what();
237  return BAYESOPT_RUNTIME_ERROR;
238  }
239  catch (...)
240  {
241  FILE_LOG(logERROR) << "Unknown error";
242  return BAYESOPT_FAILURE;
243  }
244 
245  return 0; /* everything ok*/
246 }
Boost vector and matrix types.
size_t n_iterations
Maximum BayesOpt evaluations (budget)
Definition: parameters.h:84
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
double evaluateSample(const vectord &Xi)
Function that defines the actual function to be optimized.
Definition: bayesoptwpr.cpp:80
Version of ContinuousModel for the C wrapper.
Definition: bayesoptwpr.cpp:38
Bayesian optimization for functions in continuous input spaces.
Definition: bayesopt.hpp:78
ContinuousModel()
Default constructor forbidden.
double evaluateSample(const vectord &Xi)
Function that defines the actual function to be optimized.
Definition: bayesoptwpr.cpp:47
int bayes_optimization_disc(int nDim, eval_func f, void *f_data, double *valid_x, size_t n_points, double *x, double *minf, bopt_params parameters)
C wrapper for the Bayesian optimization algorithm.
BayesOpt main C++ interface.
BayesOpt wrapper for C interface.
size_t n_init_samples
Number of samples before optimization.
Definition: parameters.h:86
void setBoundingBox(const vectord &lowerBound, const vectord &upperBound)
Sets the bounding box.
void optimize(vectord &bestPoint)
Execute the optimization process of the function defined in evaluateSample.
Configuration parameters.
Definition: parameters.h:83
Extra functions for Ublas library.
Version of DiscreteModel for the C wrapper.
Definition: bayesoptwpr.cpp:68
Bayesian optimization for functions in discrete spaces.
Definition: bayesopt.hpp:166
int bayes_optimization_categorical(int nDim, eval_func f, void *f_data, int *categories, double *x, double *minf, bopt_params parameters)
C wrapper for the Bayesian optimization algorithm.
Modules and helper macros for logging.