-
Notifications
You must be signed in to change notification settings - Fork 1
/
mcts.go
453 lines (418 loc) · 11 KB
/
mcts.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package hexz
import (
"fmt"
"log"
"math"
"strings"
"time"
"github.com/dnswlt/hexz/xrand"
)
// Nodes of the MCTS search tree.
type mcNode struct {
wins int32
count int32
// bit-encoding of several values [i:j], j exclusive):
// [ r[0:8], c[8:16], turn[16], cellType[17:21] ]
bits uint32
children []mcNode
}
func (n *mcNode) set(r, c int, turn int, cellType CellType) {
n.bits = uint32(r) | (uint32(c) << 8) | (uint32(turn>>1) << 16) | (uint32(cellType) << 17)
}
func (n *mcNode) incr(winner int) {
n.count++
if winner == n.turn() {
n.wins++
} else if winner != 0 {
// Other player won.
n.wins--
}
// Do nothing on a draw (count it as 0).
}
func (n *mcNode) r() int {
return int(n.bits & 0xff)
}
func (n *mcNode) c() int {
return int((n.bits >> 8) & 0xff)
}
func (n *mcNode) turn() int {
if n.bits&(1<<16) != 0 {
return 2
}
return 1
}
func (n *mcNode) cellType() CellType {
return CellType((n.bits >> 17) & 0xf)
}
func (n *mcNode) String() string {
return fmt.Sprintf("(%d,%d/%d) #cs:%d, wins:%d count:%d, turn:%d",
n.r(), n.c(), n.cellType(), len(n.children), n.wins, n.count, n.turn())
}
func (n *mcNode) Q() float64 {
if n.count == 0 {
return 0
}
return 0.5 + float64(n.wins)/float64(n.count)/2
}
// Tabulating logs showed a significant performance gain on amd64.
//
// go test -bench BenchmarkMCTSRun -run ^$ -count=10
//
// dnswlt/hexz$ benchstat old.txt new.txt
// goos: darwin
// goarch: arm64
// pkg: github.com/dnswlt/hexz
//
// │ old.txt │ new.txt │
// │ sec/op │ sec/op vs base │
//
// MCTSRun-10 25.17µ ± 2% 23.52µ ± 1% -6.55% (p=0.000 n=10)
const useTabulatedLogs = true
const logValuesSize = 100_000
var logValues [logValuesSize]float64
func init() {
for i := 1; i < logValuesSize; i++ {
logValues[i] = math.Log(float64(i))
}
}
var EnableInitialDrawAssumption = true
func (n *mcNode) U(parentCount int32, uctFactor float64) float64 {
if !EnableInitialDrawAssumption && n.count == 0 {
return math.MaxFloat64
}
if parentCount == 0 {
// First rollout for the parent. Assume this game was a draw.
return 0.5
}
var l float64
if useTabulatedLogs && parentCount < logValuesSize {
l = logValues[parentCount]
} else {
l = math.Log(float64(parentCount))
}
if n.count == 0 {
// Never played => assume one game was played and it was a draw.
return 0.5 + uctFactor*math.Sqrt(l)
}
return n.Q() + uctFactor*math.Sqrt(l/float64(n.count))
}
// Returns the number of leaf and branch nodes on each depth level, starting from 0 for the root.
// The depth of the tree can be computed as len(leafNodes).
func (root *mcNode) nodesPerDepth() (size int, leafNodes []int, branchNodes []int, visitCounts []map[int]int) {
type ni struct {
n *mcNode
d int
}
q := make([]ni, 1, 1024)
q[0] = ni{root, 0}
for len(q) > 0 {
n := q[len(q)-1]
q = q[:len(q)-1]
size++
if len(leafNodes) == n.d {
leafNodes = append(leafNodes, 0)
branchNodes = append(branchNodes, 0)
visitCounts = append(visitCounts, make(map[int]int))
}
if len(n.n.children) == 0 {
leafNodes[n.d]++
} else {
branchNodes[n.d]++
}
visitCounts[n.d][int(n.n.count)]++
for i := range n.n.children {
q = append(q, ni{&n.n.children[i], n.d + 1})
}
}
return
}
type MCTS struct {
UctFactor float64
// If true, SuggestMove returns the most frequently vistited child node,
// not the one with the highest win rate.
ReturnMostFrequentlyVisited bool
// Sample boards that led to a win/loss.
WinningBoard *Board
LosingBoard *Board
// For explicit memory handling.
Mem []mcNode
Next int
}
func (mcts *MCTS) playRandomGame(ge *GameEngineFlagz) (winner int) {
for !ge.IsDone() {
m, err := ge.RandomMove()
if err != nil {
log.Fatalf("Could not suggest a move: %s", err.Error())
}
if !ge.MakeMove(m) {
log.Fatalf("Could not make a move")
return
}
}
return ge.Winner()
}
func (mcts *MCTS) getNextByUtc(node *mcNode) *mcNode {
var next *mcNode
maxUct := -1.0
for i := range node.children {
l := &node.children[i]
uct := l.U(node.count, mcts.UctFactor)
if uct > maxUct {
next = l
maxUct = uct
}
}
return next
}
func (mcts *MCTS) nextMoves(ge *GameEngineFlagz) []mcNode {
b := ge.B
hasFlag := b.Resources[b.Turn-1].NumPieces[cellFlag] > 0
nChildren := ge.NormalMoves[b.Turn-1]
if hasFlag {
nChildren += ge.FreeCells
}
var cs []mcNode
if mcts.Mem != nil {
cs = mcts.Mem[mcts.Next : mcts.Next+nChildren]
mcts.Next += nChildren
} else {
cs = make([]mcNode, nChildren)
}
i := 0
for r := 0; r < len(b.Fields); r++ {
for c := 0; c < len(b.Fields[r]); c++ {
f := &b.Fields[r][c]
if f.occupied() {
continue
}
if f.isAvail(b.Turn) {
cs[i].set(r, c, b.Turn, cellNormal)
i++
}
if hasFlag {
cs[i].set(r, c, b.Turn, cellFlag)
i++
}
}
}
if len(cs) != i {
// If this happens, our memory allocation above is wrong.
panic(fmt.Sprintf("Wrong number of next moves: %d != %d+%d", len(cs), ge.FreeCells, ge.NormalMoves[b.Turn-1]))
}
return cs
}
func (mcts *MCTS) run(ge *GameEngineFlagz, node *mcNode) (winner int) {
if ge.IsDone() {
panic("Called Run() on a finished game")
}
return mcts.runInternal(ge, node, 0)
}
func (mcts *MCTS) runInternal(ge *GameEngineFlagz, node *mcNode, curDepth int) (winner int) {
b := ge.Board()
if node.children == nil {
// Terminal node in our exploration graph, but not in the whole game: rollout time!
cs := mcts.nextMoves(ge)
if len(cs) == 0 {
panic("No next moves, but game is not over yet")
}
node.children = cs
// Play a random child (rollout)
c := &cs[xrand.Intn(len(cs))]
move := GameEngineMove{
PlayerNum: c.turn(), Move: b.Move, Row: c.r(), Col: c.c(), CellType: c.cellType(),
}
if !ge.MakeMove(move) {
panic(fmt.Sprintf("Failed to make move for rollout: %s", move.String()))
}
winner = mcts.playRandomGame(ge)
if winner == 1 && mcts.WinningBoard == nil {
mcts.WinningBoard = b.Copy()
} else if winner == 2 && mcts.LosingBoard == nil {
mcts.LosingBoard = b.Copy()
}
// Record counts and wins for child.
c.incr(winner)
} else {
// Node has children already, descend to the one with the highest UTC.
c := mcts.getNextByUtc(node)
move := GameEngineMove{
PlayerNum: c.turn(), Move: b.Move, Row: c.r(), Col: c.c(), CellType: c.cellType(),
}
if !ge.MakeMove(move) {
panic(fmt.Sprintf("Failed to make move during tree descent: %s", move.String()))
}
if ge.IsDone() {
// This was the last move. Update child stats.
winner = ge.Winner()
c.incr(winner)
} else {
// Not done: descend to next level
winner = mcts.runInternal(ge, c, curDepth+1)
}
}
node.incr(winner)
return
}
type MCTSMoveStats struct {
Row int
Col int
CellType CellType
U float64
Q float64
Iterations int
}
type MCTSStats struct {
Iterations int
MaxDepth int
TreeSize int
LeafNodes []int // Per depth level, 0=root
BranchNodes []int // Per depth level, 0=root
VisitCounts []map[int]int // Per depth level, maps visit count to number of nodes with that count.
Elapsed time.Duration
Moves []MCTSMoveStats
BestMoveQ float64
}
func (s *MCTSStats) MinQ() float64 {
r := math.Inf(1)
for _, c := range s.Moves {
if c.Q < r {
r = c.Q
}
}
return r
}
func (s *MCTSStats) MaxQ() float64 {
r := 0.0
for _, c := range s.Moves {
if c.Q > r {
r = c.Q
}
}
return r
}
func (s *MCTSStats) MoveScores() *MoveScores {
normalCell := make([][]float64, numBoardRows)
flag := make([][]float64, numBoardRows)
for i := 0; i < numBoardRows; i++ {
nCols := numFieldsFirstRow - i%2
normalCell[i] = make([]float64, nCols)
flag[i] = make([]float64, nCols)
}
for _, m := range s.Moves {
switch m.CellType {
case cellNormal:
normalCell[m.Row][m.Col] = m.Q
case cellFlag:
flag[m.Row][m.Col] = m.Q
}
}
return &MoveScores{
NormalCell: normalCell,
Flag: flag,
}
}
func (s *MCTSStats) String() string {
var sb strings.Builder
fmt.Fprintf(&sb, "N: %d\nmaxDepth:%d\nsize:%d\nelapsed:%.3f\nN/sec:%.1f\n",
s.Iterations, s.MaxDepth, s.TreeSize, s.Elapsed.Seconds(), float64(s.Iterations)/s.Elapsed.Seconds())
for _, m := range s.Moves {
cellType := ""
if m.CellType == cellFlag {
cellType = " F"
}
fmt.Fprintf(&sb, " (%d,%d%s) U:%.3f Q:%.2f N:%d\n", m.Row, m.Col, cellType, m.U, m.Q, m.Iterations)
}
return sb.String()
}
func NewMCTS() *MCTS {
return &MCTS{
UctFactor: 1.0,
// Returning the node with the highest number of visits is the "standard approach",
// https://ai.stackexchange.com/questions/16905/mcts-how-to-choose-the-final-action-from-the-root
ReturnMostFrequentlyVisited: true,
}
}
func NewMCTSWithMem(cap int) *MCTS {
return &MCTS{
UctFactor: 1.0,
Mem: make([]mcNode, cap),
Next: 0,
}
}
func (mcts *MCTS) bestNextMoveWithStats(root *mcNode, elapsed time.Duration, move int) (GameEngineMove, *MCTSStats) {
size, leafNodes, branchNodes, visitCounts := root.nodesPerDepth()
moves := make([]MCTSMoveStats, len(root.children))
best := &root.children[0]
for i := range root.children {
c := &root.children[i]
if !mcts.ReturnMostFrequentlyVisited && c.Q() > best.Q() {
best = c
} else if mcts.ReturnMostFrequentlyVisited && c.count > best.count {
best = c
}
moves[i] = MCTSMoveStats{
Row: c.r(),
Col: c.c(),
CellType: c.cellType(),
Iterations: int(c.count),
U: c.U(root.count, mcts.UctFactor),
Q: c.Q(),
}
}
stats := &MCTSStats{
Iterations: int(root.count),
MaxDepth: len(leafNodes),
Elapsed: elapsed,
TreeSize: size,
LeafNodes: leafNodes,
BranchNodes: branchNodes,
VisitCounts: visitCounts,
Moves: moves,
BestMoveQ: best.Q(),
}
m := GameEngineMove{
PlayerNum: best.turn(),
Move: move,
Row: best.r(),
Col: best.c(),
CellType: best.cellType(),
}
return m, stats
}
func (mcts *MCTS) Reset() {
if mcts.Mem != nil {
mcts.Next = 0
}
mcts.WinningBoard = nil
mcts.LosingBoard = nil
}
func (mcts *MCTS) SuggestMove(gameEngine *GameEngineFlagz, maxDuration time.Duration) (GameEngineMove, *MCTSStats) {
mcts.Reset()
root := &mcNode{}
root.set(0, 0, gameEngine.Board().Turn, cellNormal) // Dummy values, only the turn matters.
started := time.Now()
ge := gameEngine.Clone()
for n := 0; ; n++ {
// Check every N rounds if we're done. Run at least once.
if (n-1)&63 == 0 && time.Since(started) >= maxDuration {
break
}
ge.copyFrom(gameEngine)
mcts.run(ge, root)
}
elapsed := time.Since(started)
return mcts.bestNextMoveWithStats(root, elapsed, gameEngine.Board().Move)
}
func (mcts *MCTS) SuggestMoveLimit(gameEngine *GameEngineFlagz, maxIterations int) (GameEngineMove, *MCTSStats) {
mcts.Reset()
root := &mcNode{}
root.set(0, 0, gameEngine.Board().Turn, cellNormal) // Dummy values, only the turn matters.
started := time.Now()
ge := gameEngine.Clone()
for n := 0; n < maxIterations; n++ {
ge.copyFrom(gameEngine)
mcts.run(ge, root)
}
elapsed := time.Since(started)
return mcts.bestNextMoveWithStats(root, elapsed, gameEngine.Board().Move)
}