-
Notifications
You must be signed in to change notification settings - Fork 2
/
eval_tree.go
170 lines (159 loc) · 3.23 KB
/
eval_tree.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
package chess_engine
type EvalResult struct {
Score
Line []*Move
}
func NewEvalResult(line []*Move, score Score) *EvalResult {
return &EvalResult{
Score: score,
Line: line,
}
}
type EvalTree struct {
Score
Move *Move
Replies map[string]*EvalTree
BestLine *EvalTree
Parent *EvalTree
}
func NewEvalTree(move *Move) *EvalTree {
return &EvalTree{
Score: LowestScore,
Move: move,
Replies: map[string]*EvalTree{},
}
}
func (t *EvalTree) Traverse(line []*Move) *EvalTree {
tree := t
for _, m := range line {
if child, ok := tree.Replies[m.String()]; ok {
tree = child
} else {
return nil
}
}
return tree
}
func (t *EvalTree) GetScore(line []*Move) (Score, bool) {
tree := t.Traverse(line)
if tree != nil {
return tree.Score, true
}
return 0.0, false
}
func (t *EvalTree) UpdateBestLine() {
var maxChild *EvalTree
maxScore := LowestScore
//fmt.Println("Finding max for", t.Move)
for _, child := range t.Replies {
//fmt.Println("Seen", child.Move, child.Score)
if child.Score >= maxScore {
maxScore = child.Score
maxChild = child
}
}
//fmt.Println("Found", maxChild.Move, maxScore)
if t.Parent != nil {
//fmt.Println("[1] setting score", maxScore*-1, t.Move)
t.Score = maxScore * -1
} else {
//fmt.Println("[2] setting score", maxScore, t.Move)
t.Score = maxScore
}
if maxChild != nil {
t.BestLine = maxChild
}
if t.Parent != nil {
//fmt.Println("update parent line", t.Move, maxScore)
t.Parent.UpdateBestLine()
} else {
//fmt.Println("set root to", maxScore, t.BestLine.Move)
}
}
func (t *EvalTree) GetLine(line []*Move) *EvalTree {
tree := t
for _, move := range line {
child, ok := tree.Replies[move.String()]
if !ok {
return nil
}
tree = child
}
return tree
}
func (t *EvalTree) Insert(line []*Move, score Score) {
//fmt.Println("Insert", line, score)
tree := t
var calcScoreOn *EvalTree
for _, move := range line {
moveStr := move.String()
next, ok := tree.Replies[moveStr]
if !ok {
if calcScoreOn == nil {
calcScoreOn = tree
}
tree.Replies[moveStr] = NewEvalTree(move)
tree.Replies[moveStr].Parent = tree
if tree.BestLine == nil {
tree.BestLine = tree.Replies[moveStr]
}
next = tree.Replies[moveStr]
}
tree = next
}
if len(tree.Replies) == 0 {
tree.Score = score
}
if tree.Parent != nil {
tree.Parent.UpdateBestLine()
}
}
func (t *EvalTree) GetBestLine() *EvalResult {
bestLine := []*Move{}
tree := t
for tree.BestLine != nil {
if tree.Move != nil {
bestLine = append(bestLine, tree.Move)
}
tree = tree.BestLine
}
if tree.Move != nil {
bestLine = append(bestLine, tree.Move)
}
return NewEvalResult(bestLine, tree.Score)
}
func (t *EvalTree) Prune() {
if t.BestLine == nil {
return
}
if len(t.Replies) <= 1 {
for _, reply := range t.Replies {
reply.Prune()
}
return
}
i := 0
toDelete := make([]string, len(t.Replies)-1)
for move, child := range t.Replies {
if child != t.BestLine {
toDelete[i] = move
i++
}
}
for _, m := range toDelete {
delete(t.Replies, m)
}
for _, reply := range t.Replies {
reply.Prune()
}
}
func (t *EvalTree) MaxDepth() int {
depth := 0
for _, reply := range t.Replies {
max := reply.MaxDepth()
if max > depth {
depth = max
}
}
return depth + 1
}