BayesOpt
bayesoptextras.h
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 __BAYESOPTEXTRAS_H__
26 #define __BAYESOPTEXTRAS_H__
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <math.h>
32 #include <mex.h>
33 
34 #include "bayesopt/bayesopt.h"
35 
36 /* Declaration */
37 
38 static void struct_value(const mxArray *s, const char *name, double *result);
39 static void struct_array(const mxArray *s, const char *name, size_t *n, double *result);
40 static void struct_size(const mxArray *s, const char *name, size_t *result);
41 static void struct_int(const mxArray *s, const char *name, int *result);
42 static void struct_string(const mxArray *s, const char *name, char* result);
43 
44 static double user_function(unsigned n, const double *x,
45  double *gradient, /* NULL if not needed */
46  void *d_);
47 
48 /* See parameters.h for the available options */
49 static bopt_params load_parameters(const mxArray* params);
50 
51 #define FLEN 128 /* max length of user function name */
52 #define MAXRHS 2 /* max nrhs for user function */
53 typedef struct {
54  char f[FLEN];
55  mxArray *plhs[2];
56  mxArray *prhs[MAXRHS];
57  int xrhs, nrhs;
58  int verbose, neval;
60 
61 /* Implementation */
62 
63 #define CHECK0(cond, msg) if (!(cond)) mexErrMsgTxt(msg);
64 
65 
66 void struct_value(const mxArray *s, const char *name, double *result)
67 {
68  mxArray *val = mxGetField(s, 0, name);
69  if (val)
70  {
71  if(!(mxIsNumeric(val) && !mxIsComplex(val)
72  && mxGetM(val) * mxGetN(val) == 1))
73  {
74  mexErrMsgTxt("param fields must be real scalars");
75  }
76  else
77  {
78  *result = mxGetScalar(val);
79  }
80  }
81  else
82  {
83  mexPrintf("Field %s not found. Default not modified.\n", name);
84  }
85  return;
86 }
87 
88 void struct_array(const mxArray *s, const char *name, size_t *n, double *result)
89 {
90  mxArray *val = mxGetField(s, 0, name);
91  if (val)
92  {
93  if(!(mxIsNumeric(val) && !mxIsComplex(val)))
94  {
95  mexErrMsgTxt("Param fields must be vector");
96  }
97  else
98  {
99  *n = mxGetM(val) * mxGetN(val);
100  memcpy(result, mxGetPr(val), *n * sizeof(double));
101  }
102  }
103  else
104  {
105  mexPrintf("Field %s not found. Default not modified.\n", name);
106  }
107  return;
108 }
109 
110 
111 void struct_size(const mxArray *s, const char *name, size_t *result)
112 {
113  mxArray *val = mxGetField(s, 0, name);
114  if (val)
115  {
116  if(!(mxIsNumeric(val) && !mxIsComplex(val)
117  && mxGetM(val) * mxGetN(val) == 1))
118  {
119  mexErrMsgTxt("param fields must be scalar");
120  }
121  else
122  {
123  *result = (size_t)(mxGetScalar(val));
124  }
125  }
126  else
127  {
128  mexPrintf("Field %s not found. Default not modified.\n", name);
129  }
130  return;
131 }
132 
133 void struct_int(const mxArray *s, const char *name, int *result)
134 {
135  mxArray *val = mxGetField(s, 0, name);
136  if (val)
137  {
138  if(!(mxIsNumeric(val) && !mxIsComplex(val)
139  && mxGetM(val) * mxGetN(val) == 1))
140  {
141  mexErrMsgTxt("param fields must be scalar");
142  }
143  else
144  {
145  *result = (int)(mxGetScalar(val));
146  }
147  }
148  else
149  {
150  mexPrintf("Field %s not found. Default not modified.\n", name);
151  }
152  return;
153 }
154 
155 
156 
157 void struct_string(const mxArray *s, const char *name, char* result)
158 {
159  mxArray *val = mxGetField(s, 0, name);
160 
161  if (val)
162  {
163  if( mxIsChar(val) )
164  {
165  /* Using MSVC 2010, data is lost unless we copy it. It seems
166  that mxGetField does not get the pointer and create a new
167  array or mxArrayToString fails to copy the string. */
168  strcpy(result,mxArrayToString(val));
169  }
170  else
171  {
172  mexErrMsgTxt("Method name must be a string");
173  }
174  }
175  else
176  {
177  mexPrintf("Field %s not found. Default not modified.\n", name);
178  }
179  return;
180 }
181 
182 
183 
184 double user_function(unsigned n, const double *x,
185  double *gradient, /* NULL if not needed */
186  void *d_)
187 {
189  double f;
190 
191 
192  d->plhs[0] = d->plhs[1] = NULL;
193 
194  memcpy(mxGetPr(d->prhs[d->xrhs]), x, n * sizeof(double));
195 
196  CHECK0(0 == mexCallMATLAB(gradient ? 2 : 1, d->plhs,
197  d->nrhs, d->prhs, d->f),
198  "error calling user function");
199 
200  CHECK0(mxIsNumeric(d->plhs[0]) && !mxIsComplex(d->plhs[0])
201  && (mxGetM(d->plhs[0]) * mxGetN(d->plhs[0]) == 1),
202  "user function must return real scalar");
203  f = mxGetScalar(d->plhs[0]);
204  mxDestroyArray(d->plhs[0]);
205 
206  if (gradient) {
207  CHECK0(mxIsDouble(d->plhs[1]) && !mxIsComplex(d->plhs[1])
208  && (mxGetM(d->plhs[1]) == 1 || mxGetN(d->plhs[1]) == 1)
209  && mxGetM(d->plhs[1]) * mxGetN(d->plhs[1]) == n,
210  "gradient vector from user function is the wrong size");
211  memcpy(gradient, mxGetPr(d->plhs[1]), n * sizeof(double));
212  mxDestroyArray(d->plhs[1]);
213  }
214  d->neval++;
215 
216  if (d->verbose) mexPrintf("Optimize eval #%d: %g\n", d->neval, f);
217  return f;
218 }
219 
220 bopt_params load_parameters(const mxArray* params)
221 {
222 
223  /* See parameters.h for the available options */
224 
225  char l_str[100], sc_str[100], name[100];
226  size_t n_hp_test, n_coef_test;
227 
228  bopt_params parameters = initialize_parameters_to_default();
229 
230  n_hp_test = parameters.kernel.n_hp;
231  n_coef_test = parameters.mean.n_coef;
232 
233  struct_size(params,"n_iterations", &parameters.n_iterations);
234  struct_size(params,"n_inner_iterations", &parameters.n_inner_iterations);
235  struct_size(params, "n_init_samples", &parameters.n_init_samples);
236  struct_size(params, "n_iter_relearn", &parameters.n_iter_relearn);
237 
238  struct_size(params, "init_method", &parameters.init_method);
239  struct_int(params, "random_seed", &parameters.random_seed);
240 
241  struct_int(params, "verbose_level", &parameters.verbose_level);
242  struct_string(params, "log_filename", parameters.log_filename);
243 
244  struct_size(params, "load_save_flag", &parameters.load_save_flag);
245  struct_string(params, "load_filename", parameters.load_filename);
246  struct_string(params, "save_filename", parameters.save_filename);
247 
248 
249  struct_string(params, "surr_name", parameters.surr_name);
250 
251  struct_value(params, "sigma_s", &parameters.sigma_s);
252  struct_value(params, "noise", &parameters.noise);
253  struct_value(params, "alpha", &parameters.alpha);
254  struct_value(params, "beta", &parameters.beta);
255 
256 
257  strcpy( l_str, learn2str(parameters.l_type));
258  struct_string(params, "l_type", l_str);
259  parameters.l_type = str2learn(l_str);
260 
261  strcpy( sc_str, score2str(parameters.sc_type));
262  struct_string(params, "sc_type", sc_str);
263  parameters.sc_type = str2score(sc_str);
264 
265 
266  struct_value(params, "epsilon", &parameters.epsilon);
267  struct_size(params, "force_jump", &parameters.force_jump);
268 
269  struct_string(params, "crit_name", parameters.crit_name);
270  struct_array(params, "crit_params", &parameters.n_crit_params,
271  &parameters.crit_params[0]);
272 
273  /* Kernel parameters */
274  struct_string(params, "kernel_name", parameters.kernel.name);
275  struct_array(params, "kernel_hp_mean", &parameters.kernel.n_hp,
276  &parameters.kernel.hp_mean[0]);
277  struct_array(params, "kernel_hp_std", &n_hp_test,
278  &parameters.kernel.hp_std[0]);
279 
280  CHECK0(parameters.kernel.n_hp == n_hp_test,
281  "Error processing kernel parameters");
282 
283  /* Mean function parameters */
284  struct_string(params, "mean_name", parameters.mean.name);
285  struct_array(params, "mean_coef_mean", &parameters.mean.n_coef,
286  &parameters.mean.coef_mean[0]);
287 
288  struct_array(params, "mean_coef_std", &n_coef_test,
289  &parameters.mean.coef_std[0]);
290 
291  CHECK0(parameters.mean.n_coef == n_coef_test,
292  "Error processing mean parameters");
293 
294 
295  return parameters;
296 }
297 
298 
299 #endif
size_t n_inner_iterations
Maximum inner optimizer evaluations.
Definition: parameters.h:85
double noise
Variance of observation noise (and nugget)
Definition: parameters.h:106
mean_parameters mean
Mean (parametric function) parameters.
Definition: parameters.h:126
size_t force_jump
If >0, and the difference between two consecutive observations is pure noise, for n consecutive steps...
Definition: parameters.h:118
double coef_std[128]
Basis function coefficients (std)
Definition: parameters.h:76
double coef_mean[128]
Basis function coefficients (mean)
Definition: parameters.h:75
learning_type l_type
Type of learning for the kernel params.
Definition: parameters.h:114
size_t n_iterations
Maximum BayesOpt evaluations (budget)
Definition: parameters.h:84
size_t n_hp
Number of kernel hyperparameters.
Definition: parameters.h:70
int verbose_level
Neg-Error,0-Warning,1-Info,2-Debug -> stdout 3-Error,4-Warning,5-Info,>5-Debug -> logfile...
Definition: parameters.h:94
score_type sc_type
Score type for kernel hyperparameters (ML,MAP,etc)
Definition: parameters.h:113
size_t load_save_flag
1-Load data,2-Save data, 3-Load and save data.
Definition: parameters.h:98
BayesOpt wrapper for C interface.
char * load_filename
Init data file path (if applicable)
Definition: parameters.h:100
size_t n_init_samples
Number of samples before optimization.
Definition: parameters.h:86
double hp_mean[128]
Kernel hyperparameters prior (mean, log space)
Definition: parameters.h:68
char * surr_name
Name of the surrogate function.
Definition: parameters.h:103
size_t n_coef
Number of mean funct.
Definition: parameters.h:77
double sigma_s
Signal variance (if known).
Definition: parameters.h:104
kernel_parameters kernel
Kernel parameters.
Definition: parameters.h:125
Configuration parameters.
Definition: parameters.h:83
size_t n_crit_params
Number of criterion hyperparameters.
Definition: parameters.h:130
char * name
Name of the kernel function.
Definition: parameters.h:67
double alpha
Inverse Gamma prior for signal var.
Definition: parameters.h:108
double hp_std[128]
Kernel hyperparameters prior (st dev, log space)
Definition: parameters.h:69
char * crit_name
Name of the criterion.
Definition: parameters.h:128
char * name
Name of the mean function.
Definition: parameters.h:74
char * log_filename
Log file path (if applicable)
Definition: parameters.h:96
double beta
Inverse Gamma prior for signal var.
Definition: parameters.h:110
size_t n_iter_relearn
Number of samples before relearn kernel.
Definition: parameters.h:87
int random_seed
>=0 -> Fixed seed, <0 -> Time based (variable).
Definition: parameters.h:92
double epsilon
For epsilon-greedy exploration.
Definition: parameters.h:117
size_t init_method
Sampling method for initial set 1-LHS, 2-Sobol (if available), other value-uniformly distributed...
Definition: parameters.h:91
char * save_filename
Sava data file path (if applicable)
Definition: parameters.h:101
double crit_params[128]
Criterion hyperparameters (if needed)
Definition: parameters.h:129