forked from 3XX0/pooly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
softmax_test.go
93 lines (81 loc) · 1.83 KB
/
softmax_test.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
93
// +build montecarlo_simulation
package pooly
import (
"code.google.com/p/plotinum/plot"
"code.google.com/p/plotinum/plotter"
"image/color"
"strconv"
"testing"
"time"
)
func TestServiceSoftMax(t *testing.T) {
var (
horizon = 250
temperatures = []float32{0.1, 0.5, 0.9}
hosts = map[string]bernouilliExperiment{
echo1: 0.1,
echo2: 0.1,
echo3: 0.9,
}
)
e1 := newEchoServer(t, echo1)
defer e1.close()
e2 := newEchoServer(t, echo2)
defer e2.close()
e3 := newEchoServer(t, echo3)
defer e3.close()
p, err := plot.New()
if err != nil {
t.Fatal(err)
}
p.Add(plotter.NewGrid())
p.X.Label.Text = "trials"
p.Y.Label.Text = "average score"
p.X.Max = float64(horizon)
p.Y.Min = 0
p.Y.Max = 1
for _, tp := range temperatures {
means := make(plotter.XYs, horizon)
s := NewService("echo", &ServiceConfig{
BanditStrategy: NewSoftMax(tp),
MemoizeScoreDuration: 1 * time.Millisecond,
})
s.Add(echo1)
s.Add(echo2)
s.Add(echo3)
time.Sleep(1 * time.Millisecond) // wait for propagation
for i := 0; i < horizon; i++ {
c, err := s.GetConn()
if err != nil {
t.Error(err)
continue
}
if i == 0 {
means[i].X = 0
means[i].Y = c.host.Score()
} else {
n := means[i-1].X + 1
m := means[i-1].Y
means[i].X = float64(i)
means[i].Y = m + (c.host.Score()-m)/(n+1)
}
a := c.Address()
if err := c.Release(nil, hosts[a].trial()); err != nil {
t.Error(err)
continue
}
time.Sleep(1 * time.Millisecond) // wait for memoization
}
s.Close()
l, err := plotter.NewLine(means)
if err != nil {
t.Fatal(err)
}
l.LineStyle.Color = color.RGBA{G: uint8(255 * tp), A: 255}
p.Legend.Add("temperature "+strconv.FormatFloat(float64(tp), 'f', 1, 32), l)
p.Add(l)
}
if err := p.Save(7, 7, "softmax_test.svg"); err != nil {
t.Fatal(err)
}
}