-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathvanilla.go
117 lines (101 loc) · 3.07 KB
/
vanilla.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
package cfr
import (
"github.com/timpalpant/go-cfr/internal/f32"
)
type CFR struct {
strategyProfile StrategyProfile
slicePool *floatSlicePool
}
func New(strategyProfile StrategyProfile) *CFR {
return &CFR{
strategyProfile: strategyProfile,
slicePool: &floatSlicePool{},
}
}
func (c *CFR) Run(node GameTreeNode) float32 {
return c.runHelper(node, node.Player(), 1.0, 1.0, 1.0)
}
func (c *CFR) runHelper(node GameTreeNode, lastPlayer int, reachP0, reachP1, reachChance float32) float32 {
var ev float32
switch node.Type() {
case TerminalNodeType:
ev = float32(node.Utility(lastPlayer))
case ChanceNodeType:
ev = c.handleChanceNode(node, lastPlayer, reachP0, reachP1, reachChance)
default:
sgn := getSign(lastPlayer, node.Player())
ev = sgn * c.handlePlayerNode(node, reachP0, reachP1, reachChance)
}
node.Close()
return ev
}
func (c *CFR) handleChanceNode(node GameTreeNode, lastPlayer int, reachP0, reachP1, reachChance float32) float32 {
var expectedValue float32
for i := 0; i < node.NumChildren(); i++ {
child := node.GetChild(i)
p := float32(node.GetChildProbability(i))
expectedValue += p * c.runHelper(child, lastPlayer, reachP0, reachP1, reachChance*p)
}
return expectedValue
}
func (c *CFR) handlePlayerNode(node GameTreeNode, reachP0, reachP1, reachChance float32) float32 {
player := node.Player()
nChildren := node.NumChildren()
if nChildren == 1 {
// Optimization to skip trivial nodes with no real choice.
child := node.GetChild(0)
return c.runHelper(child, player, reachP0, reachP1, reachChance)
}
policy := c.strategyProfile.GetPolicy(node)
strategy := policy.GetStrategy()
regrets := c.slicePool.alloc(nChildren)
defer c.slicePool.free(regrets)
var cfValue float32
for i := 0; i < nChildren; i++ {
child := node.GetChild(i)
p := strategy[i]
var util float32
if player == 0 {
util = c.runHelper(child, player, p*reachP0, reachP1, reachChance)
} else {
util = c.runHelper(child, player, reachP0, p*reachP1, reachChance)
}
regrets[i] = util
cfValue += p * util
}
// Transform action utilities into instantaneous regrets by
// subtracting out the expected utility over all possible actions.
f32.AddConst(-cfValue, regrets)
counterFactualP := counterFactualProb(player, reachP0, reachP1, reachChance)
ones := c.slicePool.alloc(nChildren)
defer c.slicePool.free(ones)
for i := range ones {
ones[i] = 1.0
}
policy.AddRegret(counterFactualP, ones, regrets)
reachP := reachProb(player, reachP0, reachP1, reachChance)
policy.AddStrategyWeight(reachP)
return cfValue
}
func getSign(player1, player2 int) float32 {
if player1 == player2 {
return 1.0
}
return -1.0
}
func reachProb(player int, reachP0, reachP1, reachChance float32) float32 {
if player == 0 {
return reachP0 * reachChance
} else {
return reachP1 * reachChance
}
}
// The probability of reaching this node, assuming that the current player
// tried to reach it.
func counterFactualProb(player int, reachP0, reachP1, reachChance float32) float32 {
if player == 0 {
return reachP1 * reachChance
} else {
return reachP0 * reachChance
}
}