BayesOpt
bayesoptbase.hpp
Go to the documentation of this file.
1 
3 /*
4 -------------------------------------------------------------------------
5  This file is part of BayesOpt, an efficient C++ library for
6  Bayesian optimization.
7 
8  Copyright (C) 2011-2015 Ruben Martinez-Cantin <rmcantin@unizar.es>
9 
10  BayesOpt is free software: you can redistribute it and/or modify it
11  under the terms of the GNU Affero General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  BayesOpt is distributed in the hope that it will be useful, but
16  WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Affero General Public License for more details.
19 
20  You should have received a copy of the GNU Affero General Public License
21  along with BayesOpt. If not, see <http://www.gnu.org/licenses/>.
22 ------------------------------------------------------------------------
23 */
24 
25 
26 #ifndef _BAYESOPTBASE_HPP_
27 #define _BAYESOPTBASE_HPP_
28 
29 #include <boost/scoped_ptr.hpp>
30 #include <boost/random.hpp>
31 #include <boost/numeric/ublas/vector.hpp>
32 #include <boost/numeric/ublas/matrix.hpp>
33 #include <boost/numeric/ublas/io.hpp>
34 #include "bayesopt/parameters.hpp"
35 
36 
40 namespace bayesopt {
41 
42 
43  //Forward declaration
44  class PosteriorModel;
45  class ProbabilityDistribution;
46  class Dataset;
47  class BOptState;
48 
49  typedef boost::numeric::ublas::vector<double> vectord;
50  typedef boost::numeric::ublas::vector<int> vectori;
51  typedef boost::numeric::ublas::matrix<double> matrixd;
52  typedef std::vector<vectord> vecOfvec;
53 
58 
72  class BAYESOPT_API BayesOptBase
73  {
74  public:
79  BayesOptBase(size_t dim, Parameters params);
80 
84  virtual ~BayesOptBase();
85 
94  virtual double evaluateSample( const vectord &query ) = 0;
95 
96 
113  virtual bool checkReachability( const vectord &query )
114  { return true; };
115 
116 
126  void optimize(vectord &bestPoint);
127 
132  void stepOptimization();
133 
135  void initializeOptimization();
136 
138  vectord getFinalResult();
139 
141  void saveOptimization(BOptState &state);
142 
144  void restoreOptimization(BOptState state);
145 
146  // Getters and Setters
147  ProbabilityDistribution* getPrediction(const vectord& query);
148  const Dataset* getData();
149  Parameters* getParameters();
150  double getValueAtMinimum();
151  size_t getCurrentIter();
152  double evaluateCriteria(const vectord& query);
153 
154  protected:
156  vectord getPointAtMinimum();
157 
161  double evaluateSampleInternal( const vectord &query );
162 
165  virtual vectord samplePoint() = 0;
166 
172  virtual void findOptimal(vectord &xOpt) = 0;
173 
176  virtual vectord remapPoint(const vectord& x) = 0;
177 
179  virtual void generateInitialPoints(matrixd& xPoints) = 0;
180 
188  void plotStepData(size_t iteration, const vectord& xNext,
189  double yNext);
190 
192  void saveInitialSamples(matrixd xPoints);
193  void saveResponse(double yPoint, bool clear);
194 
195  protected:
197  size_t mDims;
198  size_t mCurrentIter;
199  boost::mt19937 mEngine;
200 
201  private:
202  boost::scoped_ptr<PosteriorModel> mModel;
203  double mYPrev;
204  size_t mCounterStuck;
205  private:
206 
207  BayesOptBase();
208 
215  vectord nextPoint();
216 
217  };
218 
223 } //namespace bayesopt
224 
225 
226 #endif
Class that represents the state of an optimization.
Definition: bopt_state.hpp:46
Namespace of the library interface.
Definition: using.dox:1
virtual bool checkReachability(const vectord &query)
This function checks if the query is valid or not.
boost::mt19937 mEngine
Random number generator.
Parameters mParameters
Configuration parameters.
size_t mDims
Number of dimensions.
Abstract module for Bayesian optimization.
Dataset model to deal with the vector (real) based datasets.
Definition: dataset.hpp:40
size_t mCurrentIter
Current iteration number.
Parameter definitions.