@@ -16,7 +16,14 @@ pub(crate) struct Algorithm {
16
16
pub ( crate ) time_per_move : Duration ,
17
17
/// Number of times that a given board has been played
18
18
pub ( crate ) board_played_times : HashMap < Board , u32 > ,
19
- pub ( crate ) pawn_hash : HashMap < BitBoard , f32 >
19
+ pub ( crate ) pawn_hash : HashMap < BitBoard , f32 > ,
20
+ pub ( crate ) position_pawn_hash : HashMap < BitBoard , f32 > ,
21
+ pub ( crate ) position_rook_hash : HashMap < BitBoard , f32 > ,
22
+ pub ( crate ) position_king_hash : HashMap < BitBoard , f32 > ,
23
+ pub ( crate ) position_queen_hash : HashMap < BitBoard , f32 > ,
24
+ pub ( crate ) position_knight_hash : HashMap < BitBoard , f32 > ,
25
+ pub ( crate ) position_bishop_hash : HashMap < BitBoard , f32 >
26
+
20
27
}
21
28
22
29
impl Algorithm {
@@ -26,7 +33,13 @@ impl Algorithm {
26
33
transposition_table : HashMap :: with_capacity ( 45 ) ,
27
34
time_per_move,
28
35
board_played_times : HashMap :: new ( ) ,
29
- pawn_hash : HashMap :: new ( )
36
+ pawn_hash : HashMap :: new ( ) ,
37
+ position_knight_hash : HashMap :: new ( ) ,
38
+ position_pawn_hash : HashMap :: new ( ) ,
39
+ position_rook_hash : HashMap :: new ( ) ,
40
+ position_bishop_hash : HashMap :: new ( ) ,
41
+ position_queen_hash : HashMap :: new ( ) ,
42
+ position_king_hash : HashMap :: new ( ) ,
30
43
}
31
44
}
32
45
@@ -371,20 +384,28 @@ impl Algorithm {
371
384
}
372
385
return bonus;
373
386
}
387
+
388
+ fn in_hash_map ( bitboard : & BitBoard , color_bitboard : & BitBoard , position_table : [ f32 ; 64 ] , position_hash_map : & mut HashMap :: < BitBoard , f32 > ) -> f32 {
389
+ if !position_hash_map. contains_key ( & ( bitboard & color_bitboard) ) {
390
+ position_hash_map. insert ( bitboard & color_bitboard, position_bonus_calc ( position_table, bitboard, color_bitboard) ) ;
391
+ }
392
+ return * position_hash_map. get ( & ( bitboard & color_bitboard) ) . unwrap ( ) ;
393
+ }
394
+
374
395
if board. side_to_move ( ) == Color :: White {
375
- position_bonus += position_bonus_calc ( position_bonus_table_pawn , board. pieces ( Piece :: Pawn ) , board. color_combined ( Color :: White ) ) ;
376
- position_bonus += position_bonus_calc ( position_bonus_table_knight , board. pieces ( Piece :: Knight ) , board. color_combined ( Color :: White ) ) ;
377
- position_bonus += position_bonus_calc ( position_bonus_table_rook , board. pieces ( Piece :: Rook ) , board. color_combined ( Color :: White ) ) ;
378
- position_bonus += position_bonus_calc ( position_bonus_table_king , board. pieces ( Piece :: King ) , board. color_combined ( Color :: White ) ) ;
379
- position_bonus += position_bonus_calc ( position_bonus_table_queen , board. pieces ( Piece :: Queen ) , board. color_combined ( Color :: White ) ) ;
380
- position_bonus += position_bonus_calc ( position_bonus_table_bishop , board. pieces ( Piece :: Bishop ) , board. color_combined ( Color :: White ) ) ;
396
+ position_bonus += in_hash_map ( board. pieces ( Piece :: Pawn ) , board. color_combined ( Color :: White ) , POSITION_BONUS_TABLE_PAWN , & mut self . position_pawn_hash ) ;
397
+ position_bonus += in_hash_map ( board. pieces ( Piece :: Rook ) , board. color_combined ( Color :: White ) , POSITION_BONUS_TABLE_ROOK , & mut self . position_rook_hash ) ;
398
+ position_bonus += in_hash_map ( board. pieces ( Piece :: King ) , board. color_combined ( Color :: White ) , POSITION_BONUS_TABLE_KING , & mut self . position_king_hash ) ;
399
+ position_bonus += in_hash_map ( board. pieces ( Piece :: Queen ) , board. color_combined ( Color :: White ) , POSITION_BONUS_TABLE_QUEEN , & mut self . position_queen_hash ) ;
400
+ position_bonus += in_hash_map ( board. pieces ( Piece :: Bishop ) , board. color_combined ( Color :: White ) , POSITION_BONUS_TABLE_BISHOP , & mut self . position_bishop_hash ) ;
401
+ position_bonus += in_hash_map ( board. pieces ( Piece :: Knight ) , board. color_combined ( Color :: White ) , POSITION_BONUS_TABLE_KNIGHT , & mut self . position_knight_hash ) ;
381
402
} else {
382
- position_bonus += position_bonus_calc ( position_bonus_table_pawn , board. pieces ( Piece :: Pawn ) , board. color_combined ( Color :: Black ) ) ;
383
- position_bonus += position_bonus_calc ( position_bonus_table_knight , board. pieces ( Piece :: Knight ) , board. color_combined ( Color :: Black ) ) ;
384
- position_bonus += position_bonus_calc ( position_bonus_table_rook , board. pieces ( Piece :: Rook ) , board. color_combined ( Color :: Black ) ) ;
385
- position_bonus += position_bonus_calc ( position_bonus_table_king , board. pieces ( Piece :: King ) , board. color_combined ( Color :: Black ) ) ;
386
- position_bonus += position_bonus_calc ( position_bonus_table_queen , board. pieces ( Piece :: Queen ) , board. color_combined ( Color :: Black ) ) ;
387
- position_bonus += position_bonus_calc ( position_bonus_table_bishop , board. pieces ( Piece :: Bishop ) , board. color_combined ( Color :: Black ) ) ;
403
+ position_bonus += in_hash_map ( board. pieces ( Piece :: Pawn ) , board. color_combined ( Color :: Black ) , POSITION_BONUS_TABLE_PAWN , & mut self . position_pawn_hash ) ;
404
+ position_bonus += in_hash_map ( board. pieces ( Piece :: Rook ) , board. color_combined ( Color :: Black ) , POSITION_BONUS_TABLE_ROOK , & mut self . position_rook_hash ) ;
405
+ position_bonus += in_hash_map ( board. pieces ( Piece :: King ) , board. color_combined ( Color :: Black ) , POSITION_BONUS_TABLE_KING , & mut self . position_king_hash ) ;
406
+ position_bonus += in_hash_map ( board. pieces ( Piece :: Queen ) , board. color_combined ( Color :: Black ) , POSITION_BONUS_TABLE_QUEEN , & mut self . position_queen_hash ) ;
407
+ position_bonus += in_hash_map ( board. pieces ( Piece :: Bishop ) , board. color_combined ( Color :: Black ) , POSITION_BONUS_TABLE_BISHOP , & mut self . position_bishop_hash ) ;
408
+ position_bonus += in_hash_map ( board. pieces ( Piece :: Knight ) , board. color_combined ( Color :: Black ) , POSITION_BONUS_TABLE_KNIGHT , & mut self . position_knight_hash ) ;
388
409
}
389
410
}
390
411
@@ -397,19 +418,14 @@ impl Algorithm {
397
418
//pawn chain, awarding 0.5 eval for each pawn protected by another pawn.
398
419
bonus += 0.5 * ( ( pawn_bitboard & ( pawn_bitboard << 7 ) ) . count_ones ( ) + ( pawn_bitboard & ( pawn_bitboard << 9 ) ) . count_ones ( ) ) as f32 ;
399
420
400
- //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
421
+ //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.
401
422
for i in 0 ..7 {
402
423
//constant 9259542123273814144 = 0x8080808080808080, or the entire first rank.
403
424
bonus -= 0.5 * cmp:: max ( ( pawn_bitboard & ( 0x8080808080808080 >> i) ) . count_ones ( ) as i64 - 1 , 0 ) as f32 ;
404
425
}
405
426
406
427
//king safety. Outer 3 pawns get +1 eval bonus per pawn if king is behind them. King position required is either ..X..... or ......X.
407
- if ( king_bitboard & 0x2 ) . count_ones ( ) > 0 {
408
- bonus += ( pawn_bitboard & 0x7 ) . count_ones ( ) as f32 ;
409
- }
410
- if ( king_bitboard & 0x20 ) . count_ones ( ) > 0 {
411
- bonus += ( pawn_bitboard & 0xE000 ) . count_ones ( ) as f32 ;
412
- }
428
+ bonus += ( ( king_bitboard & 0x2 ) . count_ones ( ) * ( pawn_bitboard & 0x7 ) . count_ones ( ) + ( king_bitboard & 0x20 ) . count_ones ( ) * ( pawn_bitboard & 0xE000 ) . count_ones ( ) ) as f32 ;
413
429
return bonus;
414
430
}
415
431
@@ -437,5 +453,11 @@ impl Algorithm {
437
453
self . transposition_table = HashMap :: new ( ) ;
438
454
self . board_played_times = HashMap :: new ( ) ;
439
455
self . pawn_hash = HashMap :: new ( ) ;
456
+ self . position_pawn_hash = HashMap :: new ( ) ;
457
+ self . position_king_hash = HashMap :: new ( ) ;
458
+ self . position_queen_hash = HashMap :: new ( ) ;
459
+ self . position_bishop_hash = HashMap :: new ( ) ;
460
+ self . position_rook_hash = HashMap :: new ( ) ;
461
+ self . position_knight_hash = HashMap :: new ( ) ;
440
462
}
441
463
}
0 commit comments