BayesOpt
parameters.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 #include <iostream>
23 #include "bayesopt/parameters.h" // c parameters structs
24 #include "bayesopt/parameters.hpp" // c++ parameters classes
25 #include "ublas_extra.hpp" // array2vector()
26 
27 /*-----------------------------------------------------------*/
28 /* Default parameters */
29 /*-----------------------------------------------------------*/
30 
31 /* Nonparametric process "parameters" */
32 const std::string KERNEL_NAME = "kMaternARD5";
33 const double KERNEL_THETA = 1.0;
34 const double KERNEL_SIGMA = 10.0;
35 const std::string MEAN_NAME = "mConst";
36 const double MEAN_MU = 1.0;
37 const double MEAN_SIGMA = 1000.0;
38 const double PRIOR_ALPHA = 1.0;
39 const double PRIOR_BETA = 1.0;
40 const double DEFAULT_SIGMA = 1.0;
41 const double DEFAULT_NOISE = 1e-6;
42 
43 /* Algorithm parameters */
44 const size_t DEFAULT_ITERATIONS = 190;
45 const size_t DEFAULT_INIT_SAMPLES = 10;
46 const size_t DEFAULT_ITERATIONS_RELEARN = 50;
47 const size_t DEFAULT_INNER_EVALUATIONS = 500;
49 /* Logging and Files */
50 const size_t DEFAULT_VERBOSE = 1;
51 const std::string LOG_FILENAME = "bayesopt.log";
52 const std::string SAVE_FILENAME = "bayesopt.dat";
53 const std::string LOAD_FILENAME = "bayesopt.dat";
54 
55 const std::string SURR_NAME = "sGaussianProcess";
56 const std::string CRIT_NAME = "cEI";
57 
58 learning_type str2learn(const char* name)
59 {
60  if (!strcmp(name,"L_FIXED") || !strcmp(name,"fixed")) return L_FIXED;
61  else if (!strcmp(name,"L_EMPIRICAL") || !strcmp(name,"empirical")) return L_EMPIRICAL;
62  else if (!strcmp(name,"L_DISCRETE") || !strcmp(name,"discrete")) return L_DISCRETE;
63  else if (!strcmp(name,"L_MCMC") || !strcmp(name,"mcmc")) return L_MCMC;
64  else return L_ERROR;
65 }
66 
67 const char* learn2str(learning_type name)
68 {
69  switch(name)
70  {
71  case L_FIXED: return "L_FIXED";
72  case L_EMPIRICAL: return "L_EMPIRICAL";
73  case L_DISCRETE: return "L_DISCRETE";
74  case L_MCMC: return "L_MCMC";
75  case L_ERROR:
76  default: return "ERROR!";
77  }
78 }
79 
80 score_type str2score(const char* name)
81 {
82  if (!strcmp(name,"SC_MTL") || !strcmp(name,"mtl")) return SC_MTL;
83  else if (!strcmp(name,"SC_ML") || !strcmp(name,"ml")) return SC_ML;
84  else if (!strcmp(name,"SC_MAP") || !strcmp(name,"map")) return SC_MAP;
85  else if (!strcmp(name,"SC_LOOCV") || !strcmp(name,"loocv")) return SC_LOOCV;
86  else return SC_ERROR;
87 }
88 
89 const char* score2str(score_type name)
90 {
91  switch(name)
92  {
93  case SC_MTL: return "SC_MTL";
94  case SC_ML: return "SC_ML";
95  case SC_MAP: return "SC_MAP";
96  case SC_LOOCV: return "SC_LOOCV";
97  case SC_ERROR:
98  default: return "ERROR!";
99  }
100 }
101 
102 void set_kernel(bopt_params* params, const char* name)
103 {
104  strcpy(params->kernel.name, name);
105 };
106 
107 void set_mean(bopt_params* params, const char* name)
108 {
109  strcpy(params->mean.name, name);
110 };
111 
112 void set_criteria(bopt_params* params, const char* name)
113 {
114  strcpy(params->crit_name, name);
115 };
116 
117 void set_surrogate(bopt_params* params, const char* name)
118 {
119  strcpy(params->surr_name, name);
120 };
121 
122 void set_log_file(bopt_params* params, const char* name)
123 {
124  strcpy(params->log_filename, name);
125 };
126 
127 void set_load_file(bopt_params* params, const char* name)
128 {
129  strcpy(params->load_filename, name);
130 };
131 
132 void set_save_file(bopt_params* params, const char* name)
133 {
134  strcpy(params->save_filename, name);
135 };
136 
137 void set_learning(bopt_params* params, const char* name)
138 {
139  params->l_type = str2learn(name);
140 };
141 
142 void set_score(bopt_params* params, const char* name)
143 {
144  params->sc_type = str2score(name);
145 };
146 
147 bopt_params initialize_parameters_to_default(void)
148 {
149  //bayesopt::Parameters par;
150  //return par.generate_bopt_params();
151  kernel_parameters kernel;
152  kernel.name = new char[128];
153  strcpy(kernel.name,KERNEL_NAME.c_str());
154 
155  kernel.hp_mean[0] = KERNEL_THETA;
156  kernel.hp_std[0] = KERNEL_SIGMA;
157  kernel.n_hp = 1;
158 
159  mean_parameters mean;
160  mean.name = new char[128];
161  strcpy(mean.name,MEAN_NAME.c_str());
162 
163  mean.coef_mean[0] = MEAN_MU;
164  mean.coef_std[0] = MEAN_SIGMA;
165  mean.n_coef = 1;
166 
167 
168  bopt_params params;
169 
170  params.n_iterations = DEFAULT_ITERATIONS;
171  params.n_inner_iterations = DEFAULT_INNER_EVALUATIONS;
172  params.n_init_samples = DEFAULT_INIT_SAMPLES;
173  params.n_iter_relearn = DEFAULT_ITERATIONS_RELEARN;
174 
175  params.init_method = 1;
176  params.random_seed = -1;
177 
178  params.verbose_level = DEFAULT_VERBOSE;
179  params.log_filename = new char[128];
180  strcpy(params.log_filename,LOG_FILENAME.c_str());
181 
182  params.load_save_flag = 0;
183  params.load_filename = new char[128];
184  strcpy(params.load_filename,LOAD_FILENAME.c_str());
185  params.save_filename = new char[128];
186  strcpy(params.save_filename,SAVE_FILENAME.c_str());
187 
188  params.surr_name = new char[128];
189  // strcpy(params.surr_name,"sStudentTProcessNIG");
190  strcpy(params.surr_name,SURR_NAME.c_str());
191 
192  params.sigma_s = DEFAULT_SIGMA;
193  params.noise = DEFAULT_NOISE;
194  params.alpha = PRIOR_ALPHA;
195  params.beta = PRIOR_BETA;
196 
197  params.l_all = 0;
198  params.l_type = L_EMPIRICAL;
199  params.sc_type = SC_MAP;
200 
201  params.epsilon = 0.0;
202  params.force_jump = 20;
203 
204  params.crit_name = new char[128];
205  strcpy(params.crit_name,CRIT_NAME.c_str());
206  params.n_crit_params = 0;
207 
208  params.kernel = kernel;
209  params.mean = mean;
210 
211  return params;
212 }
213 
217 namespace bayesopt {
218  /*
219  * KernelParameters Class
220  */
221  KernelParameters::KernelParameters():
222  hp_mean(1), hp_std(1){
223  // Set default values
224  name = KERNEL_NAME;
225  hp_mean(0) = KERNEL_THETA;
226  hp_std(0) = KERNEL_SIGMA;
227  }
228 
229  /*
230  * MeanParameters Class
231  */
232  MeanParameters::MeanParameters():
233  coef_mean(1), coef_std(1){
234  // Set default values
235  name = MEAN_NAME;
236  coef_mean(0) = MEAN_MU;
237  coef_std(0) = MEAN_SIGMA;
238  }
239 
240  /*
241  * Parameters Class
242  */
243  Parameters::Parameters():
244  kernel(), mean(), crit_params(){
245  // Set default values
246  init_default();
247  }
248 
249  Parameters::Parameters(bopt_params c_params):
250  kernel(), mean(), crit_params(){
251  n_iterations = c_params.n_iterations;
252  n_inner_iterations = c_params.n_inner_iterations;
253  n_init_samples = c_params.n_init_samples;
254  n_iter_relearn = c_params.n_iter_relearn;
255 
256  init_method = c_params.init_method;
257  random_seed = c_params.random_seed;
258 
259  verbose_level = c_params.verbose_level;
260 
261  log_filename = std::string(c_params.log_filename);
262  load_save_flag = c_params.load_save_flag;
263  load_filename = std::string(c_params.load_filename);
264  save_filename = std::string(c_params.save_filename);
265 
266  surr_name = std::string(c_params.surr_name);
267  sigma_s = c_params.sigma_s;
268 
269  noise = c_params.noise;
270  alpha = c_params.alpha;
271  beta = c_params.beta;
272 
273  sc_type = c_params.sc_type;
274  l_type = c_params.l_type;
275 
276  l_all = c_params.l_all;
277 
278  epsilon = c_params.epsilon;
279  force_jump = c_params.force_jump;
280 
281  kernel.name = std::string(c_params.kernel.name);
282  kernel.hp_mean = bayesopt::utils::array2vector(c_params.kernel.hp_mean,
283  c_params.kernel.n_hp);
284  kernel.hp_std = bayesopt::utils::array2vector(c_params.kernel.hp_std,
285  c_params.kernel.n_hp);
286 
287  mean.name = std::string(c_params.mean.name);
288  mean.coef_mean = bayesopt::utils::array2vector(c_params.mean.coef_mean,
289  c_params.mean.n_coef);
290  mean.coef_std = bayesopt::utils::array2vector(c_params.mean.coef_std,
291  c_params.mean.n_coef);
292 
293  crit_name = c_params.crit_name;
294  crit_params = bayesopt::utils::array2vector(c_params.crit_params,
295  c_params.n_crit_params);
296  }
297 
298  bopt_params Parameters::generate_bopt_params()
299  {
300  bopt_params c_params = initialize_parameters_to_default();
301  c_params.n_iterations = n_iterations;
302  c_params.n_inner_iterations = n_inner_iterations;
303  c_params.n_init_samples = n_init_samples;
304  c_params.n_iter_relearn = n_iter_relearn;
305 
306  c_params.init_method = init_method;
307  c_params.random_seed = random_seed;
308 
309  c_params.verbose_level = verbose_level;
310 
311  strcpy(c_params.log_filename, log_filename.c_str());
312  c_params.load_save_flag = load_save_flag;
313  strcpy(c_params.load_filename, load_filename.c_str());
314  strcpy(c_params.save_filename, save_filename.c_str());
315 
316  strcpy(c_params.surr_name, surr_name.c_str());
317  c_params.sigma_s = sigma_s;
318 
319  c_params.noise = noise;
320  c_params.alpha = alpha;
321  c_params.beta = beta;
322 
323  c_params.sc_type = sc_type;
324 
325  c_params.l_type = l_type;
326 
327  c_params.l_all = l_all;
328 
329  c_params.epsilon = epsilon;
330  c_params.force_jump = force_jump;
331 
332  strcpy(c_params.kernel.name, kernel.name.c_str());
333  //TODO (Javier): Should it be necessary to check size?
334  for(size_t i=0; i<kernel.hp_mean.size(); i++){
335  c_params.kernel.hp_mean[i] = kernel.hp_mean[i];
336  }
337  for(size_t i=0; i<kernel.hp_std.size(); i++){
338  c_params.kernel.hp_std[i] = kernel.hp_std[i];
339  }
340  c_params.kernel.n_hp = kernel.hp_std.size();
341 
342  strcpy(c_params.mean.name, mean.name.c_str());
343  for(size_t i=0; i<mean.coef_mean.size(); i++){
344  c_params.mean.coef_mean[i] = mean.coef_mean[i];
345  }
346  for(size_t i=0; i<mean.coef_std.size(); i++){
347  c_params.mean.coef_std[i] = mean.coef_std[i];
348  }
349  c_params.mean.n_coef = mean.coef_std.size();
350 
351  strcpy(c_params.crit_name, crit_name.c_str());
352  for(size_t i=0; i<crit_params.size(); i++){
353  c_params.crit_params[i] = crit_params[i];
354  }
355  c_params.n_crit_params = crit_params.size();
356 
357  return c_params;
358  }
359 
360  void Parameters::bostrdup (char* d, const char *s) {
361  // Warning. This memory is not freed!
362  d = new char[strlen (s) + 1]; // Space for length plus nul
363  if (d == NULL) d = NULL; // No memory
364  strcpy (d,s); // Copy the characters
365  }
366 
367  void Parameters::set_learning(std::string name){
368  l_type = str2learn(name.c_str());
369  }
370  std::string Parameters::get_learning(){
371  return std::string(learn2str(l_type));
372  }
373 
374  void Parameters::set_score(std::string name){
375  sc_type = str2score(name.c_str());
376  }
377  std::string Parameters::get_score(){
378  return std::string(score2str(sc_type));
379  }
380 
381  void Parameters::init_default(){
382  n_iterations = DEFAULT_ITERATIONS;
383  n_inner_iterations = DEFAULT_INNER_EVALUATIONS;
384  n_init_samples = DEFAULT_INIT_SAMPLES;
385  n_iter_relearn = DEFAULT_ITERATIONS_RELEARN;
386 
387  init_method = 1;
388  random_seed = -1;
389 
390  verbose_level = DEFAULT_VERBOSE;
391  log_filename = LOG_FILENAME;
392 
393  load_save_flag = 0;
394  load_filename = LOAD_FILENAME;
395  save_filename = SAVE_FILENAME;
396 
397  surr_name = SURR_NAME;
398 
399  sigma_s = DEFAULT_SIGMA;
400  noise = DEFAULT_NOISE;
401  alpha = PRIOR_ALPHA;
402  beta = PRIOR_BETA;
403 
404  l_all = false;
405  l_type = L_EMPIRICAL;
406  sc_type = SC_MAP;
407 
408  epsilon = 0.0;
409  force_jump = 20;
410 
411  crit_name = CRIT_NAME;
412  }
413 }//namespace bayesopt
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
Namespace of the library interface.
Definition: using.dox:1
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
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
Extra functions for Ublas library.
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 l_all
Learn all hyperparameters or only kernel.
Definition: parameters.h:115
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
Kernel configuration parameters.
Definition: parameters.h:66
Parameter definitions.
Parameter definitions.
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