-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvictoire.zig
401 lines (324 loc) · 13.4 KB
/
victoire.zig
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
//! Contains the chess engine, search functions, and related functionalities.
const std = @import("std");
const chess = @import("chess.zig");
const movegen = @import("movegen.zig");
const transposition = @import("transposition.zig");
const evaluation = @import("evaluation.zig");
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
const hasher = transposition.ZobristHasher.init();
const SearchNode = struct {
board: chess.Board,
depth: u32,
ply: u32,
alpha: i64,
beta: i64,
hash: u64,
eval: evaluation.Evaluator,
pub inline fn root(board: chess.Board, depth: u32) SearchNode {
return .{
.board = board,
.depth = depth,
.ply = 0,
.alpha = -evaluation.checkmate,
.beta = evaluation.checkmate,
.hash = hasher.calculate(board),
.eval = evaluation.Evaluator.init(board),
};
}
pub inline fn next(self: SearchNode, move: chess.Move) SearchNode {
return .{
.board = self.board.copyAndMake(move),
.depth = if (self.depth == 0) 0 else self.depth - 1,
.ply = self.ply + 1,
.alpha = -self.beta,
.beta = -self.alpha,
.hash = hasher.update(self.hash, move),
.eval = self.eval.next(move),
};
}
pub inline fn nullWindow(self: SearchNode) SearchNode {
var result = self;
result.alpha = self.beta - 1;
return result;
}
pub inline fn betaNullWindow(self: SearchNode) SearchNode {
var result = self;
result.beta = self.alpha + 1;
return result;
}
pub inline fn append(self: SearchNode, depth: u32) SearchNode {
var result = self;
result.depth += depth;
return result;
}
pub inline fn reduce(self: SearchNode, plies: u32) SearchNode {
var result = self;
result.depth -= @min(result.depth, plies);
return result;
}
pub inline fn evaluate(self: SearchNode) i64 {
var mutable_node = self;
return self.eval.evaluate(&mutable_node.board);
}
};
pub const SearchResult = struct {
best_move: chess.Move = chess.Move.nullMove(),
score: i64 = -evaluation.checkmate,
depth: u32 = 0,
pub inline fn raw(score: i64) SearchResult {
return .{ .score = score };
}
pub inline fn inv(self: SearchResult) SearchResult {
return .{
.best_move = self.best_move,
.score = -self.score,
.depth = self.depth,
};
}
pub inline fn checkmate(self: SearchResult) ?i64 {
if (@abs(self.score) < evaluation.checkmate - 200) return null;
const mul: i64 = if (self.score > 0) 1 else -1;
return mul * @divFloor(self.depth + 1, 2);
}
};
const MoveDataList = std.ArrayList(MoveData);
const MoveData = struct {
move: chess.Move = chess.Move.nullMove(),
score: i64 = -evaluation.checkmate,
is_pv: bool = false,
pub inline fn appendMove(list: *MoveDataList, move: chess.Move) void {
list.append(.{
.move = move,
}) catch unreachable;
}
pub fn lessThan(_: void, self: MoveData, other: MoveData) bool {
if (other.is_pv) return true;
if (self.is_pv) return false;
return self.score < other.score;
}
};
const Table = transposition.TranspositionTable(TranspositionData);
const TranspositionData = struct {
search_result: SearchResult = SearchResult.raw(0),
flag: enum { exact, lower, upper } = .exact,
pub inline fn init(search_result: SearchResult) TranspositionData {
return .{ .search_result = search_result };
}
};
/// Calculate movetime based on time control.
pub fn manageTime(time: i64, inc: i64, movestogo: ?i64) i64 {
if (inc > time) return time;
const mtg = movestogo orelse 50;
return @divFloor(time, mtg) + inc;
}
/// The Victory Chess Engine.
pub const Engine = struct {
options: struct {
quiesce_depth: u32 = 12,
table_size: u64 = 1_000_000,
late_move_reduction: bool = true,
null_move_pruning: bool = true,
} = .{},
data: struct {
move_list: MoveDataList = undefined,
deadline: ?i64 = null,
aborted: bool = false,
table: Table = undefined,
} = .{},
infos: struct {
nodes: u64 = 0,
} = .{},
pub fn init() Engine {
var engine = Engine{};
engine.data.move_list = MoveDataList.init(allocator);
engine.data.table = Table.init(engine.options.table_size, TranspositionData{}) catch unreachable;
return engine;
}
pub fn initWithSize(size: u64) Engine {
var engine = Engine{};
engine.options.table_size = size;
engine.data.move_list = MoveDataList.init(allocator);
engine.data.table = Table.init(engine.options.table_size, TranspositionData{}) catch unreachable;
return engine;
}
pub fn deinit(self: *Engine) void {
self.data.move_list.deinit();
self.data.table.deinit();
}
pub fn search(self: *Engine, board: chess.Board, depth: u32, time: ?i64) SearchResult {
self.data.aborted = false;
self.data.deadline = time;
self.infos.nodes = 0;
if (time != null) self.data.deadline.? += std.time.milliTimestamp();
var result = SearchResult.raw(0);
for (1..depth + 1) |ply| {
const ply_result = self.PVS(SearchNode.root(board, @intCast(ply)));
if (@atomicLoad(bool, &self.data.aborted, .seq_cst)) break;
result = ply_result;
if (result.checkmate() != null and result.score > 0) break;
}
return result;
}
pub fn explore(self: *Engine, board: chess.Board, context: anytype, explorer: fn (@TypeOf(context), SearchResult) bool) void {
self.data.aborted = false;
self.data.deadline = null;
self.infos.nodes = 0;
for (1..100) |ply| {
const ply_result = self.PVS(SearchNode.root(board, @intCast(ply)));
if (@atomicLoad(bool, &self.data.aborted, .seq_cst)) break;
if (explorer(context, ply_result)) break;
if (ply_result.checkmate() != null and ply_result.score > 0) break;
}
}
fn shouldAbort(self: *Engine) bool {
if (@atomicLoad(bool, &self.data.aborted, .seq_cst)) return true;
if (self.data.deadline != null) {
if (self.data.deadline.? <= std.time.milliTimestamp()) {
@atomicStore(bool, &self.data.aborted, true, .seq_cst);
return true;
}
}
return false;
}
pub fn stop(self: *Engine) void {
@atomicStore(bool, &self.data.aborted, true, .seq_cst);
}
fn PVS(self: *Engine, node: SearchNode) SearchResult {
if (self.shouldAbort()) return SearchResult.raw(0);
if (node.depth == 0) return SearchResult.raw(self.quiesce(node.append(self.options.quiesce_depth)));
self.infos.nodes += 1;
const move_list_len = self.data.move_list.items.len;
var search_result = SearchResult{};
var mutable_node = node;
var pv: ?chess.Move = null;
var record_depth: u32 = 0;
// Checks transposition table.
if (node.depth > 1) transpo: {
const record = self.data.table.get(node.hash) orelse break :transpo;
record_depth = record.search_result.depth;
if (record_depth >= node.depth) {
switch (record.flag) {
.exact => return record.search_result,
.lower => mutable_node.alpha = @max(node.alpha, record.search_result.score),
.upper => mutable_node.beta = @min(node.beta, record.search_result.score),
}
if (mutable_node.alpha >= mutable_node.beta) return record.search_result;
}
pv = record.search_result.best_move;
}
// Null move pruning.
if (self.options.null_move_pruning and !movegen.isCheck(&mutable_node.board)) {
const r: u32 = if (node.depth > 6) 4 else 3;
const child = mutable_node.next(chess.Move.nullMove()).reduce(r + 1).betaNullWindow();
const child_result = self.PVS(child).inv();
if (child_result.score >= mutable_node.beta) return SearchResult.raw(mutable_node.beta);
}
// Generates moves.
const move_count = movegen.generate(node.board, &self.data.move_list, MoveData.appendMove);
// Detects checkmate and stalemate.
if (move_count == 0) return switch (movegen.end(&mutable_node.board)) {
.checkmate => SearchResult.raw(-evaluation.checkmate + node.ply),
.stalemate => SearchResult.raw(evaluation.stalemate),
};
// Updates move scores for ordering.
for (move_list_len..self.data.move_list.items.len) |i| {
var move_data = &self.data.move_list.items[i];
if (pv != null and pv.?.sameAs(move_data.move)) {
move_data.is_pv = true;
continue;
}
const hash = hasher.update(node.hash, move_data.move);
const record = self.data.table.get(hash);
if (record != null and record.?.flag == .exact) {
const sr = record.?.search_result;
move_data.score = sr.score + sr.depth * 10;
} else move_data.score = -mutable_node.next(move_data.move).evaluate();
}
// Orders moves.
std.sort.heap(MoveData, self.data.move_list.items[move_list_len..], {}, MoveData.lessThan);
// PVS algorithm.
for (0..move_count) |i| {
const move_data = self.data.move_list.pop();
const child_result = blk: {
const child = mutable_node.next(move_data.move);
if (i == 0) break :blk self.PVS(child).inv();
// Late move reduction.
const lmr: u32 = reduc: {
if (!self.options.late_move_reduction) break :reduc 0;
if (move_data.move.is_check_evasion) break :reduc 0;
if (move_data.move.capture != null) break :reduc 0;
if (node.depth < 4) break :reduc 0;
const last_depth = node.ply + node.depth - 1;
if (last_depth < 4) {
if (i < 5) break :reduc 0;
break :reduc 1;
} else {
if (i < 4) break :reduc 0;
if (i < 8) break :reduc 1;
break :reduc node.depth / 2;
}
};
const reduced_result = red: {
if (lmr == 0) break :red SearchResult.raw(mutable_node.alpha + 1);
break :red self.PVS(child.reduce(lmr).nullWindow()).inv();
};
const result = res: {
if (reduced_result.score <= mutable_node.alpha) break :res reduced_result;
const result = self.PVS(child.nullWindow()).inv();
if (result.score > mutable_node.alpha) {
if (mutable_node.alpha < result.score and result.score < mutable_node.beta)
break :res self.PVS(child).inv();
}
break :res result;
};
break :blk result;
};
if (child_result.score > mutable_node.alpha) {
mutable_node.alpha = child_result.score;
search_result.depth = child_result.depth;
search_result.best_move = move_data.move;
}
if (mutable_node.alpha >= mutable_node.beta) {
self.data.move_list.resize(move_list_len) catch unreachable;
break;
}
}
search_result.score = mutable_node.alpha;
search_result.depth += 1;
// Saves search result in transposition table.
if (node.depth > 1 and node.depth >= record_depth) {
var record = TranspositionData.init(search_result);
if (search_result.score <= node.alpha) record.flag = .upper;
if (search_result.score >= mutable_node.beta) record.flag = .lower;
self.data.table.set(node.hash, record);
}
return search_result;
}
fn quiesce(self: *Engine, node: SearchNode) i64 {
if (self.shouldAbort()) return 0;
self.infos.nodes += 1;
const pat = node.evaluate();
if (node.depth == 0) return pat;
if (pat >= node.beta) return node.beta;
var mutable_node = node;
mutable_node.alpha = @max(node.alpha, pat);
const move_list_len = self.data.move_list.items.len;
const move_count = movegen.generate(node.board, &self.data.move_list, MoveData.appendMove);
if (move_count == 0) return switch (movegen.end(&mutable_node.board)) {
.checkmate => -evaluation.checkmate + node.ply,
.stalemate => evaluation.stalemate,
};
for (0..move_count) |_| {
const move_data = self.data.move_list.pop();
if (move_data.move.capture == null) continue;
const score = -self.quiesce(mutable_node.next(move_data.move));
if (score >= mutable_node.beta) {
self.data.move_list.resize(move_list_len) catch unreachable;
return mutable_node.beta;
}
mutable_node.alpha = @max(mutable_node.alpha, score);
}
return mutable_node.alpha;
}
};