25 #ifndef __BAYESOPTEXTRAS_H__ 26 #define __BAYESOPTEXTRAS_H__ 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);
44 static double user_function(
unsigned n,
const double *x,
49 static bopt_params load_parameters(
const mxArray* params);
56 mxArray *prhs[MAXRHS];
63 #define CHECK0(cond, msg) if (!(cond)) mexErrMsgTxt(msg); 66 void struct_value(
const mxArray *s,
const char *name,
double *result)
68 mxArray *val = mxGetField(s, 0, name);
71 if(!(mxIsNumeric(val) && !mxIsComplex(val)
72 && mxGetM(val) * mxGetN(val) == 1))
74 mexErrMsgTxt(
"param fields must be real scalars");
78 *result = mxGetScalar(val);
83 mexPrintf(
"Field %s not found. Default not modified.\n", name);
88 void struct_array(
const mxArray *s,
const char *name,
size_t *n,
double *result)
90 mxArray *val = mxGetField(s, 0, name);
93 if(!(mxIsNumeric(val) && !mxIsComplex(val)))
95 mexErrMsgTxt(
"Param fields must be vector");
99 *n = mxGetM(val) * mxGetN(val);
100 memcpy(result, mxGetPr(val), *n *
sizeof(
double));
105 mexPrintf(
"Field %s not found. Default not modified.\n", name);
111 void struct_size(
const mxArray *s,
const char *name,
size_t *result)
113 mxArray *val = mxGetField(s, 0, name);
116 if(!(mxIsNumeric(val) && !mxIsComplex(val)
117 && mxGetM(val) * mxGetN(val) == 1))
119 mexErrMsgTxt(
"param fields must be scalar");
123 *result = (size_t)(mxGetScalar(val));
128 mexPrintf(
"Field %s not found. Default not modified.\n", name);
133 void struct_int(
const mxArray *s,
const char *name,
int *result)
135 mxArray *val = mxGetField(s, 0, name);
138 if(!(mxIsNumeric(val) && !mxIsComplex(val)
139 && mxGetM(val) * mxGetN(val) == 1))
141 mexErrMsgTxt(
"param fields must be scalar");
145 *result = (int)(mxGetScalar(val));
150 mexPrintf(
"Field %s not found. Default not modified.\n", name);
157 void struct_string(
const mxArray *s,
const char *name,
char* result)
159 mxArray *val = mxGetField(s, 0, name);
168 strcpy(result,mxArrayToString(val));
172 mexErrMsgTxt(
"Method name must be a string");
177 mexPrintf(
"Field %s not found. Default not modified.\n", name);
184 double user_function(
unsigned n,
const double *x,
192 d->plhs[0] = d->plhs[1] = NULL;
194 memcpy(mxGetPr(d->prhs[d->xrhs]), x, n *
sizeof(
double));
196 CHECK0(0 == mexCallMATLAB(gradient ? 2 : 1, d->plhs,
197 d->nrhs, d->prhs, d->f),
198 "error calling user function");
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]);
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]);
216 if (d->verbose) mexPrintf(
"Optimize eval #%d: %g\n", d->neval, f);
225 char l_str[100], sc_str[100], name[100];
226 size_t n_hp_test, n_coef_test;
228 bopt_params parameters = initialize_parameters_to_default();
233 struct_size(params,
"n_iterations", ¶meters.
n_iterations);
235 struct_size(params,
"n_init_samples", ¶meters.
n_init_samples);
236 struct_size(params,
"n_iter_relearn", ¶meters.
n_iter_relearn);
238 struct_size(params,
"init_method", ¶meters.
init_method);
239 struct_int(params,
"random_seed", ¶meters.
random_seed);
241 struct_int(params,
"verbose_level", ¶meters.
verbose_level);
242 struct_string(params,
"log_filename", parameters.
log_filename);
244 struct_size(params,
"load_save_flag", ¶meters.
load_save_flag);
245 struct_string(params,
"load_filename", parameters.
load_filename);
246 struct_string(params,
"save_filename", parameters.
save_filename);
249 struct_string(params,
"surr_name", parameters.
surr_name);
251 struct_value(params,
"sigma_s", ¶meters.
sigma_s);
252 struct_value(params,
"noise", ¶meters.
noise);
253 struct_value(params,
"alpha", ¶meters.
alpha);
254 struct_value(params,
"beta", ¶meters.
beta);
257 strcpy( l_str, learn2str(parameters.
l_type));
258 struct_string(params,
"l_type", l_str);
259 parameters.
l_type = str2learn(l_str);
261 strcpy( sc_str, score2str(parameters.
sc_type));
262 struct_string(params,
"sc_type", sc_str);
263 parameters.
sc_type = str2score(sc_str);
266 struct_value(params,
"epsilon", ¶meters.
epsilon);
267 struct_size(params,
"force_jump", ¶meters.
force_jump);
269 struct_string(params,
"crit_name", parameters.
crit_name);
270 struct_array(params,
"crit_params", ¶meters.
n_crit_params,
274 struct_string(params,
"kernel_name", parameters.
kernel.
name);
275 struct_array(params,
"kernel_hp_mean", ¶meters.
kernel.
n_hp,
277 struct_array(params,
"kernel_hp_std", &n_hp_test,
281 "Error processing kernel parameters");
284 struct_string(params,
"mean_name", parameters.
mean.
name);
285 struct_array(params,
"mean_coef_mean", ¶meters.
mean.
n_coef,
288 struct_array(params,
"mean_coef_std", &n_coef_test,
292 "Error processing mean parameters");
size_t n_inner_iterations
Maximum inner optimizer evaluations.
double noise
Variance of observation noise (and nugget)
mean_parameters mean
Mean (parametric function) parameters.
size_t force_jump
If >0, and the difference between two consecutive observations is pure noise, for n consecutive steps...
double coef_std[128]
Basis function coefficients (std)
double coef_mean[128]
Basis function coefficients (mean)
learning_type l_type
Type of learning for the kernel params.
size_t n_iterations
Maximum BayesOpt evaluations (budget)
size_t n_hp
Number of kernel hyperparameters.
int verbose_level
Neg-Error,0-Warning,1-Info,2-Debug -> stdout 3-Error,4-Warning,5-Info,>5-Debug -> logfile...
score_type sc_type
Score type for kernel hyperparameters (ML,MAP,etc)
size_t load_save_flag
1-Load data,2-Save data, 3-Load and save data.
BayesOpt wrapper for C interface.
char * load_filename
Init data file path (if applicable)
size_t n_init_samples
Number of samples before optimization.
double hp_mean[128]
Kernel hyperparameters prior (mean, log space)
char * surr_name
Name of the surrogate function.
size_t n_coef
Number of mean funct.
double sigma_s
Signal variance (if known).
kernel_parameters kernel
Kernel parameters.
Configuration parameters.
size_t n_crit_params
Number of criterion hyperparameters.
char * name
Name of the kernel function.
double alpha
Inverse Gamma prior for signal var.
double hp_std[128]
Kernel hyperparameters prior (st dev, log space)
char * crit_name
Name of the criterion.
char * name
Name of the mean function.
char * log_filename
Log file path (if applicable)
double beta
Inverse Gamma prior for signal var.
size_t n_iter_relearn
Number of samples before relearn kernel.
int random_seed
>=0 -> Fixed seed, <0 -> Time based (variable).
double epsilon
For epsilon-greedy exploration.
size_t init_method
Sampling method for initial set 1-LHS, 2-Sobol (if available), other value-uniformly distributed...
char * save_filename
Sava data file path (if applicable)
double crit_params[128]
Criterion hyperparameters (if needed)