Skip to content

Commit 219475c

Browse files
committed
position bonus bugfix
1 parent 537b844 commit 219475c

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

src/algorithms/the_algorithm.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -347,22 +347,32 @@ impl Algorithm {
347347
//Compares piece position with an 8x8 table containing certain values. The value corresponding to the position of the piece gets added as evaluation.
348348
let mut position_bonus: f32 = 0.;
349349
if utils::module_enabled(self.modules, modules::POSITION_BONUS) {
350-
fn position_bonus_calc(position_table: [f32; 64], bitboard: &BitBoard) -> f32 {
350+
fn position_bonus_calc(position_table: [f32; 64], piece_bitboard: &BitBoard, color_bitboard: &BitBoard) -> f32 {
351351
//Essentially, gets the dot product between a "vector" of the bitboard (containing 64 0s and 1s) and the table with position bonus constants.
352352
let mut bonus: f32 = 0.;
353+
//Get's the bitboard with all piece positions, and runs bitwise and for the board having one's own colors.
354+
let mut piece_board: u64 = (piece_bitboard.reverse_colors().to_size(63) as u64) & (color_bitboard.reverse_colors().to_size(63) as u64);
353355
for i in 0..63 {
354-
//I'm pretty sure the bitboard and position_table have opposite orientationns. Regardless, flipping the bitboard significantly increased performance. I don't know if the bitboard gets only 1 colour, or both. If both then for some reason this still improves things.
355-
bonus += ((bitboard.reverse_colors().to_size(63) as u64) >> i & 1) as f32 * position_table[i];
356+
//I'm pretty sure the bitboard and position_table have opposite orientationns. Regardless, flipping the bitboard significantly increased performance.
357+
bonus += (piece_board >> 1 & 1) as f32 * position_table[i];
356358
}
357359
return bonus;
358360
}
359-
position_bonus += position_bonus_calc(position_bonus_table_pawn, board.pieces(Piece::Pawn));
360-
position_bonus += position_bonus_calc(position_bonus_table_knight, board.pieces(Piece::Knight));
361-
position_bonus += position_bonus_calc(position_bonus_table_rook, board.pieces(Piece::Rook));
362-
position_bonus += position_bonus_calc(position_bonus_table_king, board.pieces(Piece::King));
363-
position_bonus += position_bonus_calc(position_bonus_table_queen, board.pieces(Piece::Queen));
364-
position_bonus += position_bonus_calc(position_bonus_table_bishop, board.pieces(Piece::Bishop));
365-
361+
if board.side_to_move() == Color::White {
362+
position_bonus += position_bonus_calc(position_bonus_table_pawn, board.pieces(Piece::Pawn), board.color_combined(Color::White));
363+
position_bonus += position_bonus_calc(position_bonus_table_knight, board.pieces(Piece::Knight), board.color_combined(Color::White));
364+
position_bonus += position_bonus_calc(position_bonus_table_rook, board.pieces(Piece::Rook), board.color_combined(Color::White));
365+
position_bonus += position_bonus_calc(position_bonus_table_king, board.pieces(Piece::King), board.color_combined(Color::White));
366+
position_bonus += position_bonus_calc(position_bonus_table_queen, board.pieces(Piece::Queen), board.color_combined(Color::White));
367+
position_bonus += position_bonus_calc(position_bonus_table_bishop, board.pieces(Piece::Bishop), board.color_combined(Color::White));
368+
} else {
369+
position_bonus += position_bonus_calc(position_bonus_table_pawn, board.pieces(Piece::Pawn), board.color_combined(Color::Black));
370+
position_bonus += position_bonus_calc(position_bonus_table_knight, board.pieces(Piece::Knight), board.color_combined(Color::Black));
371+
position_bonus += position_bonus_calc(position_bonus_table_rook, board.pieces(Piece::Rook), board.color_combined(Color::Black));
372+
position_bonus += position_bonus_calc(position_bonus_table_king, board.pieces(Piece::King), board.color_combined(Color::Black));
373+
position_bonus += position_bonus_calc(position_bonus_table_queen, board.pieces(Piece::Queen), board.color_combined(Color::Black));
374+
position_bonus += position_bonus_calc(position_bonus_table_bishop, board.pieces(Piece::Bishop), board.color_combined(Color::Black));
375+
}
366376
}
367377

368378
let evaluation: f32 = controlled_squares as f32 / 20. + diff_material as f32 + position_bonus;

src/common/constants.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,18 @@ pub(crate) const position_bonus_table_bishop: [f32; 64] = [
4949
0., 0., 0., 0., 0., 0., 0., 0.,
5050
0., 0., 0., 0., 0., 0., 0., 0.,
5151
0., 0., 0., 0., 0., 0., 0., 0.,
52-
0., 0., 0., 0., 0., 0., 0., 0.,
53-
0., 0., 0., 0., 0., 0., 0., 0.,
54-
0., 0., 0., 0., 0., 0., 0., 0.
52+
0., 0.05, 0.1, 0., 0., 0.1, 0.05, 0.,
53+
0.05, 0.1, 0.05, 0., 0., 0.05, 0.1, 0.05,
54+
0., 0.05, 0., 0., 0., 0., 0.05, 0.
5555
];
5656

5757
pub(crate) const position_bonus_table_queen: [f32; 64] = [
5858
0., 0., 0., 0., 0., 0., 0., 0.,
5959
0., 0., 0., 0., 0., 0., 0., 0.,
60-
0., 0., 0., 0., 0., 0., 0., 0.,
61-
0., 0., 0., 0., 0., 0., 0., 0.,
62-
0., 0., 0., 0., 0., 0., 0., 0.,
63-
0., 0., 0., 0., 0., 0., 0., 0.,
60+
0., 0., 0.1, 0.1, 0.1, 0.1, 0., 0.,
61+
0., 0., 0.1, 0.1, 0.1, 0.1, 0., 0.,
62+
0., 0., 0.1, 0.1, 0.1, 0.1, 0., 0.,
63+
0., 0., 0.1, 0.1, 0.1, 0.1, 0., 0.,
6464
0., 0., 0., 0., 0., 0., 0., 0.,
6565
0., 0., 0., 0., 0., 0., 0., 0.
6666
];
@@ -73,6 +73,6 @@ pub(crate) const position_bonus_table_king: [f32; 64] = [
7373
0., 0., 0., 0., 0., 0., 0., 0.,
7474
0., 0., 0., 0., 0., 0., 0., 0.,
7575
0., 0., 0., 0., 0., 0., 0., 0.,
76-
0., 0., 0., 0., 0., 0., 0., 0.
76+
0.1, 0.3, 0.3, 0., 0., 0.3, 0.3, 0.1
7777
];
7878
}

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ async fn main() {
2727
// competition.analyze_algorithm_choices(|(game_info, _), _| {
2828
// game_info.outcome == GameOutcome::InconclusiveTooLong
2929
// });
30-
let results = competition.start_competition(1000).await;
30+
let results = competition.start_competition(500).await;
3131
dbg!(results);
3232
}

0 commit comments

Comments
 (0)