BayesOpt
branin_system_calls.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 #define _USE_MATH_DEFINES
24 #include <cmath>
25 #include <algorithm>
26 #include <boost/numeric/ublas/assignment.hpp>
27 #include "bayesopt/bayesopt.hpp"
28 #include "param_loader.hpp"
29 #include "fileparser.hpp"
30 
31 #ifndef M_PI
32 /* It shouldn't be necessary, but Windows is completely nuts about
33  math constants and I HATE when include order matters. */
34  #define M_PI 3.14159265358979323846
35 #endif
36 
38 {
39 public:
41  ContinuousModel(2,par) {}
42 
43  double evaluateSample( const vectord& xin)
44  {
45  if (xin.size() != 2)
46  {
47  std::cout << "WARNING: This only works for 2D inputs." << std::endl
48  << "WARNING: Using only first two components." << std::endl;
49  }
50 
51  float y = -1;
52  double x1 = xin(0);
53  double x2 = xin(1);
54 
55  bayesopt::utils::FileParser fp("results.txt");
56  std::string call = "python ../examples/standalone_calls/eval_branin.py " +
57  fp.to_string(x1) + " " + fp.to_string(x2);
58  int ret = system(call.c_str());
59 
60  fp.openInput();
61  fp.read("y",y);
62  fp.close();
63 
64  return y;
65  }
66 
67  bool checkReachability(const vectord &query)
68  {return true;};
69 
70  inline double sqr( double x ){ return x*x; };
71 
72  void printOptimal()
73  {
74  vectord sv(2);
75  sv(0) = 0.1238938; sv(1) = 0.818333;
76  std::cout << "Solutions: " << sv << "->"
77  << evaluateSample(sv) << std::endl;
78  sv(0) = 0.5427728; sv(1) = 0.151667;
79  std::cout << "Solutions: " << sv << "->"
80  << evaluateSample(sv) << std::endl;
81  sv(0) = 0.961652; sv(1) = 0.1650;
82  std::cout << "Solutions: " << sv << "->"
83  << evaluateSample(sv) << std::endl;
84  }
85 
86 };
87 
88 int main(int nargs, char *args[])
89 {
91  if(nargs > 1){
92  if(!bayesopt::utils::ParamLoader::load(args[1], par)){
93  std::cout << "ERROR: provided file \"" << args[1] << "\" does not exist" << std::endl;
94  return -1;
95  }
96  }
97  else{
98  par = initialize_parameters_to_default();
99  par.n_iterations = 190;
100  par.random_seed = 0;
101  par.verbose_level = 1;
102  par.noise = 1e-10;
103  //bayesopt::utils::ParamLoader::save("system_opt.txt", par);
104  }
105 
106 
107  SystemCallsBranin branin(par);
108  vectord result(2);
109 
110  branin.optimize(result);
111  std::cout << "Result: " << result << "->"
112  << branin.evaluateSample(result) << std::endl;
113  branin.printOptimal();
114 
115  // Remove results.txt file
116  std::string filename("results.txt");
117  if( remove( filename.c_str() ) == 0 ){
118  std::cout << "File \"" << filename << "\" successfully removed" << std::endl;
119  }
120  else{
121  std::cout << "Error: cannot remove \"" << filename << "\" file" << std::endl;
122  }
123 
124  return 0;
125 }
126 
127 
Bayesian optimization for functions in continuous input spaces.
Definition: bayesopt.hpp:78
ContinuousModel()
Default constructor forbidden.
bool checkReachability(const vectord &query)
This function checks if the query is valid or not.
BayesOpt main C++ interface.
size_t n_iterations
Maximum BayesOpt evaluations (budget)
Definition: parameters.hpp:66
void optimize(vectord &bestPoint)
Execute the optimization process of the function defined in evaluateSample.
double evaluateSample(const vectord &xin)
Function that defines the actual function to be optimized.
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
double noise
Variance of observation noise (and nugget)
Definition: parameters.hpp:88
Functions to write and parse data files.
Allows to load parameters from file.
int random_seed
>=0 -> Fixed seed, <0 -> Time based (variable).
Definition: parameters.hpp:74