This repository has been archived by the owner on Feb 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 261
/
LCB.py
63 lines (53 loc) · 2.53 KB
/
LCB.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Copyright (c) 2016, the GPyOpt Authors
# Licensed under the BSD 3-clause license (see LICENSE.txt)
from .base import AcquisitionBase
from ..util.general import get_quantiles
from ..core.task.cost import constant_cost_withGradients
class AcquisitionLCB(AcquisitionBase):
"""
GP-Lower Confidence Bound acquisition function
:param model: GPyOpt class of model
:param space: GPyOpt class of domain
:param optimizer: optimizer of the acquisition. Should be a GPyOpt optimizer
:param cost_withGradients: function
:param exploration_weight: positive parameter to comtrol exploration/explotitation
.. Note:: allows to compute the Improvement per unit of cost
"""
def __init__(self, model, space, optimizer=None, cost_withGradients=None, exploration_weight=2):
self.optimizer = optimizer
super(AcquisitionLCB, self).__init__(model, space, optimizer)
self.exploration_weight = exploration_weight
if cost_withGradients == None:
self.cost_withGradients = constant_cost_withGradients
else:
print('LBC acquisition does now make sense with cost. Cost set to constant.')
self.cost_withGradients = constant_cost_withGradients
def _compute_acq(self, m, s, x):
"""
Computes the GP-Lower Confidence Bound per unit of cost
"""
f_acqu = -m + self.exploration_weight * s
cost_x, _ = self.cost_withGradients(x)
return -(f_acqu*self.space.indicator_constrains(x))/cost_x
def acquisition_function(self,x):
"""
GP-Lower Confidence Bound
"""
m, s = self.model.predict(x)
return self._compute_acq(m, s, x) # note: returns negative value for posterior minimization
def acquisition_function_withGradients(self, x):
"""
Computes the GP-Lower Confidence Bound and its derivative (has a very easy derivative!)
"""
m, s, dmdx, dsdx = self.model.predict_withGradients(x)
return self._compute_acq_withGradients(m, s, dmdx, dsdx, x)
def _compute_acq_withGradients(self, m, s, dmdx, dsdx, x):
"""
GP-Lower Confidence Bound and its derivative
"""
f_acqu = -m + self.exploration_weight * s
df_acqu = -dmdx + self.exploration_weight * dsdx
cost_x, cost_grad_x = self.cost_withGradients(x)
f_acq_cost = f_acqu/cost_x
df_acq_cost = (df_acqu*cost_x - f_acqu*cost_grad_x)/(cost_x**2)
return -f_acq_cost*self.space.indicator_constrains(x), -df_acq_cost*self.space.indicator_constrains(x)