-
Notifications
You must be signed in to change notification settings - Fork 1
/
search.go
228 lines (218 loc) · 5.17 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
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
package main
import (
"fmt"
"log"
"time"
)
type SearchInfo struct {
startTime time.Time
stopTime time.Time
depth int
depthSet int
timeSet bool
movesToGo int
infinite bool
nodes uint64
quit bool
stopped bool
fh float32 //fail high
fhf float32 // fail high first
}
// checkUp checks if time is up or there is an interrupt from GUI
func checkUp(info *SearchInfo) {
if info.timeSet && info.stopTime.Before(time.Now()) {
info.stopped = true
}
}
// isRepetition checks if current board structure is repetition of some previous board structure
func isRepetition(pos *Board) bool {
for index := pos.HisPly - pos.FiftyMove; index < pos.HisPly-1; index++ {
if index < 0 || index >= MaxGameMoves {
fmt.Println("Error in finding repetition")
return false
}
if pos.PosKey == pos.History[index].PosKey {
return true
}
}
return false
}
// ClearForSearch clears the board and searchInfo before searching
func ClearForSearch(pos *Board, info *SearchInfo) {
for i := 0; i < 13; i++ {
for j := 0; j < BrdSqNum; j++ {
pos.SearchHistory[i][j] = 0
}
}
for i := 0; i < 2; i++ {
for j := 0; j < MaxDepth; j++ {
pos.SearchKillers[i][j] = 0
}
}
pos.PvTable.Clear()
pos.Ply = 0
info.nodes = 0
info.fh = 0
info.fhf = 0
}
// Quiescence search does alpha beta considering all the quiet moves. It covers horizon effects on the chess board
func Quiescence(alpha, beta int, info *SearchInfo, pos *Board) int {
err := pos.Check()
if err != nil {
log.Fatalln("Error occurred in Quiescence")
}
checkUp(info)
info.nodes++
if isRepetition(pos) || pos.FiftyMove >= 100 {
return 0
}
if pos.Ply > MaxDepth-1 {
score, _ := EvalPosition(pos)
return score
}
score, _ := EvalPosition(pos)
if score >= beta {
return beta
}
if score > alpha {
alpha = score
}
var list MoveList
OnlyCapturedMoves = true
(&list).GenerateAllMoves(pos)
OnlyCapturedMoves = false
legalMovesCount := 0
oldAlpha := alpha
bestMove := NoMove
score = -Infinite
for i := 0; i < list.count; i++ {
(&list).PickNextMove(i)
moveMade, _ := MakeMove(list.moves[i].move, pos)
if !moveMade {
continue
}
legalMovesCount++
score = -Quiescence(-beta, -alpha, info, pos)
TakeMove(pos)
if info.stopped == true {
return 0
}
if score > alpha {
if score >= beta {
if legalMovesCount == 1 {
info.fhf++
}
info.fh++
return beta
}
alpha = score
bestMove = list.moves[i].move
}
}
if alpha != oldAlpha {
StorePvMove(pos, bestMove)
}
return alpha
}
func AlphaBeta(alpha, beta, depth int, info *SearchInfo, pos *Board, doNull bool) int {
err := pos.Check()
if err != nil {
log.Fatalln("Invalid board position")
return 0
}
if depth == 0 {
return Quiescence(alpha, beta, info, pos)
}
checkUp(info)
info.nodes++
// draw condition
if (isRepetition(pos) || pos.FiftyMove >= 100) && pos.Ply > 0 {
return 0
}
// when depth has exceeded maxdepth
if pos.Ply > MaxDepth-1 {
score, _ := EvalPosition(pos)
return score
}
if attacked, _ := SqAttacked(pos.KingSq[pos.Side], pos.Side^1, pos); attacked {
depth++
}
var list MoveList
(&list).GenerateAllMoves(pos)
legalMovesCount := 0 // if no legal moves then it is condition of check mate or stale mate
oldAlpha := alpha
bestMove := NoMove
score := -Infinite
pvMove, _ := ProbePvTable(pos)
if pvMove != NoMove {
for i := 0; i < list.count; i++ {
if list.moves[i].move == pvMove {
list.moves[i].score = 2000000
break
}
}
}
for i := 0; i < list.count; i++ {
(&list).PickNextMove(i)
moveMade, _ := MakeMove(list.moves[i].move, pos)
if !moveMade {
continue
}
legalMovesCount++
score = -AlphaBeta(-beta, -alpha, depth-1, info, pos, doNull)
TakeMove(pos)
if info.stopped == true {
return 0
}
if score > alpha {
if score >= beta {
if legalMovesCount == 1 {
info.fhf++
}
info.fh++
if (list.moves[i].move & MFlagCAP) == 0 {
pos.SearchKillers[1][pos.Ply] = pos.SearchKillers[0][pos.Ply]
pos.SearchKillers[0][pos.Ply] = list.moves[i].move
}
return beta
}
alpha = score
bestMove = list.moves[i].move
if (list.moves[i].move & MFlagCAP) == 0 {
pos.SearchHistory[pos.Pieces[FromSq(bestMove)]][ToSq(bestMove)] += depth
}
}
}
if legalMovesCount == 0 {
if attacked, _ := SqAttacked(pos.KingSq[pos.Side], pos.Side^1, pos); attacked {
return -Mate + pos.Ply
} else {
return 0
}
}
if alpha != oldAlpha {
StorePvMove(pos, bestMove)
}
return alpha
}
func SearchPosition(pos *Board, info *SearchInfo) {
bestMove := NoMove
bestScore := -Infinite
ClearForSearch(pos, info)
// iterative deepening
for currentDepth := 1; currentDepth <= info.depth; currentDepth++ {
bestScore = AlphaBeta(-Infinite, Infinite, currentDepth, info, pos, true)
if info.stopped == true {
break
}
pvMoves, _ := GetPvLine(currentDepth, pos)
bestMove = pos.PvArray[0]
fmt.Printf("info score cp %d depth %d, nodes %v time %v pv", bestScore, currentDepth, info.nodes, time.Now().Sub(info.startTime).Seconds()*1000)
for i := 0; i < pvMoves; i++ {
fmt.Printf(" %s", PrintMove(pos.PvArray[i]))
}
fmt.Println()
// fmt.Println("Ordering: ", info.fhf/info.fh)
}
fmt.Printf("bestmove %s\n", PrintMove(bestMove))
}