BayesOpt
bayesoptdisc.cpp
1 
2 /*
3 -------------------------------------------------------------------------
4  This file is part of BayesOpt, an efficient C++ library for
5  Bayesian optimization.
6 
7  Copyright (C) 2011-2015 Ruben Martinez-Cantin <rmcantin@unizar.es>
8 
9  BayesOpt is free software: you can redistribute it and/or modify it
10  under the terms of the GNU Affero General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  BayesOpt is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Affero General Public License for more details.
18 
19  You should have received a copy of the GNU Affero General Public License
20  along with BayesOpt. If not, see <http://www.gnu.org/licenses/>.
21 ------------------------------------------------------------------------
22 */
23 
24 
25 #include "bayesopt/bayesopt.hpp"
26 
27 #include <boost/bind.hpp>
28 #include <boost/numeric/ublas/matrix_proxy.hpp>
29 //#include "randgen.hpp"
30 #include "lhs.hpp"
31 #include "gridsampling.hpp"
32 #include "log.hpp"
33 
34 namespace bayesopt
35 {
36 
37  // DiscreteModel::DiscreteModel(const vecOfvec &validSet):
38  // BayesOptBase(), mInputSet(validSet)
39  // {} // Constructor
40 
41 
42  DiscreteModel::DiscreteModel( const vecOfvec &validSet,
43  Parameters parameters):
44  BayesOptBase(validSet[0].size(),parameters), mInputSet(validSet)
45  {
46  mDims = mInputSet[0].size();
47  } // Constructor
48 
49  DiscreteModel::DiscreteModel(const vectori &categories,
50  Parameters parameters):
51  BayesOptBase(categories.size(),parameters)
52  {
53  mDims = categories.size();
54  utils::buildGrid(categories,mInputSet);
55  }
56 
57 
59  {} // Default destructor
60 
61 
62 
63  // PROTECTED
65  {
66  randInt sample(mEngine, intUniformDist(0,mInputSet.size()-1));
67  return mInputSet[sample()];
68  };
69 
70  void DiscreteModel::findOptimal(vectord &xOpt)
71  {
72  std::vector<double> critv(mInputSet.size());
73  std::transform(mInputSet.begin(),mInputSet.end(),critv.begin(),
74  boost::bind(&DiscreteModel::evaluateCriteria,this,_1));
75 
76  xOpt = mInputSet[std::distance(critv.begin(),
77  std::max_element(critv.begin(),critv.end()))];
78 
79  // xOpt = *mInputSet.begin();
80  // double min = evaluateCriteria(xOpt);
81 
82  // for(vecOfvec::iterator it = mInputSet.begin();
83  // it != mInputSet.end(); ++it)
84  // {
85  // double current = evaluateCriteria(*it);
86  // if (current < min)
87  // {
88  // xOpt = *it;
89  // min = current;
90  // }
91  // }
92  }
93 
94  //In this case, it is the trivial function
95  vectord DiscreteModel::remapPoint(const vectord& x)
96  { return x; }
97 
98  void DiscreteModel::generateInitialPoints(matrixd& xPoints)
99  {
100 
101  vecOfvec perms = mInputSet;
102 
103  // By using random permutations, we guarantee that
104  // the same point is not selected twice
106 
107  // vectord xPoint(mInputSet[0].size());
108  for(size_t i = 0; i < xPoints.size1(); i++)
109  {
110  const vectord xP = perms[i];
111  row(xPoints,i) = xP;
112  }
113  }
114 
115 } // namespace bayesopt
116 
117 
DiscreteModel()
Default constructor forbidden.
void findOptimal(vectord &xOpt)
Call the inner optimization method to find the optimal point acording to the criteria.
vectord samplePoint()
Sample a single point in the input space.
Namespace of the library interface.
Definition: using.dox:1
boost::mt19937 mEngine
Random number generator.
BayesOpt main C++ interface.
size_t mDims
Number of dimensions.
virtual ~DiscreteModel()
Default destructor.
Latin Hypercube Sampling.
void randomPerms(D &arr, randEngine &mtRandom)
Modify an array using ramdom permutations.
Definition: lhs.hpp:80
Abstract module for Bayesian optimization.
void generateInitialPoints(matrixd &xPoints)
Selects the initial set of points to build the surrogate model.
vectord remapPoint(const vectord &x)
Remap the point x to the original space.
Regular grid sampling.
vecOfvec mInputSet
List of input points.
Definition: bayesopt.hpp:205
Modules and helper macros for logging.