BayesOpt
param_loader.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 "param_loader.hpp"
24 
25 namespace bayesopt
26 {
27  namespace utils{
28  bool ParamLoader::load(std::string filename, Parameters &par){
29  utils::FileParser fp(filename);
30  if(!fp.fileExists()){
31  return false;
32  }
33 
34  par = initialize_parameters_to_default();
35  fp.openInput();
36 
37  loadOrSave(fp, par);
38  return true;
39  }
40 
41  void ParamLoader::save(std::string filename, Parameters &par){
42  utils::FileParser fp(filename);
43  fp.openOutput();
44 
45  loadOrSave(fp, par);
46  }
47 
48  void ParamLoader::loadOrSave(utils::FileParser &fp, Parameters &par){
49  fp.readOrWrite("n_iterations", par.n_iterations);
50  fp.readOrWrite("n_inner_iterations", par.n_inner_iterations);
51  fp.readOrWrite("n_init_samples", par.n_init_samples);
52  fp.readOrWrite("n_iter_relearn", par.n_iter_relearn);
53  fp.readOrWrite("init_method", par.init_method);
54  fp.readOrWrite("random_seed", par.random_seed);
55  fp.readOrWrite("verbose_level", par.verbose_level);
56  fp.readOrWrite("log_filename", par.log_filename);
57  fp.readOrWrite("load_save_flag", par.load_save_flag);
58  fp.readOrWrite("load_filename", par.load_filename);
59  fp.readOrWrite("save_filename", par.save_filename);
60  fp.readOrWrite("surr_name", par.surr_name);
61  fp.readOrWrite("sigma_s", par.sigma_s);
62  fp.readOrWrite("noise", par.noise);
63  fp.readOrWrite("alpha", par.alpha);
64  fp.readOrWrite("beta", par.beta);
65 
66  // Enums
67  if(fp.isReading()){
68  par.sc_type = str2score(fp.read("sc_type").c_str());
69  par.l_type = str2learn(fp.read("l_type").c_str());
70  }
71  else if(fp.isWriting()){
72  fp.write("sc_type",score2str(par.sc_type));
73  fp.write("l_type", learn2str(par.l_type));
74  }
75 
76  fp.readOrWrite("l_all", par.l_all);
77  fp.readOrWrite("epsilon", par.epsilon);
78  fp.readOrWrite("force_jump", par.force_jump);
79 
80  fp.readOrWrite("kernel.name", par.kernel.name);
81  fp.readOrWrite("kernel.hp_mean", par.kernel.hp_mean);
82  fp.readOrWrite("kernel.hp_std", par.kernel.hp_std);
83 
84  fp.readOrWrite("mean.name", par.mean.name);
85  fp.readOrWrite("mean.coef_mean", par.mean.coef_mean);
86  fp.readOrWrite("mean.coef_std", par.mean.coef_std);
87 
88  fp.readOrWrite("crit_name", par.crit_name);
89  fp.readOrWrite("crit_params", par.crit_params);
90 
91  }
92  } //namespace utils
93 } //namespace bayesopt
94 
Namespace of the library interface.
Definition: using.dox:1
Allows to load parameters from file.