Skip to content

Commit

Permalink
Tweak king eval (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdmendes authored Sep 17, 2023
2 parents a5fe144 + c8aabc3 commit 6c64794
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 42 deletions.
20 changes: 3 additions & 17 deletions src/evaluation/position/king.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ fn king_pawn_shelter(position: &Position, king_color: Color, king_square: Square
let mut shelter = 0;

let our_pawns = position.board.pieces_bb(Piece::Pawn) & position.board.occupancy_bb(king_color);
let their_pawns =
position.board.pieces_bb(Piece::Pawn) & position.board.occupancy_bb(king_color.opposite());
let their_queen_rooks = (position.board.pieces_bb(Piece::Queen)
| position.board.pieces_bb(Piece::Rook))
& position.board.occupancy_bb(king_color.opposite());

let file_min = match king_square.file() {
0 => 0,
Expand All @@ -22,7 +17,6 @@ fn king_pawn_shelter(position: &Position, king_color: Color, king_square: Square
_ => king_square.file() + 1,
};

const OPEN_FILE_PENALTY: ValueScore = -20;
const SHELTER_PENALTY: ValueScore = -20;

for file in file_min..=file_max {
Expand All @@ -42,14 +36,7 @@ fn king_pawn_shelter(position: &Position, king_color: Color, king_square: Square
};
shelter += shelter_penalty;
} else {
let file_mask = Bitboard::file_mask(file);
let their_pawns_on_file = their_pawns & file_mask;
let their_queen_rooks_on_file = their_queen_rooks & file_mask;
if their_pawns_on_file.is_empty() && their_queen_rooks_on_file.is_not_empty() {
shelter += OPEN_FILE_PENALTY;
} else {
shelter += OPEN_FILE_PENALTY / 2;
}
shelter += SHELTER_PENALTY;
}
}

Expand All @@ -64,8 +51,7 @@ fn king_tropism(position: &Position, king_color: Color, king_square: Square) ->
let tropism = them_occupancy.fold(0, |acc, sq| {
let distance = sq.distance(king_square);
let piece_cof = match position.board.piece_at(sq) {
Some(Piece::Queen) => 3,
Some(Piece::Rook) => 2,
Some(Piece::Queen) | Some(Piece::Rook) => 2,
Some(Piece::Bishop) | Some(Piece::Knight) => 1,
_ => unreachable!(),
};
Expand Down Expand Up @@ -183,7 +169,7 @@ mod tests {
Position::from_fen("r4r1k/1p2p1pp/p2p2b1/3P4/6P1/PNP1q1P1/1P3R2/R2Q2K1 w - - 1 22")
.unwrap();

assert!((-80..=-40).contains(&position_shelter(&position)));
assert!((-120..=-50).contains(&position_shelter(&position)));
}

#[test]
Expand Down
61 changes: 36 additions & 25 deletions src/evaluation/position/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod pawns;

pub const MAX_POSITIONAL_GAIN: ValueScore = 300;

pub fn midgame_ratio(position: &Position) -> u8 {
fn midgame_ratio(position: &Position) -> u8 {
Piece::list().iter().fold(0, |acc, piece| {
acc.saturating_add(
position.board.pieces_bb(*piece).count_ones() as u8
Expand All @@ -26,23 +26,40 @@ pub fn midgame_ratio(position: &Position) -> u8 {
})
}

fn mobility_bonus(piece: Piece) -> ValueScore {
match piece {
Piece::Pawn => 0,
Piece::Bishop => 3,
Piece::Knight | Piece::Rook => 2,
Piece::Queen => 1,
Piece::King => 0,
}
}

fn insufficient_material(position: &Position) -> bool {
let pieces_count = position.board.occupancy_bb_all().count_ones();

if pieces_count > 4 {
return false;
}

let knights_bb = position.board.pieces_bb(Piece::Knight);
if knights_bb.count_ones() == 2 {
return true;
}

let bishops_bb = position.board.pieces_bb(Piece::Bishop);
if pieces_count == 3 && (knights_bb | bishops_bb).is_not_empty() {
return true;
}

false
}

impl Evaluable for Position {
fn value(&self) -> ValueScore {
// Insufficient material
if self.board.occupancy_bb_all().count_ones() <= 4 {
// Two knights vs king
let knights_bb = self.board.pieces_bb(Piece::Knight);
if knights_bb.count_ones() == 2 {
return 0;
}

// Knight/bishop vs king
let bishops_bb = self.board.pieces_bb(Piece::Bishop);
if self.board.occupancy_bb_all().count_ones() == 3
&& (knights_bb | bishops_bb).is_not_empty()
{
return 0;
}
if insufficient_material(self) {
return 0;
}

let midgame_ratio = midgame_ratio(self);
Expand Down Expand Up @@ -75,20 +92,14 @@ impl Evaluable for Position {
let mobility_bonus = if *piece == Piece::Pawn {
0
} else {
let piece_mobility_bonus = match *piece {
Piece::Bishop => 4,
Piece::Rook | Piece::Knight => 3,
Piece::Queen => 2,
Piece::King => -1,
_ => unreachable!(),
};
let mobility_bonus = mobility_bonus(*piece);
let white_mobility_bonus = white_pieces.into_iter().fold(0, |acc, square| {
let attacks = piece_attacks(*piece, square, occupancy) & !white_occupancy;
acc + attacks.count_ones() as ValueScore * piece_mobility_bonus
acc + attacks.count_ones() as ValueScore * mobility_bonus
});
let black_mobility_bonus = black_pieces.into_iter().fold(0, |acc, square| {
let attacks = piece_attacks(*piece, square, occupancy) & !black_occupancy;
acc + attacks.count_ones() as ValueScore * piece_mobility_bonus
acc + attacks.count_ones() as ValueScore * mobility_bonus
});
white_mobility_bonus - black_mobility_bonus
};
Expand Down

0 comments on commit 6c64794

Please sign in to comment.