-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathengine.go
More file actions
189 lines (169 loc) · 5.09 KB
/
engine.go
File metadata and controls
189 lines (169 loc) · 5.09 KB
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
package chess_engine
import (
"context"
"fmt"
"time"
)
type BSEngine struct {
StartingPosition *Game
Cancel context.CancelFunc
Evaluators Evaluators
EvalTree *EvalTree
SelDepth int
TotalNodes int
NodesPerSecond int
CurrentDepth int
Seen SeenMap
Queue *Queue
}
func NewBSEngine(depth int) *BSEngine {
return &BSEngine{
SelDepth: depth,
}
}
func (b *BSEngine) GetPosition() *Game {
return b.StartingPosition
}
func (b *BSEngine) SetPosition(fen *Game) {
b.StartingPosition = fen
}
func (b *BSEngine) SetOption(opt EngineOption, val int) {
if opt == SELDEPTH {
b.SelDepth = val
}
}
func (b *BSEngine) Start(output chan string, maxNodes, maxDepth int) {
ctx, cancel := context.WithCancel(context.Background())
b.Cancel = cancel
go b.start(ctx, output, maxNodes, maxDepth)
}
func (b *BSEngine) start(ctx context.Context, output chan string, maxNodes, maxDepth int) {
b.Seen = NewSeenMap()
b.EvalTree = NewEvalTree(nil)
b.NodesPerSecond = 0
b.TotalNodes = 0
b.Queue = NewQueue()
timer := time.NewTimer(time.Second)
//depth := b.SelDepth + 1
b.Queue.QueueNextLine(b.StartingPosition, b.Seen, b.SelDepth, b.Evaluators)
for {
select {
case <-ctx.Done():
b.outputInfo(output, true)
return
case <-timer.C:
b.TotalNodes += b.NodesPerSecond
b.NodesPerSecond = 0
timer = time.NewTimer(time.Second)
b.outputInfo(output, false)
default:
if maxNodes > 0 && b.TotalNodes+b.NodesPerSecond >= maxNodes {
b.outputInfo(output, true)
return
}
if !b.Queue.IsEmpty() {
game := b.Queue.GetNextGame()
if game == nil {
panic("game nil")
}
if game.Score == nil {
b.Evaluators.Eval(game)
}
if *game.Score == Mate {
*game.Score = *game.Score - Score(float64(len(game.Line)))
}
b.EvalTree.Insert(game.Line, *game.Score)
if len(game.Line) == 0 || len(game.Line) == b.SelDepth {
b.EvalTree.UpdateBestLine()
//if b.EvalTree.Score == Mate {
// b.outputInfo(output, true)
// return
//}
} else if len(game.Line) < b.SelDepth {
// If we already found Mate at this depth we can skip
// this whole tree
if b.EvalTree.Score.IsMateInNOrBetter(len(game.Line)) {
continue
}
debug := false
// Check if the score difference between this line
// and the parent is not too big. If it is we should
// consider some alternative moves.
tree := b.EvalTree.Traverse(game.Line[:len(game.Line)])
queuedForcingLines := b.Queue.QueueForcingLines(game, b.Seen, b.SelDepth-len(game.Line), b.Evaluators)
// The root position is already looked after
if len(game.Line) == 1 || queuedForcingLines {
continue
}
if tree != nil && tree.Parent != nil && tree.Parent.Move != nil {
// Positive diff means that the move is winning
// Negative diff means that the move is losing
ts := tree.Score
if ts < 0 {
ts *= -1
}
gs := *game.Parent.Score // <- not technically necessarily the score we should be looking at?
if gs < 0 {
gs *= -1
}
if ts > gs {
gs, ts = ts, gs
}
diff := gs - ts
if debug {
fmt.Println(diff)
}
if float64(diff) > 200 { // if blunder / major gain
// So we should have moved something differently
// before. Queue the next line from the parent's parent.
//fmt.Println("Major loss for", game.ToMove.Opposite(), game.Line, diff, tree.Score, *game.Parent.Score)
if b.Queue.QueueNextLine(game.Parent, b.Seen, b.SelDepth-len(game.Parent.Line), b.Evaluators) {
}
}
}
}
} else {
// The queue is empty so there are no more moves to look at.
// However we can queue more moves if it turns out our current
// best move leads to a worse position than what we started with.
firstScore := *b.StartingPosition.Score * -1
if b.EvalTree.BestLine == nil || firstScore > b.EvalTree.BestLine.Score {
// Queue forcing lines, than queue alternative best moves
//fmt.Println("queue alternative...why?", b.EvalTree.BestLine)
hasNext := b.Queue.QueueNextLine(b.StartingPosition, b.Seen, b.SelDepth, b.Evaluators)
if !hasNext {
//fmt.Println("we are losing")
b.outputInfo(output, true)
return
}
} else {
//fmt.Println("We are better", *b.StartingPosition.Score, b.EvalTree.BestLine.Score)
//fmt.Println(Line(b.EvalTree.BestLine.GetBestLine().Line).String())
// Otherwise output the best move
b.outputInfo(output, true)
return
}
}
}
}
}
func (b *BSEngine) outputInfo(output chan string, sendBestMove bool) {
bestLine := b.EvalTree.BestLine
bestResult := bestLine.GetBestLine()
line := Line(bestResult.Line).String()
output <- fmt.Sprintf("info depth %d ns %d nodes %d score cp %d pv %s",
len(bestResult.Line),
b.NodesPerSecond,
b.TotalNodes,
bestResult.Score.ToCentipawn(),
line)
if sendBestMove {
output <- fmt.Sprintf("bestmove %s", bestLine.Move.String())
}
}
func (b *BSEngine) AddEvaluator(e Evaluator) {
b.Evaluators = append(b.Evaluators, e)
}
func (b *BSEngine) Stop() {
b.Cancel()
}