BayesOpt
bayesoptmodule.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 
4 # ----------------------------------------------------------------------------
5 # This file is part of BayesOptimization, an efficient C++ library for
6 # Bayesian optimization.
7 #
8 # Copyright (C) 2011-2015 Ruben Martinez-Cantin <rmcantin@unizar.es>
9 #
10 # BayesOptimization is free software: you can redistribute it and/or modify
11 # it 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 # BayesOptimization is distributed in the hope that it will be useful,
16 # but 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 BayesOptimization. If not, see <http://www.gnu.org/licenses/>.
22 # ----------------------------------------------------------------------------
23 
24 import numpy as np
25 import bayesopt as bo
26 
27 
33 
34 
38 class BayesOptContinuous(object):
39 
40 
45  def __init__(self,n_dim):
46 
47  self.params = {}
48 
49  self.n_dim = n_dim
50 
51  self.lb = np.zeros((self.n_dim,))
52 
53  self.ub = np.ones((self.n_dim,))
54 
55  @property
56  def parameters(self):
57  return self.params
58 
59  @parameters.setter
60  def parameters(self,params):
61  self.params = params
62 
63  @property
64  def lower_bound(self):
65  return self.lb
66 
67  @lower_bound.setter
68  def lower_bound(self,lb):
69  self.lb = lb
70 
71  @property
72  def upper_bound(self):
73  return self.ub
74 
75  @upper_bound.setter
76  def upper_bound(self,ub):
77  self.ub = ub
78 
79 
81  def evaluateSample(self, x_in):
82  raise NotImplementedError("Please Implement this method")
83 
84 
85  def optimize(self):
86  min_val, x_out, error = bo.optimize(self.evaluateSample, self.n_dim,
87  self.lb, self.ub,
88  self.params)
89 
90  return min_val, x_out, error
91 
92 
93 
98 
99 
104  def __init__(self, x_set=None, n_dim=None, n_samples=None):
105 
106  self.params = {} #bo.initialize_params()
107  if x_set is None:
108 
109  if n_dim is None or n_samples is None:
110  raise ValueError
111  else:
112  self.x_set = np.random.rand(n_samples, n_dim)
113 
114  @property
115  def parameters(self):
116  return self.params
117 
118  @parameters.setter
119  def parameters(self,params):
120  self.params = params
121 
122 
123 
125  def evaluateSample(self, x_in):
126  raise NotImplementedError("Please Implement this method")
127 
128 
129  def optimize(self):
130  min_val, x_out, error = bo.optimize_discrete(self.evaluateSample,
131  self.x_set,
132  self.params)
133 
134  return min_val, x_out, error
135 
136 
137 
142 
143 
148  def __init__(self, categories=None):
149 
150  self.params = {}
151  self.categories = categories
152 
153  @property
154  def parameters(self):
155  return self.params
156 
157  @parameters.setter
158  def parameters(self,params):
159  self.params = params
160 
161 
162 
164  def evaluateSample(self, x_in):
165  raise NotImplementedError("Please Implement this method")
166 
167 
168  def optimize(self):
169  min_val, x_out, error = bo.optimize_categorical(self.evaluateSample,
170  self.categories,
171  self.params)
172 
173  return min_val, x_out, error
174 
175 
176 
177 if __name__ == "__main__":
178  BO = BayesOptContinuous()
179  __value__, __x__, __err__ = BO.optimize()
180  print("Result", __x__)
def optimize(self)
Main function.
def evaluateSample(self, x_in)
Function for testing.
def __init__(self, categories=None)
Let&#39;s define the parameters.
def evaluateSample(self, x_in)
Function for testing.
def __init__(self, n_dim)
Let&#39;s define the parameters.
def __init__(self, x_set=None, n_dim=None, n_samples=None)
Let&#39;s define the parameters.
x_set
Set of discrete points.
Python Module for BayesOptCategorical.
def optimize(self)
Main function.
Python Module for BayesOptDiscrete.
def evaluateSample(self, x_in)
Function for testing.
Python Module for BayesOptContinuous.
def optimize(self)
Main function.