Skip to content

Commit

Permalink
Evaluate king safety
Browse files Browse the repository at this point in the history
Evaluate by attacks on the squares around the king.

Elo   | 24.27 +- 10.43 (95%)
SPRT  | 8.0+0.08s Threads=1 Hash=16MB
LLR   | 3.00 (-2.94, 2.94) [0.00, 5.00]
Games | N: 2452 W: 864 L: 693 D: 895
Penta | [78, 248, 464, 297, 139]
  • Loading branch information
jgilchrist committed Dec 4, 2024
1 parent a167027 commit f954772
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 155 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Tune all evaluation parameters with https://github.com/GediminasMasaitis/texel-tuner (53.17 +- 16.76)
* Evaluate piece mobility (41.36 +- 13.82)
* Add a texel tuner in-repo and tune, resolving an issue where mobility scores were not computed correctly (28.53 +- 11.44)
* Evaluate king safety (24.27 +- 10.43)

### Misc

Expand Down
39 changes: 0 additions & 39 deletions src/engine/eval/mobility.rs

This file was deleted.

52 changes: 52 additions & 0 deletions src/engine/eval/mobility_and_king_safety.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use super::PhasedEval;
use crate::chess::bitboard::Bitboard;
use crate::chess::game::Game;
use crate::chess::movegen::tables;
use crate::chess::player::Player;
use crate::engine::eval::params::{
ATTACKED_KING_SQUARES, BISHOP_MOBILITY, KNIGHT_MOBILITY, QUEEN_MOBILITY, ROOK_MOBILITY,
};

fn mobility_and_opp_king_safety_for(game: &Game, player: Player) -> PhasedEval {
let mut eval = PhasedEval::ZERO;
let blockers = game.board.occupancy();

let mut attacked_squares = Bitboard::EMPTY;

for p in game.board.knights(player) {
let moves = tables::knight_attacks(p);
attacked_squares |= moves;
eval += KNIGHT_MOBILITY[moves.count() as usize];
}

for p in game.board.bishops(player) {
let moves = tables::bishop_attacks(p, blockers);
attacked_squares |= moves;
eval += BISHOP_MOBILITY[moves.count() as usize];
}

for p in game.board.rooks(player) {
let moves = tables::rook_attacks(p, blockers);
attacked_squares |= moves;
eval += ROOK_MOBILITY[moves.count() as usize];
}

for p in game.board.queens(player) {
let moves = tables::bishop_attacks(p, blockers) | tables::rook_attacks(p, blockers);
attacked_squares |= moves;
eval += QUEEN_MOBILITY[moves.count() as usize];
}

let enemy_king = game.board.king(player.other()).single();
let enemy_king_surrounding_squares = tables::king_attacks(enemy_king);
let attacks_on_enemy_king = attacked_squares & enemy_king_surrounding_squares;

eval -= ATTACKED_KING_SQUARES[attacks_on_enemy_king.count() as usize];

eval
}

pub fn eval(game: &Game) -> PhasedEval {
mobility_and_opp_king_safety_for(game, Player::White)
- mobility_and_opp_king_safety_for(game, Player::Black)
}
4 changes: 2 additions & 2 deletions src/engine/eval/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod material;
mod mobility;
mod mobility_and_king_safety;
mod params;
mod phased_eval;
pub mod piece_square_tables;
Expand Down Expand Up @@ -60,7 +60,7 @@ pub fn eval(game: &Game) -> Eval {
pub fn absolute_eval(game: &Game) -> WhiteEval {
let eval = game.incremental_eval.piece_square_tables
+ material::eval(game)
+ mobility::eval(game);
+ mobility_and_king_safety::eval(game);

eval.for_phase(game.incremental_eval.phase_value)
}
Expand Down
Loading

0 comments on commit f954772

Please sign in to comment.