@@ -6,18 +6,16 @@ use tokio::time::{Duration, Instant};
6
6
use crate :: algorithms:: { draw_checker, eval} ;
7
7
use crate :: common:: constants:: { modules:: * , naive_psqt_tables:: * , tapered_pesto_psqt_tables:: * } ;
8
8
use crate :: common:: utils:: { self , module_enabled, piece_value, Stats } ;
9
+ use crate :: modules:: { alpha_beta, analyze} ;
9
10
use crate :: modules:: search_extensions;
10
11
use crate :: modules:: skip_bad_moves;
11
12
use crate :: modules:: transposition_table:: { self , TranspositionEntry } ;
12
- use crate :: modules:: { alpha_beta, analyze} ;
13
13
14
14
use super :: utils:: Evaluation ;
15
15
16
-
17
16
#[ derive( Clone , Debug ) ]
18
17
pub ( crate ) struct Algorithm {
19
18
pub ( crate ) modules : u32 ,
20
- transposition_table : HashMap < Board , TranspositionEntry > ,
21
19
pub ( crate ) time_per_move : Duration ,
22
20
/// Number of times that a given board has been played
23
21
pub ( crate ) board_played_times : HashMap < Board , u32 > ,
@@ -48,7 +46,6 @@ impl Algorithm {
48
46
pub ( crate ) fn new ( modules : u32 , time_per_move : Duration ) -> Self {
49
47
Self {
50
48
modules,
51
- transposition_table : HashMap :: with_capacity ( 45 ) ,
52
49
time_per_move,
53
50
board_played_times : HashMap :: new ( ) ,
54
51
pawn_hash : HashMap :: new ( ) ,
@@ -72,9 +69,10 @@ impl Algorithm {
72
69
deadline : Option < Instant > ,
73
70
stats : & mut Stats ,
74
71
num_extensions : u32 ,
75
- board_played_times_prediction : & mut HashMap < Board , u32 > ,
72
+ board_played_times_prediction : & mut HashMap < u64 , u32 > ,
76
73
mut mg_incremental_psqt_eval : f32 ,
77
74
mut eg_incremental_psqt_eval : f32 ,
75
+ transposition_table : & mut HashMap < u64 , TranspositionEntry > ,
78
76
) -> NodeData {
79
77
if depth == 0 {
80
78
stats. leaves_visited += 1 ;
@@ -90,15 +88,15 @@ impl Algorithm {
90
88
None ,
91
89
Some ( mg_incremental_psqt_eval + eg_incremental_psqt_eval) ,
92
90
) ;
93
- if module_enabled ( self . modules , TRANSPOSITION_TABLE ) {
94
- transposition_table:: insert_in_transposition_table (
95
- & mut self . transposition_table ,
96
- board,
97
- depth,
98
- stats,
99
- evaluation,
100
- ) ;
101
- }
91
+ // if module_enabled(self.modules, TRANSPOSITION_TABLE) {
92
+ // transposition_table::insert_in_transposition_table(
93
+ // transposition_table,
94
+ // board,
95
+ // depth,
96
+ // stats,
97
+ // evaluation,
98
+ // );
99
+ // }
102
100
103
101
return NodeData :: new ( evaluation, None ) ;
104
102
}
@@ -114,12 +112,16 @@ impl Algorithm {
114
112
return NodeData :: new ( best_evaluation, None ) ;
115
113
}
116
114
117
- let transposition_table = if module_enabled ( self . modules , TRANSPOSITION_TABLE ) {
118
- Some ( & self . transposition_table )
119
- } else {
120
- None
121
- } ;
122
- let mut boards = Self :: create_board_list ( board, stats, legal_moves, transposition_table) ;
115
+ let mut boards = Self :: create_board_list (
116
+ board,
117
+ stats,
118
+ legal_moves,
119
+ if module_enabled ( self . modules , TRANSPOSITION_TABLE ) {
120
+ Some ( transposition_table)
121
+ } else {
122
+ None
123
+ } ,
124
+ ) ;
123
125
124
126
// Sort by eval
125
127
Self :: sort_by_eval ( maximise, & mut boards) ;
@@ -146,16 +148,14 @@ impl Algorithm {
146
148
return NodeData :: new ( best_evaluation, None ) ;
147
149
}
148
150
149
- let search_extensions = module_enabled ( self . modules , SEARCH_EXTENSIONS ) ;
150
- let extend_by = search_extensions:: calculate (
151
- num_extensions,
152
- num_legal_moves,
153
- new_board,
154
- search_extensions,
155
- ) ;
151
+ let extend_by = if module_enabled ( self . modules , SEARCH_EXTENSIONS ) {
152
+ search_extensions:: calculate ( num_extensions, num_legal_moves, new_board)
153
+ } else {
154
+ 0
155
+ } ;
156
156
157
- let evaluation = if let Some ( transposition_entry) = transposition_entry {
158
- transposition_entry. evaluation
157
+ let evaluation = if transposition_entry. is_some_and ( |entry| entry . depth >= depth ) {
158
+ transposition_entry. unwrap ( ) . evaluation
159
159
} else {
160
160
draw_checker:: count_board ( board_played_times_prediction, & new_board) ;
161
161
let evaluation = self . node_eval_recursive (
@@ -170,6 +170,7 @@ impl Algorithm {
170
170
board_played_times_prediction,
171
171
mg_incremental_psqt_eval,
172
172
eg_incremental_psqt_eval,
173
+ transposition_table,
173
174
) ;
174
175
draw_checker:: uncount_board ( board_played_times_prediction, & new_board) ;
175
176
debug_data = evaluation. debug_data ;
@@ -259,9 +260,9 @@ impl Algorithm {
259
260
Some ( mg_incremental_psqt_eval + eg_incremental_psqt_eval) ;
260
261
}
261
262
262
- if module_enabled ( self . modules , TRANSPOSITION_TABLE ) && depth >= 3 {
263
+ if module_enabled ( self . modules , TRANSPOSITION_TABLE ) {
263
264
transposition_table:: insert_in_transposition_table (
264
- & mut self . transposition_table ,
265
+ transposition_table,
265
266
board,
266
267
depth,
267
268
stats,
@@ -287,7 +288,7 @@ impl Algorithm {
287
288
board : & Board ,
288
289
stats : & mut Stats ,
289
290
legal_moves : MoveGen ,
290
- transposition_table : Option < & HashMap < Board , TranspositionEntry > > ,
291
+ transposition_table : Option < & HashMap < u64 , TranspositionEntry > > ,
291
292
) -> Vec < ( ChessMove , Board , Option < TranspositionEntry > ) > {
292
293
legal_moves
293
294
. map ( |chess_move| {
@@ -331,6 +332,7 @@ impl Algorithm {
331
332
board : & Board ,
332
333
depth : u32 ,
333
334
deadline : Option < Instant > ,
335
+ transposition_table : & mut HashMap < u64 , TranspositionEntry > ,
334
336
) -> ( Option < Action > , Vec < String > , Stats ) {
335
337
let mut stats = Stats :: default ( ) ;
336
338
let out = self . node_eval_recursive (
@@ -345,6 +347,7 @@ impl Algorithm {
345
347
& mut HashMap :: new ( ) ,
346
348
0. ,
347
349
0. ,
350
+ transposition_table,
348
351
) ;
349
352
let analyzer_data = out. debug_data . unwrap_or_default ( ) ;
350
353
( out. evaluation . next_action , analyzer_data, stats)
@@ -360,13 +363,16 @@ impl Algorithm {
360
363
* self . board_played_times . get ( board) . unwrap_or ( & 0 ) + 1 ,
361
364
) ;
362
365
366
+ let mut transposition_table = HashMap :: new ( ) ;
363
367
// Guarantee that at least the first layer gets done.
364
368
const START_DEPTH : u32 = 1 ;
365
- let mut deepest_complete_output = self . next_action ( board, START_DEPTH , None ) ;
369
+ let mut deepest_complete_output =
370
+ self . next_action ( board, START_DEPTH , None , & mut transposition_table) ;
366
371
let mut deepest_complete_depth = START_DEPTH ;
367
372
368
373
for depth in ( deepest_complete_depth + 1 ) ..=10 {
369
- let latest_output = self . next_action ( board, depth, Some ( deadline) ) ;
374
+ let latest_output =
375
+ self . next_action ( board, depth, Some ( deadline) , & mut transposition_table) ;
370
376
if utils:: passed_deadline ( deadline) {
371
377
// The cancelled layer is the one with this data
372
378
deepest_complete_output. 2 . progress_on_next_layer =
@@ -378,7 +384,6 @@ impl Algorithm {
378
384
}
379
385
}
380
386
deepest_complete_output. 2 . depth = deepest_complete_depth;
381
- deepest_complete_output. 2 . tt_size = self . transposition_table . len ( ) as u32 ;
382
387
383
388
let mut action = match deepest_complete_output. 0 {
384
389
Some ( action) => action,
@@ -410,7 +415,7 @@ impl Algorithm {
410
415
pub ( crate ) fn eval (
411
416
& mut self ,
412
417
board : & Board ,
413
- board_played_times_prediction : & HashMap < Board , u32 > ,
418
+ board_played_times_prediction : & HashMap < u64 , u32 > ,
414
419
mg_incremental_psqt_eval : f32 ,
415
420
eg_incremental_psqt_eval : f32 ,
416
421
) -> f32 {
@@ -426,7 +431,9 @@ impl Algorithm {
426
431
} ;
427
432
}
428
433
let board_played_times = * self . board_played_times . get ( board) . unwrap_or ( & 0 )
429
- + * board_played_times_prediction. get ( board) . unwrap_or ( & 0 ) ;
434
+ + * board_played_times_prediction
435
+ . get ( & board. get_hash ( ) )
436
+ . unwrap_or ( & 0 ) ;
430
437
if board_played_times >= 2 {
431
438
// This is third time this is played. Draw by three-fold repetition
432
439
return 0. ;
@@ -652,7 +659,6 @@ impl Algorithm {
652
659
}
653
660
654
661
pub ( crate ) fn reset ( & mut self ) {
655
- self . transposition_table = HashMap :: new ( ) ;
656
662
self . board_played_times = HashMap :: new ( ) ;
657
663
self . pawn_hash = HashMap :: new ( ) ;
658
664
self . naive_psqt_pawn_hash = HashMap :: new ( ) ;
0 commit comments