Spaces:
Sleeping
Sleeping
File size: 5,320 Bytes
3656ccf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | //! Position/move encodings — must match the Python tokenizers exactly
//! (`PostionTokenizer`, `alphazero_move_encoder.move_to_action_plane`).
use shakmaty::zobrist::{Zobrist64, ZobristHash};
use shakmaty::{CastlingSide, Chess, Color, EnPassantMode, Move, Position, Role};
pub const NUM_PLANES: usize = 73;
pub fn piece_token(role: Role, color: Color) -> u8 {
let base = match role {
Role::Pawn => 1,
Role::Knight => 2,
Role::Bishop => 3,
Role::Rook => 4,
Role::Queen => 5,
Role::King => 6,
};
if color == Color::White {
base
} else {
base + 6
}
}
/// (from_sq, to_sq, promotion) in python-chess conventions: castling is the
/// king's two-square move (e1g1), squares indexed a1=0..h8=63.
pub fn move_coords(m: &Move) -> (usize, usize, Option<Role>) {
match m {
Move::Castle { king, rook } => {
let k = u32::from(*king) as usize;
let r = u32::from(*rook) as usize;
let to = if r > k { k + 2 } else { k - 2 };
(k, to, None)
}
_ => (
u32::from(m.from().expect("no drops in chess")) as usize,
u32::from(m.to()) as usize,
m.promotion(),
),
}
}
/// AlphaZero 8x8x73 action plane (mirror of move_to_action_plane).
pub fn action_plane(from: usize, to: usize, promo: Option<Role>) -> usize {
let df = (to % 8) as i32 - (from % 8) as i32;
let dr = (to / 8) as i32 - (from / 8) as i32;
if let Some(p) = promo {
if p != Role::Queen {
let dir_idx = (df + 1) as usize;
let piece_idx = match p {
Role::Knight => 0,
Role::Bishop => 1,
Role::Rook => 2,
_ => unreachable!("queen handled above"),
};
return 64 + dir_idx * 3 + piece_idx;
}
}
const KNIGHT_JUMPS: [(i32, i32); 8] = [
(1, 2), (2, 1), (2, -1), (1, -2), (-1, -2), (-2, -1), (-2, 1), (-1, 2),
];
if let Some(i) = KNIGHT_JUMPS.iter().position(|&d| d == (df, dr)) {
return 56 + i;
}
const QUEEN_DIRS: [(i32, i32); 8] = [
(0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1),
];
let dist = df.abs().max(dr.abs());
let dir_f = if df != 0 { df / dist } else { 0 };
let dir_r = if dr != 0 { dr / dist } else { 0 };
let dir_idx = QUEEN_DIRS.iter().position(|&d| d == (dir_f, dir_r)).unwrap();
dir_idx * 7 + (dist as usize - 1)
}
pub fn encode_position(pos: &Chess) -> ([u8; 64], u8, u8, u8) {
let mut tokens = [0u8; 64];
let board = pos.board();
for sq in board.occupied() {
let piece = board.piece_at(sq).unwrap();
tokens[u32::from(sq) as usize] = piece_token(piece.role, piece.color);
}
let player = if pos.turn() == Color::White { 1 } else { 0 };
let castles = pos.castles();
let mut castling = 0u8;
if castles.has(Color::White, CastlingSide::KingSide) {
castling |= 1;
}
if castles.has(Color::White, CastlingSide::QueenSide) {
castling |= 2;
}
if castles.has(Color::Black, CastlingSide::KingSide) {
castling |= 4;
}
if castles.has(Color::Black, CastlingSide::QueenSide) {
castling |= 8;
}
let ep = pos
.ep_square(EnPassantMode::Legal)
.map(|s| (u32::from(s) % 8) as u8)
.unwrap_or(8);
(tokens, player, castling, ep)
}
pub fn zobrist(pos: &Chess) -> u64 {
let h: Zobrist64 = pos.zobrist_hash(EnPassantMode::Legal);
h.0
}
/// Terminal value from the side-to-move POV, or None. `keys` is the sequence
/// of zobrist keys of all positions seen (game history + search path),
/// including the current position — mirrors python-chess is_repetition(3)
/// seeing the search pushes on the shared board.
pub fn terminal_value(pos: &Chess, keys: &[u64], current: u64) -> Option<f32> {
if pos.legal_moves().is_empty() {
return Some(if pos.is_check() { -1.0 } else { 0.0 });
}
if pos.is_insufficient_material() || pos.halfmoves() >= 100 {
return Some(0.0);
}
if keys.iter().filter(|&&k| k == current).count() >= 3 {
return Some(0.0);
}
None
}
/// `terminal_value` for a position inside a search tree (Lc0's two-fold rule).
/// A position that repeats one already on the search path — root included,
/// i.e. `keys[root_idx..]` — is a draw at its second occurrence: whoever could
/// have deviated chose to repeat, and can keep repeating. Without this the
/// search sees perpetuals and repetition draws only once the cycle comes
/// round a third time, 4+ plies deeper than it needs to. Repetitions that
/// reach back into the pre-root game history still need the full threefold.
pub fn search_terminal_value(pos: &Chess, keys: &[u64], current: u64, root_idx: usize) -> Option<f32> {
if let Some(t) = terminal_value(pos, keys, current) {
return Some(t);
}
let prior = &keys[..keys.len().saturating_sub(1)];
if prior.len() > root_idx && prior[root_idx..].contains(¤t) {
return Some(0.0);
}
None
}
/// Half-move index of the position (0 = white's first move).
pub fn halfmove(pos: &Chess) -> u32 {
2 * (u32::from(pos.fullmoves()) - 1) + if pos.turn() == Color::White { 0 } else { 1 }
}
|