-
Notifications
You must be signed in to change notification settings - Fork 9
/
stopping_condition.go
244 lines (223 loc) · 6.41 KB
/
stopping_condition.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package montecarlo
import (
"sort"
"github.com/domino14/word-golib/tilemapping"
"github.com/rs/zerolog/log"
"github.com/domino14/macondo/stats"
)
const IterationsCutoff = 2000
const PerPlyStopScaling = 625
const SimilarPlaysIterationsCutoff = 750
const MinReasonableWProb = 0.005 // or 0.5%
// use stats to figure out when to stop simming.
func (s *Simmer) shouldStop(iterationCount uint64,
playSimilarityCache map[string]bool) bool {
// This function runs as the sim is ongoing. So we should be careful
// what we do with memory here.
plays := s.plays
sc := s.stoppingCondition
if len(plays) < 2 {
return true
}
if int(iterationCount) > IterationsCutoff+(s.maxPlies*PerPlyStopScaling) {
return true
}
// Otherwise, do some statistics.
// shallow copy the array so we can sort it/play with it.
c := make([]*SimmedPlay, len(plays))
// count ignored plays
ignoredPlays := 0
bottomUnignoredWinPct := 0.0
for i := range c {
c[i] = plays[i]
c[i].RLock()
if c[i].ignore {
ignoredPlays++
}
c[i].RUnlock()
}
if ignoredPlays >= len(c)-1 {
// if there is only 1 unignored play, exit.
return true
}
// sort copy by win pct.
sort.Slice(c, func(i, j int) bool {
c[i].RLock()
c[j].RLock()
defer c[j].RUnlock()
defer c[i].RUnlock()
if c[i].winPctStats.Mean() == c[j].winPctStats.Mean() {
return c[i].equityStats.Mean() > c[j].equityStats.Mean()
}
return c[i].winPctStats.Mean() > c[j].winPctStats.Mean()
})
// find the bottom unignored win pct play
for i := len(c) - 1; i >= 0; i-- {
c[i].RLock()
if !c[i].ignore {
bottomUnignoredWinPct = c[i].winPctStats.Mean()
c[i].RUnlock()
break
}
c[i].RUnlock()
}
// we want to cut off plays that have no chance of winning.
// assume the very top play is the winner, and then cut off plays that have
// no chance of catching up.
// "no chance" is of course defined by the stopping condition :)
var ci float64
switch sc {
case Stop95:
ci = stats.Z95
case Stop98:
ci = stats.Z98
case Stop99:
ci = stats.Z99
}
tiebreakByEquity := false
tentativeWinner := c[0]
tentativeWinner.RLock()
μ := tentativeWinner.winPctStats.Mean()
e := tentativeWinner.winPctStats.StandardError(ci)
if μ <= MinReasonableWProb {
// If the top play by win % has basically no win chance, tiebreak the whole
// thing by equity.
tiebreakByEquity = true
} else if μ >= (1-MinReasonableWProb) && bottomUnignoredWinPct >= (1-MinReasonableWProb) {
// If the top play by win % has basically no losing chance, check if the bottom
// play also has no losing chance
tiebreakByEquity = true
}
tentativeWinner.RUnlock()
if tiebreakByEquity {
// We may need to re-determine the tentative winner.
highestEquity := -1000000.0
highestEquityIdx := -1
for idx, p := range c {
p.RLock()
eq := p.equityStats.Mean()
if eq > highestEquity {
highestEquityIdx = idx
highestEquity = eq
}
p.RUnlock()
}
if highestEquityIdx != 0 {
c[0], c[highestEquityIdx] = c[highestEquityIdx], c[0]
tentativeWinner = c[0]
log.Info().
Str("old-tentative-winner", c[highestEquityIdx].play.ShortDescription()).
Str("tentative-winner", tentativeWinner.play.ShortDescription()).
Msg("tiebreaking by equity, re-determining tentative winner")
}
μ = tentativeWinner.equityStats.Mean()
e = tentativeWinner.equityStats.StandardError(ci)
log.Debug().Msg("stopping-condition-tiebreak-by-equity")
}
newIgnored := 0
// assume standard normal distribution (?)
for _, p := range c[1:] {
p.RLock()
if p.ignore {
p.RUnlock()
continue
}
μi := p.winPctStats.Mean()
ei := p.winPctStats.StandardError(ci)
if tiebreakByEquity {
μi = p.equityStats.Mean()
ei = p.equityStats.StandardError(ci)
}
p.RUnlock()
if passTest(μ, e, μi, ei) {
p.Ignore()
newIgnored++
} else if iterationCount > SimilarPlaysIterationsCutoff {
if materiallySimilar(tentativeWinner, p, playSimilarityCache) {
p.Ignore()
newIgnored++
}
}
}
if newIgnored > 0 {
log.Debug().Int("newIgnored", newIgnored).Msg("sim-cut-off")
}
if ignoredPlays+newIgnored >= len(c)-1 {
// if there is only 1 unignored play, exit.
return true
}
return false
}
// passTest: determine if a random variable X > Y with the given
// confidence level; return true if X > Y.
func passTest(μ, e, μi, ei float64) bool {
// Z := zVal(μ, v, μi, vi)
// X > Y if (μ - e) > (μi + ei)
return (μ - e) > (μi + ei)
}
func materiallySimilar(p1, p2 *SimmedPlay, pcache map[string]bool) bool {
p1ps := p1.play.ShortDescription()
p2ps := p2.play.ShortDescription()
if p1ps > p2ps {
p1ps, p2ps = p2ps, p1ps
}
lookupstr := p1ps + "|" + p2ps
if similar, ok := pcache[lookupstr]; ok {
log.Trace().Str("lookupstr", lookupstr).
Bool("similar", similar).
Msg("in-similarity-cache")
return similar
}
// two plays are "materially similar" if they use the same tiles and
// start at the same square.
p1r, p1c, p1v := p1.play.CoordsAndVertical()
p2r, p2c, p2v := p2.play.CoordsAndVertical()
if !(p1r == p2r && p1c == p2c && p1v == p2v) {
pcache[lookupstr] = false
return false
}
if p1.play.TilesPlayed() != p2.play.TilesPlayed() {
pcache[lookupstr] = false
return false
}
if len(p1.play.Tiles()) != len(p2.play.Tiles()) {
pcache[lookupstr] = false
return false
}
// these plays start at the same square and are the same length.
// do they use the same tiles?
a1 := make([]tilemapping.MachineLetter, len(p1.play.Tiles()))
a2 := make([]tilemapping.MachineLetter, len(p2.play.Tiles()))
copy(a1, p1.play.Tiles())
copy(a2, p2.play.Tiles())
sort.Slice(a1, func(i, j int) bool { return a1[i] < a1[j] })
sort.Slice(a2, func(i, j int) bool { return a2[i] < a2[j] })
for i := range a1 {
if a1[i] != a2[i] {
pcache[lookupstr] = false
return false
}
}
log.Debug().Str("lookupstr", lookupstr).Msg("materially-similar")
pcache[lookupstr] = true
return true
}
// func zVal(μ, v, μi, vi float64) float64 {
// // mean of X - Y = E(X-Y) = E(X) - E(Y)
// mean := μ - μi
// // variance of (X-Y) = V(X) + V(Y)
// variance := v + vi
// stdev := math.Sqrt(variance)
// // P(X > Y) = P(X - Y > 0)
// // let D = X - Y
// // then P(D > 0)
// // convert to standard normal variable (mean 0 stdev 1)
// // = P ((D - mean) / (stdev) > (0 - mean) / stdev)
// // then P(Z>(0 - mean)/stdev)
// // 95 percentile is Z 1.96
// // 99 percentile is Z 2.58
// return -mean / stdev
// }
// func zValStdev(μ, s, μi, si float64) float64 {
// return zVal(μ, s*s, μi, si*si)
// }