BayesOpt
bo_disc.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 <boost/numeric/ublas/matrix_proxy.hpp> // For row()
24 
25 #include "bayesopt/bayesopt.h" // For the C API
26 #include "bayesopt/bayesopt.hpp" // For the C++ API
27 #include "lhs.hpp"
28 #include "specialtypes.hpp"
29 
30 
31 /* Function to be used for C-API testing */
32 double testFunction(unsigned int n, const double *x,
33  double *gradient, /* NULL if not needed */
34  void *func_data)
35 {
36  double f = 10.;
37  for (unsigned int i = 0; i < n; ++i)
38  {
39  f += (x[i] - .53) * (x[i] - .53);
40  }
41  return f;
42 }
43 
44 /* Class to be used for C++-API testing */
46 {
47  public:
48 
49  ExampleDisc(const vecOfvec & validSet, bayesopt::Parameters param):
50  DiscreteModel(validSet,param) {}
51 
52  double evaluateSample( const vectord &Xi )
53  {
54  double x[100];
55  for (size_t i = 0; i < Xi.size(); ++i)
56  {
57  x[i] = Xi(i);
58  }
59  return testFunction(Xi.size(),x,NULL,NULL);
60  };
61 
62  bool checkReachability( const vectord &query )
63  { return true; };
64 };
65 
66 
67 int main(int nargs, char *args[])
68 {
69  // Common configuration
70  // See parameters.h for the available options
71  // This is just an example of what can be done.
73 
74  par.surr_name = "sStudentTProcessJef";
75  par.noise = 1e-10;
76  par.n_iterations = 70; // Number of iterations
77  par.n_init_samples = 20;
78  /*******************************************/
79 
80  const size_t nPoints = 1000; // Number of discrete points
81  const size_t n = 6; // Number of dimensions
82 
83  // Let's generate a discrete grid by latin hypercube sampling in the n-dimensional
84  // space.
85  randEngine mtRandom;
86  matrixd xPoints(nPoints,n);
87 
88  bayesopt::utils::lhs(xPoints,mtRandom);
89 
90 
91  // Set of discrete points using C arrays.
92  // Thanks to the quirks of Visual Studio, the following expression is invalid, so we
93  // have to replace it by a literal. WARNING: Make sure to update the size of the array
94  // if the number of points or dimensions change.
95 #ifdef _MSC_VER
96  double xPointsArray[6000];
97 #else
98  const size_t nPinArr = n*nPoints;
99  double xPointsArray[nPinArr];
100 #endif
101 
102  // Set of discrete points using ublas vectors. For the C++ interface.
103  vecOfvec xP;
104 
105 
106  // Lets copy the generated points to the specific structure for each interface.
107  for(size_t i = 0; i<nPoints; ++i)
108  {
109  vectord point = row(xPoints,i);
110  xP.push_back(point);
111  for(size_t j=0; j<n; ++j)
112  {
113  xPointsArray[i*n+j] = point(j);
114  }
115  }
116 
117 
118  /*******************************************/
119  // Run C++ interface
120  std::cout << "Running C++ interface" << std::endl;
121  ExampleDisc opt(xP,par);
122  vectord result(n);
123  opt.optimize(result);
124 
125 
126  // Run C interface
127  std::cout << "Running C interface" << std::endl;
128  double x[128], fmin[128];
129  bayes_optimization_disc(n, &testFunction, NULL, xPointsArray, nPoints,
130  x, fmin, par.generate_bopt_params());
131 
132 
133  // Find the optimal value by brute force
134  size_t min = 0;
135  double minvalue = opt.evaluateSample(row(xPoints,min));
136  for(size_t i = 1; i<nPoints; ++i)
137  {
138  vectord point = row(xPoints,i);
139  if (opt.evaluateSample(point) < minvalue)
140  {
141  min = i;
142  minvalue = opt.evaluateSample(row(xPoints,min));
143  std::cout << i << "," << minvalue << std::endl;
144  }
145  }
146 
147  std::cout << "Final result C++: " << result << " | Value:" << opt.evaluateSample(result) << std::endl;
148  std::cout << "Final result C: ["<< n << "](";
149  for (int i = 0; i < n; i++ )
150  std::cout << x[i] << ", ";
151  std::cout << ")" << " | Value:" << fmin[0] << std::endl;
152  std::cout << "Optimal: " << row(xPoints,min) << " | Value:" << minvalue << std::endl;
153 }
Boost vector and matrix types.
DiscreteModel()
Default constructor forbidden.
BAYESOPT_API int bayes_optimization_disc(int nDim, eval_func f, void *f_data, double *valid_x, size_t n_points, double *x, double *minf, bopt_params parameters)
C wrapper for the Bayesian optimization algorithm.
size_t n_init_samples
Number of samples before optimization.
Definition: parameters.hpp:68
BayesOpt main C++ interface.
BayesOpt wrapper for C interface.
Latin Hypercube Sampling.
void lhs(M &Result, randEngine &mtRandom)
Latin hypercube sampling It is used to generate a uniform Latin hypercube.
Definition: lhs.hpp:91
double evaluateSample(const vectord &Xi)
Function that defines the actual function to be optimized.
Definition: bo_disc.cpp:52
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.
std::string surr_name
Name of the surrogate function.
Definition: parameters.hpp:85
Bayesian optimization for functions in discrete spaces.
Definition: bayesopt.hpp:166
double noise
Variance of observation noise (and nugget)
Definition: parameters.hpp:88
bool checkReachability(const vectord &query)
This function checks if the query is valid or not.
Definition: bo_disc.cpp:62