-
Notifications
You must be signed in to change notification settings - Fork 535
/
guessandcheck.go
92 lines (79 loc) · 1.97 KB
/
guessandcheck.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright ©2016 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package optimize
import (
"math"
"gonum.org/v1/gonum/stat/distmv"
)
var _ Method = (*GuessAndCheck)(nil)
// GuessAndCheck is a global optimizer that evaluates the function at random
// locations. Not a good optimizer, but useful for comparison and debugging.
type GuessAndCheck struct {
Rander distmv.Rander
bestF float64
bestX []float64
}
func (*GuessAndCheck) Uses(has Available) (uses Available, err error) {
return has.function()
}
func (g *GuessAndCheck) Init(dim, tasks int) int {
if dim <= 0 {
panic(nonpositiveDimension)
}
if tasks < 0 {
panic(negativeTasks)
}
g.bestF = math.Inf(1)
g.bestX = resize(g.bestX, dim)
return tasks
}
func (g *GuessAndCheck) sendNewLoc(operation chan<- Task, task Task) {
g.Rander.Rand(task.X)
task.Op = FuncEvaluation
operation <- task
}
func (g *GuessAndCheck) updateMajor(operation chan<- Task, task Task) {
// Update the best value seen so far, and send a MajorIteration.
if task.F < g.bestF {
g.bestF = task.F
copy(g.bestX, task.X)
} else {
task.F = g.bestF
copy(task.X, g.bestX)
}
task.Op = MajorIteration
operation <- task
}
func (g *GuessAndCheck) Run(operation chan<- Task, result <-chan Task, tasks []Task) {
// Send initial tasks to evaluate
for _, task := range tasks {
g.sendNewLoc(operation, task)
}
// Read from the channel until PostIteration is sent.
Loop:
for {
task := <-result
switch task.Op {
default:
panic("unknown operation")
case PostIteration:
break Loop
case MajorIteration:
g.sendNewLoc(operation, task)
case FuncEvaluation:
g.updateMajor(operation, task)
}
}
// PostIteration was sent. Update the best new values.
for task := range result {
switch task.Op {
default:
panic("unknown operation")
case MajorIteration:
case FuncEvaluation:
g.updateMajor(operation, task)
}
}
close(operation)
}