-
Notifications
You must be signed in to change notification settings - Fork 14
/
search.go
151 lines (131 loc) · 3.92 KB
/
search.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
// Copyright (c) 2014-2018 by Michael Dvorkin. All Rights Reserved.
// Use of this source code is governed by a MIT-style license that can
// be found in the LICENSE file.
//
// I am making my contributions/submissions to this project solely in my
// personal capacity and am not conveying any rights to any intellectual
// property of any third parties.
package donna
// Root node search. Basic principle is expressed by Boob's Law: you always find
// something in the last place you look.
func (p *Position) search(alpha, beta, depth int) (score int) {
ply, inCheck := ply(), p.isInCheck(p.color)
// Root move generator makes sure all generated moves are valid. The
// best move found so far is always the first one we search.
gen := NewRootGen(p, depth)
if depth == 1 {
gen.generateRootMoves()
} else {
gen.reset()
}
bestAlpha, bestScore := alpha, alpha
bestMove, moveCount := Move(0), 0
for move := gen.nextMove(); move.some(); move = gen.nextMove() {
position := p.makeMove(move)
moveCount++; game.nodes++
if engine.uci {
engine.uciMove(move, moveCount, depth)
}
// Reduce search depth if we're not checking.
giveCheck := position.isInCheck(position.color)
newDepth := let(giveCheck && p.exchange(move) >= 0, depth, depth - 1)
// Start search with full window.
game.deepening = (moveCount == 1)
if moveCount == 1 {
score = -position.searchTree(-beta, -alpha, newDepth)
} else {
reduction := 0
if !inCheck && !giveCheck && depth > 2 && move.isQuiet() && !move.isKiller(ply) && !move.isPawnAdvance() {
reduction = lateMoveReductions[(moveCount-1) & 63][depth & 63]
if game.history[move.piece()][move.to()] < 0 {
reduction++
}
}
score = -position.searchTree(-alpha - 1, -alpha, max(0, newDepth - reduction))
// Verify late move reduction and re-run the search if necessary.
if reduction > 0 && score > alpha {
score = -position.searchTree(-alpha - 1, -alpha, newDepth)
}
// If zero window fails then try full window.
if score > alpha {
score = -position.searchTree(-beta, -alpha, newDepth)
}
}
position.undoLastMove()
// Don't touch anything if the time has elapsed and we need to abort th search.
if engine.clock.halt {
return alpha
}
if moveCount == 1 || score > alpha {
bestMove = move
game.saveBest(0, move)
gen.scoreMove(depth, score).rearrangeRootMoves()
if moveCount > 1 {
game.volatility++
}
} else {
gen.scoreMove(depth, -depth)
}
if score > bestScore {
bestScore = score
if score > alpha {
game.saveBest(ply, move)
if score < beta {
alpha = score
bestMove = move
} else {
p.cache(move, score, depth, ply, cacheBeta)
if !inCheck && alpha > bestAlpha {
game.saveGood(depth, bestMove).updatePoor(depth, bestMove, gen.reset())
}
return score
}
}
}
}
if moveCount == 0 {
score = let(inCheck, -Checkmate, 0) // Mate if in check, stalemate otherwise.
if engine.uci {
engine.uciScore(depth, score, alpha, beta)
}
return score
}
score = bestScore
if !inCheck && alpha > bestAlpha {
game.saveGood(depth, bestMove).updatePoor(depth, bestMove, gen.reset())
}
cacheFlags := cacheAlpha
if score >= beta {
cacheFlags = cacheBeta
} else if bestMove.some() {
cacheFlags = cacheExact
}
p.cache(bestMove, score, depth, ply, cacheFlags)
if engine.uci {
engine.uciScore(depth, score, alpha, beta)
}
return
}
// Testing helper method to test root search.
func (p *Position) solve(depth int) Move {
if depth != 1 {
NewRootGen(p, 1).generateRootMoves()
}
p.search(-Checkmate, Checkmate, depth)
return game.pv[0].moves[0]
}
func (p *Position) Perft(depth int) (total int64) {
if depth == 0 {
return 1
}
gen := NewGen(p, depth).generateAllMoves()
for move := gen.nextMove(); move != 0; move = gen.nextMove() {
if !move.valid(p, gen.pins) {
continue
}
position := p.makeMove(move)
total += position.Perft(depth - 1)
position.undoLastMove()
}
return
}