BayesOpt
bayesoptcont.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 "bayesopt/bayesopt.hpp"
24 
25 #include <limits>
26 #include <boost/numeric/ublas/matrix_proxy.hpp>
27 
28 #include "lhs.hpp"
29 #include "randgen.hpp"
30 #include "log.hpp"
31 #include "boundingbox.hpp"
32 #include "inneroptimization.hpp"
33 
34 
35 
36 namespace bayesopt {
37 
39  {
40  public:
41  explicit CritCallback(ContinuousModel* model):mBO(model){};
42  double evaluate(const vectord &query)
43  {
44  return mBO->evaluateCriteria(query);
45  }
46  private:
47  ContinuousModel* mBO;
48  };
49 
51  BayesOptBase(dim,parameters)
52  {
53  mCallback.reset(new CritCallback(this));
54  cOptimizer.reset(new NLOPT_Optimization(mCallback.get(),dim));
55  cOptimizer->setAlgorithm(COMBINED);
56  cOptimizer->setMaxEvals(parameters.n_inner_iterations);
57 
58  vectord lowerBound = zvectord(mDims);
59  vectord upperBound = svectord(mDims,1.0);
60  mBB.reset(new utils::BoundingBox<vectord>(lowerBound,upperBound));
61  } // Constructor
62 
64  {
65  // delete cOptimizer;
66  } // Default destructor
67 
68  void ContinuousModel::setBoundingBox(const vectord &lowerBound,
69  const vectord &upperBound)
70  {
71  // We don't change the bounds of the inner optimization because,
72  // thanks to this bounding box model, everything is mapped to the
73  // unit hypercube, thus the default inner optimization are just
74  // right.
75  mBB.reset(new utils::BoundingBox<vectord>(lowerBound,upperBound));
76 
77  FILE_LOG(logINFO) << "Bounds: ";
78  FILE_LOG(logINFO) << lowerBound;
79  FILE_LOG(logINFO) << upperBound;
80  } //setBoundingBox
81 
82 
83 
84 
85 
87 
89  {
90  randFloat drawSample(mEngine,realUniformDist(0,1));
91  vectord Xnext(mDims);
92  for(vectord::iterator x = Xnext.begin(); x != Xnext.end(); ++x)
93  {
94  *x = drawSample();
95  }
96  return Xnext;
97  };
98 
99  void ContinuousModel::findOptimal(vectord &xOpt)
100  {
101  double minf = cOptimizer->run(xOpt);
102 
103  //Let's try some local exploration like spearmint
104  randNFloat drawSample(mEngine,normalDist(0,0.001));
105  for(size_t ii = 0;ii<5; ++ii)
106  {
107  vectord pert = getPointAtMinimum();
108  for(size_t j=0; j<xOpt.size(); ++j)
109  {
110  pert(j) += drawSample();
111  }
112  try
113  {
114  double minf2 = cOptimizer->localTrialAround(pert);
115  if (minf2<minf)
116  {
117  minf = minf2;
118  FILE_LOG(logDEBUG) << "Local beats Global";
119  xOpt = pert;
120  }
121  }
122  catch(std::invalid_argument& e)
123  {
124  //We ignore this one
125  }
126  }
127  };
128 
129  vectord ContinuousModel::remapPoint(const vectord& x)
130  {
131  return mBB->unnormalizeVector(x);
132  }
133 
135  {
137  }
138 } //namespace bayesopt
vectord samplePoint()
Sample a single point in the input space.
void findOptimal(vectord &xOpt)
Call the inner optimization method to find the optimal point acording to the criteria.
vectord remapPoint(const vectord &x)
Remap the point x to the original space (e.g.
Namespace of the library interface.
Definition: using.dox:1
Bayesian optimization for functions in continuous input spaces.
Definition: bayesopt.hpp:78
ContinuousModel()
Default constructor forbidden.
void generateInitialPoints(matrixd &xPoints)
Selects the initial set of points to build the surrogate model.
boost::mt19937 mEngine
Random number generator.
Parameters mParameters
Configuration parameters.
BayesOpt main C++ interface.
size_t mDims
Number of dimensions.
Latin Hypercube Sampling.
size_t n_inner_iterations
Maximum inner optimizer evaluations.
Definition: parameters.hpp:67
Defines a bounding box or axis-alligned bound constraints.
Definition: bayesopt.hpp:35
Abstract module for Bayesian optimization.
void setBoundingBox(const vectord &lowerBound, const vectord &upperBound)
Sets the bounding box.
void samplePoints(M &xPoints, int method, randEngine &mtRandom)
Selects the sampling method.
Definition: lhs.hpp:141
Boost types for random number generation.
virtual ~ContinuousModel()
Default destructor.
Global exploration, local refinement (hand tuned)
Module for box constrain management.
boost::scoped_ptr< utils::BoundingBox< vectord > > mBB
Bounding Box (input space limits)
Definition: bayesopt.hpp:121
size_t init_method
Sampling method for initial set 1-LHS, 2-Sobol (if available), other value-uniformly distributed...
Definition: parameters.hpp:73
C++ wrapper of the NLOPT library.
Modules and helper macros for logging.
vectord getPointAtMinimum()
Get optimal point in the inner space (e.g.