initial BVM usage (halfs performance :( )

This commit is contained in:
Simon Gardling 2025-02-20 16:40:11 -05:00
parent dc9432b07b
commit e1d83825b7
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D
3 changed files with 54 additions and 23 deletions

View File

@ -1,20 +1,33 @@
use crate::repr::{Board, Piece, PosMap}; use crate::repr::{Board, Piece, PosMap};
pub struct BoardValueMap(PosMap<f32>); pub struct BoardValueMap(PosMap<i8>);
impl BoardValueMap { impl BoardValueMap {
pub fn board_value(&self, board: &Board, color: Piece) -> f32 { pub fn board_value(&self, board: &Board, color: Piece) -> i8 {
let mut board_value: f32 = 0.0; Board::all_positions()
for (i, j) in Board::all_positions() { .filter_map(|(i, j)| board.get(i, j).map(|p| (i, j, p)))
if let Some(pos_p) = board.get(i, j) { .map(|(i, j, pos_p)| {
let mut value = *self.0.get(i, j); let mut value = *self.0.get(i, j);
if pos_p != color { if pos_p != color {
// enemy has position // enemy has position
value = -value; value = -value;
} }
board_value += value; value
})
.sum()
} }
pub fn new() -> Self {
let mut map = PosMap::new();
for (i, j) in Board::all_positions() {
map.set(i, j, 1);
} }
board_value
for (i, j) in Board::sides() {
map.set(i, j, 4);
}
Self(map)
} }
} }

View File

@ -1,5 +1,9 @@
use lazy_static::lazy_static;
use crate::repr::{Board, Piece, Winner}; use crate::repr::{Board, Piece, Winner};
use super::board_value::BoardValueMap;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Move { pub struct Move {
/// `i` position of move /// `i` position of move
@ -28,6 +32,10 @@ pub struct Move {
pub lazy_children: bool, pub lazy_children: bool,
} }
lazy_static! {
static ref BVM: BoardValueMap = BoardValueMap::new();
}
impl Move { impl Move {
pub const fn coords(&self) -> (usize, usize) { pub const fn coords(&self) -> (usize, usize) {
(self.i, self.j) (self.i, self.j)
@ -47,17 +55,6 @@ impl Move {
return 0; return 0;
} }
let mut self_value = self.board.net_score(agent_color) as i64; BVM.board_value(&self.board, agent_color) as i64
let corner_v_agent = Board::sides()
.filter(|&(i, j)| self.board.get_piece(i, j, agent_color))
.count() as i64;
let corner_v_not_agent = Board::sides()
.filter(|&(i, j)| self.board.get_piece(i, j, !agent_color))
.count() as i64;
// make net-corner capture important
self_value += (corner_v_agent - corner_v_not_agent) * 4;
self_value
} }
} }

View File

@ -26,11 +26,21 @@ type ChainCollection = ArrayVec<Chain, 8>;
/// 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>); pub struct PosMap<T: Default>(ArrayVec<T, BOARD_AREA>);
impl<T: Default> PosMap<T> {
const fn index(row: usize, col: usize) -> usize {
row * BOARD_SIZE + col
}
pub fn new() -> Self {
Self(ArrayVec::from_iter(
(0..BOARD_AREA).map(|_| Default::default()),
))
}
impl<T> PosMap<T> {
pub fn get(&self, row: usize, col: usize) -> &T { pub fn get(&self, row: usize, col: usize) -> &T {
let index = row * BOARD_SIZE + col; let index = Self::index(row, col);
debug_assert!( debug_assert!(
BOARD_AREA + 1 >= index, BOARD_AREA + 1 >= index,
@ -40,6 +50,17 @@ impl<T> PosMap<T> {
unsafe { self.0.get_unchecked(index) } unsafe { self.0.get_unchecked(index) }
} }
pub fn set(&mut self, row: usize, col: usize, value: T) {
let index = Self::index(row, col);
debug_assert!(
BOARD_AREA + 1 >= index,
"index out of range, was: {}",
index
);
self.0[index] = value;
}
} }
/// Creates a lookup map for adjacencies and chains from each position on the board /// Creates a lookup map for adjacencies and chains from each position on the board