improve PosMap

This commit is contained in:
Simon Gardling 2025-03-20 01:12:27 -04:00
parent 4db6f51c9c
commit f51c940b3f
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D
5 changed files with 35 additions and 27 deletions

7
Cargo.lock generated
View File

@ -145,6 +145,12 @@ dependencies = [
"windows-sys", "windows-sys",
] ]
[[package]]
name = "const_for"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9c50fcfdf972929aff202c16b80086aa3cfc6a3a820af714096c58c7c1d0582"
[[package]] [[package]]
name = "criterion" name = "criterion"
version = "0.5.1" version = "0.5.1"
@ -443,6 +449,7 @@ dependencies = [
"allocative", "allocative",
"arrayvec", "arrayvec",
"console", "console",
"const_for",
"criterion", "criterion",
"crossbeam-channel", "crossbeam-channel",
"either", "either",

View File

@ -25,6 +25,7 @@ debug = true
allocative = "0.3" allocative = "0.3"
arrayvec = "0.7" arrayvec = "0.7"
console = "0.15" console = "0.15"
const_for = "0.1"
crossbeam-channel = "0.5" crossbeam-channel = "0.5"
either = "1.13" either = "1.13"
indicatif = { version = "0.17", features = [ "rayon" ] } indicatif = { version = "0.17", features = [ "rayon" ] }

View File

@ -19,7 +19,7 @@ impl BoardValueMap {
} }
/// Weights from: https://repub.eur.nl/pub/7142/ei2005-47.pdf /// Weights from: https://repub.eur.nl/pub/7142/ei2005-47.pdf
pub fn new() -> Self { pub const fn weighted() -> Self {
const POSITION_VALUES: [[i16; 8]; 8] = [ const POSITION_VALUES: [[i16; 8]; 8] = [
[100, -20, 10, 5, 5, 10, -20, 100], [100, -20, 10, 5, 5, 10, -20, 100],
[-20, -50, -2, -2, -2, -2, -50, -20], [-20, -50, -2, -2, -2, -2, -50, -20],
@ -32,4 +32,10 @@ impl BoardValueMap {
]; ];
Self(PosMap::from(POSITION_VALUES)) Self(PosMap::from(POSITION_VALUES))
} }
pub const fn flat() -> Self {
Self(PosMap::from(
[[1; Board::SIZE as usize]; Board::SIZE as usize],
))
}
} }

View File

@ -37,7 +37,7 @@ pub struct Move {
pub is_trimmed: bool, pub is_trimmed: bool,
} }
static BVM: LazyLock<BoardValueMap> = LazyLock::new(BoardValueMap::new); const BVM: BoardValueMap = BoardValueMap::weighted();
impl Move { impl Move {
pub fn new(coord: Option<CoordPair>, board: Board, color: Piece, agent_color: Piece) -> Self { pub fn new(coord: Option<CoordPair>, board: Board, color: Piece, agent_color: Piece) -> Self {

View File

@ -1,45 +1,39 @@
use super::{bitboard::BitBoard, piece::Piece, CoordAxis, CoordPair}; use super::{bitboard::BitBoard, piece::Piece, CoordAxis, CoordPair};
use allocative::Allocative; use allocative::Allocative;
use arrayvec::ArrayVec; use const_for::const_for;
use rand::seq::IteratorRandom; use rand::seq::IteratorRandom;
use std::{cmp::Ordering, fmt}; use std::{cmp::Ordering, fmt, mem::MaybeUninit};
/// Map of all points on the board against some type T /// Map of all points on the board against some type T
/// Used to index like so: example[i][j] /// Used to index like so: example[i][j]
/// with each coordinate /// with each coordinate
pub struct PosMap<T>(ArrayVec<T, { Board::AREA.0 as usize }>); pub struct PosMap<T>([T; Board::AREA.0 as usize]);
impl<T: Default> PosMap<T> { impl<T: Copy> PosMap<T> {
#[allow(clippy::new_without_default)] pub const unsafe fn uninit() -> Self {
pub fn new() -> Self { Self([MaybeUninit::zeroed().assume_init(); Board::AREA.0 as usize])
Self(ArrayVec::from_iter(
(0..Board::AREA.0).map(|_| Default::default()),
))
} }
pub fn get(&self, coords: CoordPair) -> &T { pub const fn from(v: [[T; Board::SIZE as usize]; Board::SIZE as usize]) -> Self {
let mut n = unsafe { Self::uninit() };
const_for!(i in 0..Board::SIZE => {
const_for!(j in 0..Board::SIZE => {
n.set(CoordPair::from_axes(i, j), v[i as usize][j as usize]);
});
});
n
}
pub const fn get(&self, coords: CoordPair) -> &T {
&self.0[coords.0 as usize] &self.0[coords.0 as usize]
} }
pub fn set(&mut self, coords: CoordPair, value: T) { pub const fn set(&mut self, coords: CoordPair, value: T) {
self.0[coords.0 as usize] = value; self.0[coords.0 as usize] = value;
} }
} }
type PosMapOrig<T> = [[T; Board::SIZE as usize]; Board::SIZE as usize];
impl<T: Default + Copy> From<PosMapOrig<T>> for PosMap<T> {
fn from(value: PosMapOrig<T>) -> Self {
let mut new = Self::new();
for i in 0..Board::SIZE {
for j in 0..Board::SIZE {
new.set((i, j).into(), value[i as usize][j as usize]);
}
}
new
}
}
#[derive(PartialEq, Eq, Copy, Clone, Debug, Allocative)] #[derive(PartialEq, Eq, Copy, Clone, Debug, Allocative)]
pub enum Winner { pub enum Winner {
Player(Piece), Player(Piece),