-
Notifications
You must be signed in to change notification settings - Fork 8
/
bounds.go
77 lines (63 loc) · 1.34 KB
/
bounds.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
74
75
76
77
package bayesopt
import (
"github.com/pkg/errors"
"gonum.org/v1/gonum/optimize"
)
var _ optimize.Method = BoundsMethod{}
var _ optimize.Statuser = BoundsMethod{}
type BoundsMethod struct {
Method optimize.Method
Bounds []Param
}
func (m BoundsMethod) constrain(loc *optimize.Location) {
if loc == nil {
return
}
for i, param := range m.Bounds {
max := param.GetMax()
min := param.GetMin()
if loc.X[i] > max {
loc.X[i] = max
} else if loc.X[i] < min {
loc.X[i] = min
}
}
}
func (m BoundsMethod) Init(dims, tasks int) int {
return m.Method.Init(dims, tasks)
}
func (m BoundsMethod) Run(operation chan<- optimize.Task, result <-chan optimize.Task, tasks []optimize.Task) {
op := make(chan optimize.Task)
res := make(chan optimize.Task)
go func() {
defer close(res)
for t := range result {
m.constrain(t.Location)
res <- t
}
}()
go func() {
defer close(operation)
for t := range op {
m.constrain(t.Location)
operation <- t
}
}()
for _, t := range tasks {
m.constrain(t.Location)
}
m.Method.Run(op, res, tasks)
}
func (m BoundsMethod) Needs() struct {
Gradient bool
Hessian bool
} {
return m.Method.Needs()
}
func (m BoundsMethod) Status() (optimize.Status, error) {
s, ok := m.Method.(optimize.Statuser)
if ok {
return s.Status()
}
return optimize.NotTerminated, errors.Errorf("not Statuser")
}