-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathexploration.go
73 lines (63 loc) · 1.66 KB
/
exploration.go
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
64
65
66
67
68
69
70
71
72
73
package bayesopt
import (
"math"
"github.com/d4l3k/go-bayesopt/gp"
)
// Exploration is the strategy to use for exploring the Gaussian process.
type Exploration interface {
Estimate(gp *gp.GP, minimize bool, x []float64) (float64, error)
}
// UCB implements upper confidence bound exploration.
type UCB struct {
Kappa float64
}
// Estimate implements Exploration.
func (e UCB) Estimate(gp *gp.GP, minimize bool, x []float64) (float64, error) {
mean, sd, err := gp.Estimate(x)
if err != nil {
return 0, err
}
if minimize {
return mean - e.Kappa*sd, nil
}
return mean + e.Kappa*sd, nil
}
// BarrierFunc returns a value that is added to the value to bound the
// optimization.
type BarrierFunc interface {
Val(x []float64, params []Param) float64
Grad(x []float64, params []Param) []float64
}
// BasicBarrier returns -Inf if an x value is outside the param range.
func BasicBarrier(x []float64, params []Param) float64 {
for i, p := range params {
v := x[i]
if v < p.GetMin() || v > p.GetMax() {
return math.Inf(-1)
}
}
return 0
}
// LogBarrier implements a logarithmic barrier function.
type LogBarrier struct{}
// Val returns the value of the barrier function.
func (LogBarrier) Val(x []float64, params []Param) float64 {
v := 0.0
for i, p := range params {
v += math.Log2(p.GetMax() - x[i])
v += math.Log2(x[i] - p.GetMin())
}
if math.IsNaN(v) {
return math.Inf(-1)
}
return v
}
// Grad returns the gradient of the barrier function.
func (LogBarrier) Grad(x []float64, params []Param) []float64 {
grad := make([]float64, len(x))
for i, p := range params {
grad[i] = 1/(x[i]-p.GetMin()) - 1/(p.GetMax()-x[i])
// TODO handle NaN
}
return grad
}