1
1
use std:: collections:: HashMap ;
2
- use tokio:: time:: { Duration , Instant } ;
3
2
4
3
use chess:: { Action , BitBoard , Board , BoardStatus , ChessMove , Color , MoveGen , Piece } ;
4
+ use tokio:: time:: { Duration , Instant } ;
5
5
6
6
use crate :: common:: constants:: {
7
- modules:: { self , * } ,
7
+ modules:: * ,
8
8
naive_psqt_tables:: * ,
9
9
tapered_pesto_psqt_tables:: * ,
10
10
} ;
@@ -134,7 +134,7 @@ impl Algorithm {
134
134
// node on our layer
135
135
stats. progress_on_next_layer *= 1. / num_legal_moves as f32 ;
136
136
stats. progress_on_next_layer +=
137
- ( i. saturating_sub ( 1 ) ) as f32 / num_legal_moves as f32 ;
137
+ i. saturating_sub ( 1 ) as f32 / num_legal_moves as f32 ;
138
138
return best_evaluation;
139
139
} ;
140
140
@@ -182,7 +182,7 @@ impl Algorithm {
182
182
num_extensions + extend_by,
183
183
board_played_times_prediction,
184
184
mg_incremental_psqt_eval,
185
- eg_incremental_psqt_eval
185
+ eg_incremental_psqt_eval,
186
186
) ;
187
187
board_played_times_prediction. insert (
188
188
new_board,
@@ -196,8 +196,8 @@ impl Algorithm {
196
196
// Replace best_eval if ours is better
197
197
if evaluation. eval . is_some ( )
198
198
&& ( best_evaluation. eval . is_none ( )
199
- || maximise && evaluation. eval . unwrap ( ) > best_evaluation. eval . unwrap ( )
200
- || !maximise && evaluation. eval . unwrap ( ) < best_evaluation. eval . unwrap ( ) )
199
+ || maximise && evaluation. eval . unwrap ( ) > best_evaluation. eval . unwrap ( )
200
+ || !maximise && evaluation. eval . unwrap ( ) < best_evaluation. eval . unwrap ( ) )
201
201
{
202
202
if original && module_enabled ( self . modules , ANALYZE ) {
203
203
let mut vec = Vec :: new ( ) ;
@@ -238,19 +238,19 @@ impl Algorithm {
238
238
}
239
239
}
240
240
241
- if utils :: module_enabled ( self . modules , modules :: TAPERED_INCREMENTAL_PESTO_PSQT ) {
241
+ if module_enabled ( self . modules , TAPERED_INCREMENTAL_PESTO_PSQT ) {
242
242
fn calc_increment (
243
243
piece_type : Piece ,
244
244
location : usize ,
245
- mg_eg : bool
245
+ mg_eg : bool ,
246
246
) -> f32 {
247
247
if mg_eg {
248
248
TAPERED_MG_PESTO [ piece_type. to_index ( ) ] [ location]
249
249
} else {
250
250
TAPERED_EG_PESTO [ piece_type. to_index ( ) ] [ location]
251
251
}
252
252
}
253
- let moved_piece_type= board. piece_on ( chess_move. get_source ( ) ) . unwrap ( ) ;
253
+ let moved_piece_type = board. piece_on ( chess_move. get_source ( ) ) . unwrap ( ) ;
254
254
255
255
let multiplier = if board. side_to_move ( ) == Color :: White {
256
256
1
@@ -264,7 +264,6 @@ impl Algorithm {
264
264
mg_incremental_psqt_eval_change += Self :: calc_tapered_psqt_eval ( board, i, true ) ;
265
265
mg_incremental_psqt_eval_change += Self :: calc_tapered_psqt_eval ( board, i, false ) ;
266
266
}
267
-
268
267
} else {
269
268
//Remove the eval from the previous square we stood on.
270
269
let source: usize = ( 56 - chess_move. get_source ( ) . to_int ( )
@@ -277,8 +276,8 @@ impl Algorithm {
277
276
let dest: usize = ( 56 - chess_move. get_dest ( ) . to_int ( )
278
277
+ 2 * ( chess_move. get_dest ( ) . to_int ( ) % 8 ) )
279
278
as usize ;
280
- mg_incremental_psqt_eval_change += calc_increment ( moved_piece_type, dest, true ) ;
281
- eg_incremental_psqt_eval_change += calc_increment ( moved_piece_type, dest, false ) ;
279
+ mg_incremental_psqt_eval_change += calc_increment ( moved_piece_type, dest, true ) ;
280
+ eg_incremental_psqt_eval_change += calc_increment ( moved_piece_type, dest, false ) ;
282
281
283
282
//Decrement enemy eval from potetntial capture
284
283
if let Some ( attacked_piece_type) = board. piece_on ( chess_move. get_dest ( ) ) {
@@ -289,8 +288,8 @@ impl Algorithm {
289
288
mg_incremental_psqt_eval += mg_incremental_psqt_eval_change * multiplier as f32 ;
290
289
eg_incremental_psqt_eval += eg_incremental_psqt_eval_change * multiplier as f32 ;
291
290
}
292
- best_evaluation. incremental_psqt_eval =
293
- Some ( mg_incremental_psqt_eval + eg_incremental_psqt_eval) ;
291
+ best_evaluation. incremental_psqt_eval =
292
+ Some ( mg_incremental_psqt_eval + eg_incremental_psqt_eval) ;
294
293
}
295
294
296
295
if module_enabled ( self . modules , TRANSPOSITION_TABLE ) && depth >= 3 {
@@ -320,7 +319,7 @@ impl Algorithm {
320
319
board : & Board ,
321
320
depth : u32 ,
322
321
deadline : Option < Instant > ,
323
- ) -> ( Option < chess :: Action > , Vec < String > , Stats ) {
322
+ ) -> ( Option < Action > , Vec < String > , Stats ) {
324
323
let mut stats = Stats :: default ( ) ;
325
324
let out = self . node_eval_recursive (
326
325
board,
@@ -343,7 +342,7 @@ impl Algorithm {
343
342
& mut self ,
344
343
board : & Board ,
345
344
deadline : Instant ,
346
- ) -> ( chess :: Action , Vec < String > , Stats ) {
345
+ ) -> ( Action , Vec < String > , Stats ) {
347
346
self . board_played_times . insert (
348
347
* board,
349
348
* self . board_played_times . get ( board) . unwrap_or ( & 0 ) + 1 ,
@@ -401,7 +400,7 @@ impl Algorithm {
401
400
board : & Board ,
402
401
board_played_times_prediction : & HashMap < Board , u32 > ,
403
402
mg_incremental_psqt_eval : f32 ,
404
- eg_incremental_psqt_eval : f32
403
+ eg_incremental_psqt_eval : f32 ,
405
404
) -> f32 {
406
405
let board_status = board. status ( ) ;
407
406
if board_status == BoardStatus :: Stalemate {
@@ -426,7 +425,7 @@ impl Algorithm {
426
425
let diff_material: i32 = material_each_side. 0 as i32 - material_each_side. 1 as i32 ;
427
426
428
427
let mut controlled_squares = 0 ;
429
- if utils :: module_enabled ( self . modules , modules :: SQUARE_CONTROL_METRIC ) {
428
+ if module_enabled ( self . modules , SQUARE_CONTROL_METRIC ) {
430
429
controlled_squares = if board. side_to_move ( ) == Color :: Black {
431
430
-1i32
432
431
} else {
@@ -436,7 +435,7 @@ impl Algorithm {
436
435
437
436
// Compares piece position with an 8x8 table containing certain values. The value corresponding to the position of the piece gets added as evaluation.
438
437
let mut naive_psqt: f32 = 0. ;
439
- if utils :: module_enabled ( self . modules , modules :: NAIVE_PSQT ) {
438
+ if module_enabled ( self . modules , NAIVE_PSQT ) {
440
439
fn naive_psqt_calc (
441
440
naive_psqt_table : [ f32 ; 64 ] ,
442
441
piece_bitboard : & BitBoard ,
@@ -503,18 +502,18 @@ impl Algorithm {
503
502
let mut mg_tapered_pesto: f32 = 0. ;
504
503
let mut eg_tapered_pesto: f32 = 0. ;
505
504
let mut tapered_pesto: f32 = 0. ;
506
- if utils :: module_enabled ( self . modules , modules :: TAPERED_EVERY_PESTO_PSQT ) {
505
+ if module_enabled ( self . modules , TAPERED_EVERY_PESTO_PSQT ) {
507
506
for i in 0 ..5 {
508
507
mg_tapered_pesto += Self :: calc_tapered_psqt_eval ( board, i, true ) ;
509
508
eg_tapered_pesto += Self :: calc_tapered_psqt_eval ( board, i, false ) ;
510
509
}
511
- tapered_pesto = ( ( material_each_side. 0 + material_each_side. 1 - 2 * piece_value ( Piece :: King ) ) as f32 * mg_tapered_pesto +
512
- ( 78 - ( material_each_side. 0 + material_each_side. 1 - 2 * piece_value ( Piece :: King ) ) ) as f32 * eg_tapered_pesto)
513
- / 78. ;
510
+ tapered_pesto = ( ( material_each_side. 0 + material_each_side. 1 - 2 * piece_value ( Piece :: King ) ) as f32 * mg_tapered_pesto +
511
+ ( 78 - ( material_each_side. 0 + material_each_side. 1 - 2 * piece_value ( Piece :: King ) ) ) as f32 * eg_tapered_pesto)
512
+ / 78. ;
514
513
}
515
514
516
515
let mut pawn_structure: f32 = 0. ;
517
- if utils :: module_enabled ( self . modules , modules :: PAWN_STRUCTURE ) {
516
+ if module_enabled ( self . modules , PAWN_STRUCTURE ) {
518
517
fn pawn_structure_calc (
519
518
all_pawn_bitboard : & BitBoard ,
520
519
color_bitboard : & BitBoard ,
@@ -526,15 +525,15 @@ impl Algorithm {
526
525
//pawn chain, awarding 0.5 eval for each pawn protected by another pawn. Constants should in theory cover an (literal) edge case... I hope.
527
526
bonus += 0.5
528
527
* ( ( pawn_bitboard & 0xFEFEFEFEFEFEFEFE & ( pawn_bitboard << 9 ) ) . count_ones ( )
529
- + ( pawn_bitboard & 0x7F7F7F7F7F7F7F7F & ( pawn_bitboard << 7 ) ) . count_ones ( ) )
530
- as f32 ;
528
+ + ( pawn_bitboard & 0x7F7F7F7F7F7F7F7F & ( pawn_bitboard << 7 ) ) . count_ones ( ) )
529
+ as f32 ;
531
530
532
531
//stacked pawns. -0.5 points per rank containing >1 pawns. By taking the pawn bitboard and operating bitwise AND for another bitboard (integer) where the leftmost rank is filled. This returns all pawns in that rank. By bitshifting we can choose rank. Additionally by counting we get number of pawns. We then remove 1 as we only want to know if there are >1 pawn. If there is, subtract 0.5 points per extra pawn.
533
532
for i in 0 ..7 {
534
533
//constant 0x8080808080808080: entire first rank.
535
534
bonus -= 0.5
536
535
* ( ( pawn_bitboard & ( 0x8080808080808080 >> i) ) . count_ones ( ) as f32 - 1. )
537
- . max ( 0. ) ;
536
+ . max ( 0. ) ;
538
537
}
539
538
540
539
//king safety. Outer 3 pawns get +1 eval bonus per pawn if king is behind them. King bitboard required is either ..X..... or ......X.
@@ -558,9 +557,9 @@ impl Algorithm {
558
557
}
559
558
560
559
let mut incremental_psqt_eval: f32 = 0. ;
561
- if utils :: module_enabled ( self . modules , modules :: TAPERED_INCREMENTAL_PESTO_PSQT ) {
560
+ if module_enabled ( self . modules , TAPERED_INCREMENTAL_PESTO_PSQT ) {
562
561
incremental_psqt_eval = ( material_each_side. 0 + material_each_side. 1 - 2 * piece_value ( Piece :: King ) ) as f32 * mg_incremental_psqt_eval
563
- + ( 78 - material_each_side. 0 + material_each_side. 1 - 2 * piece_value ( Piece :: King ) ) as f32 * eg_incremental_psqt_eval
562
+ + ( 78 - material_each_side. 0 + material_each_side. 1 - 2 * piece_value ( Piece :: King ) ) as f32 * eg_incremental_psqt_eval
564
563
}
565
564
566
565
let evaluation: f32 = controlled_squares as f32 / 20.
@@ -577,7 +576,7 @@ impl Algorithm {
577
576
piece_bitboard : & BitBoard ,
578
577
color_bitboard : & BitBoard ,
579
578
piece_index : usize ,
580
- mg_eg : bool
579
+ mg_eg : bool ,
581
580
) -> f32 {
582
581
// Essentially, gets the dot product between a "vector" of the bitboard (containing 64 0s and 1s) and the table with NAIVE_PSQT bonus constants.
583
582
let mut bonus: f32 = 0. ;
@@ -592,22 +591,21 @@ impl Algorithm {
592
591
// and we (hopefully?) linerarly transition from one to the other, depending on material value.
593
592
bonus += ( ( piece_bitboard & color_bitboard)
594
593
. reverse_colors ( )
595
- . to_size ( i as u8 ) & 1 ) as f32
594
+ . to_size ( i as u8 ) & 1 ) as f32
596
595
* TAPERED_MG_PESTO [ piece_index] [ i] ;
597
596
}
598
597
bonus
599
598
} else {
600
599
for i in 0 ..63 {
601
600
bonus += ( ( piece_bitboard & color_bitboard)
602
601
. reverse_colors ( )
603
- . to_size ( i as u8 ) & 1 ) as f32
602
+ . to_size ( i as u8 ) & 1 ) as f32
604
603
* TAPERED_EG_PESTO [ piece_index] [ i] ;
605
604
}
606
605
bonus
607
606
}
608
-
609
607
}
610
-
608
+
611
609
macro_rules! tapered_psqt_calc {
612
610
( $board: tt, $piece: tt, $index: tt, $mg_eg: tt) => {
613
611
tapered_psqt_calc(
@@ -627,7 +625,6 @@ impl Algorithm {
627
625
5 => tapered_psqt_calc ! ( board, King , 5 , mg_eg) ,
628
626
6_u8 ..=u8:: MAX => unimplemented ! ( )
629
627
}
630
-
631
628
}
632
629
633
630
pub ( crate ) fn reset ( & mut self ) {
0 commit comments