BayesOpt
bopt_state.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 "bopt_state.hpp"
24 #include "fileparser.hpp"
25 
26 #include <fstream>
27 #include <iostream>
28 
29 namespace bayesopt
30 {
31  BOptState::BOptState(): mParameters(){}
32 
33  void BOptState::saveToFile(std::string filename){
34  utils::FileParser fp(filename);
35  fp.openOutput();
36  loadOrSave(fp);
37  }
38 
39  bool BOptState::loadFromFile(std::string filename, Parameters &program_params){
40  utils::FileParser fp(filename);
41  if(!fp.fileExists()){
42  return false;
43  }
44  fp.openInput();
45  loadOrSave(fp);
46 
47  // Non-stored fields: verbose_level, log_filename, load_save_flag, load_filename,
48  // save_filename, random_seed.
49 
50  // Fill non-stored values with program_params values:
51  mParameters.verbose_level = program_params.verbose_level;
52  mParameters.log_filename = program_params.log_filename;
53  mParameters.load_save_flag = program_params.load_save_flag;
54  mParameters.load_filename = program_params.load_filename;
55  mParameters.save_filename = program_params.save_filename;
56  mParameters.random_seed = program_params.random_seed;
57 
58  // n_iterations can be taken from program_params if value is greater than current iteration
59  if(program_params.n_iterations > mCurrentIter){
60  mParameters.n_iterations = program_params.n_iterations;
61  }
62 
63  return true;
64  }
65 
66  void BOptState::loadOrSave(utils::FileParser &fp){
67  fp.readOrWrite("mCurrentIter", mCurrentIter);
68  fp.readOrWrite("mCounterStuck", mCounterStuck);
69  fp.readOrWrite("mYPrev", mYPrev);
70 
71  fp.readOrWrite("mParameters.n_iterations", mParameters.n_iterations);
72  fp.readOrWrite("mParameters.n_inner_iterations", mParameters.n_inner_iterations);
73  fp.readOrWrite("mParameters.n_init_samples", mParameters.n_init_samples);
74  fp.readOrWrite("mParameters.n_iter_relearn", mParameters.n_iter_relearn);
75  fp.readOrWrite("mParameters.init_method", mParameters.init_method);
76 
77  fp.readOrWrite("mParameters.surr_name", mParameters.surr_name);
78  fp.readOrWrite("mParameters.sigma_s", mParameters.sigma_s);
79  fp.readOrWrite("mParameters.noise", mParameters.noise);
80  fp.readOrWrite("mParameters.alpha", mParameters.alpha);
81  fp.readOrWrite("mParameters.beta", mParameters.beta);
82 
83  // Enums
84  if(fp.isReading()){
85  mParameters.sc_type = str2score(fp.read("mParameters.sc_type").c_str());
86  mParameters.l_type = str2learn(fp.read("mParameters.l_type").c_str());
87  }
88  else if(fp.isWriting()){
89  fp.write("mParameters.sc_type",score2str(mParameters.sc_type));
90  fp.write("mParameters.l_type", learn2str(mParameters.l_type));
91  }
92 
93  fp.readOrWrite("mParameters.l_all", mParameters.l_all);
94  fp.readOrWrite("mParameters.epsilon", mParameters.epsilon);
95  fp.readOrWrite("mParameters.force_jump", mParameters.force_jump);
96 
97  fp.readOrWrite("mParameters.kernel.name", mParameters.kernel.name);
98  fp.readOrWrite("mParameters.kernel.hp_mean", mParameters.kernel.hp_mean);
99  fp.readOrWrite("mParameters.kernel.hp_std", mParameters.kernel.hp_std);
100 
101  fp.readOrWrite("mParameters.mean.name", mParameters.mean.name);
102  fp.readOrWrite("mParameters.mean.coef_mean", mParameters.mean.coef_mean);
103  fp.readOrWrite("mParameters.mean.coef_std", mParameters.mean.coef_std);
104 
105  fp.readOrWrite("mParameters.crit_name", mParameters.crit_name);
106  fp.readOrWrite("mParameters.crit_params", mParameters.crit_params);
107 
108  fp.readOrWrite("mY", mY);
109  fp.readOrWrite("mX", mX);
110  }
111 } //namespace bayesopt
112 
double epsilon
For epsilon-greedy exploration.
Definition: parameters.hpp:99
KernelParameters kernel
Kernel parameters.
Definition: parameters.hpp:107
bool l_all
Learn all hyperparameters or only kernel.
Definition: parameters.hpp:97
size_t load_save_flag
1-Load data,2-Save data, 3-Load and save data.
Definition: parameters.hpp:80
std::string name
Name of the mean function.
Definition: parameters.hpp:53
void saveToFile(std::string filename)
Creates or overwrites the provided file with the state.
Definition: bopt_state.cpp:33
vectord coef_std
Basis function coefficients (std)
Definition: parameters.hpp:55
MeanParameters mean
Mean (parametric function) parameters.
Definition: parameters.hpp:108
double sigma_s
Signal variance (if known).
Definition: parameters.hpp:86
vectord hp_mean
Kernel hyperparameters prior (mean, log space)
Definition: parameters.hpp:43
Namespace of the library interface.
Definition: using.dox:1
learning_type l_type
Type of learning for the kernel params.
Definition: parameters.hpp:96
std::string load_filename
Init data file path (if applicable)
Definition: parameters.hpp:82
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
vectord crit_params
Criterion hyperparameters (if needed)
Definition: parameters.hpp:111
size_t n_inner_iterations
Maximum inner optimizer evaluations.
Definition: parameters.hpp:67
double beta
Inverse Gamma prior for signal var.
Definition: parameters.hpp:92
bool loadFromFile(std::string filename, Parameters &program_params)
Loads the state from the provided file and takes program_params values if needed. ...
Definition: bopt_state.cpp:39
size_t n_iterations
Maximum BayesOpt evaluations (budget)
Definition: parameters.hpp:66
size_t force_jump
If >0, and the difference between two consecutive observations is pure noise, for n consecutive steps...
Definition: parameters.hpp:100
Representation of a optimization state.
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
std::string log_filename
Log file path (if applicable)
Definition: parameters.hpp:78
double alpha
Inverse Gamma prior for signal var.
Definition: parameters.hpp:90
BOptState()
Constructor, parameters set into default.
Definition: bopt_state.cpp:31
std::string crit_name
Name of the criterion.
Definition: parameters.hpp:110
int verbose_level
Neg-Error,0-Warning,1-Info,2-Debug -> stdout 3-Error,4-Warning,5-Info,>5-Debug -> logfile...
Definition: parameters.hpp:76
size_t init_method
Sampling method for initial set 1-LHS, 2-Sobol (if available), other value-uniformly distributed...
Definition: parameters.hpp:73
double noise
Variance of observation noise (and nugget)
Definition: parameters.hpp:88
Functions to write and parse data files.
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
std::string save_filename
Sava data file path (if applicable)
Definition: parameters.hpp:83
int random_seed
>=0 -> Fixed seed, <0 -> Time based (variable).
Definition: parameters.hpp:74
size_t n_iter_relearn
Number of samples before relearn kernel.
Definition: parameters.hpp:69