initial BVM usage (halfs performance :( )

This commit is contained in:
2025-02-20 16:40:11 -05:00
parent dc9432b07b
commit e1d83825b7
3 changed files with 54 additions and 23 deletions

View File

@@ -1,20 +1,33 @@
use crate::repr::{Board, Piece, PosMap};
pub struct BoardValueMap(PosMap<f32>);
pub struct BoardValueMap(PosMap<i8>);
impl BoardValueMap {
pub fn board_value(&self, board: &Board, color: Piece) -> f32 {
let mut board_value: f32 = 0.0;
for (i, j) in Board::all_positions() {
if let Some(pos_p) = board.get(i, j) {
pub fn board_value(&self, board: &Board, color: Piece) -> i8 {
Board::all_positions()
.filter_map(|(i, j)| board.get(i, j).map(|p| (i, j, p)))
.map(|(i, j, pos_p)| {
let mut value = *self.0.get(i, j);
if pos_p != color {
// enemy has position
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 super::board_value::BoardValueMap;
#[derive(Clone, Debug)]
pub struct Move {
/// `i` position of move
@@ -28,6 +32,10 @@ pub struct Move {
pub lazy_children: bool,
}
lazy_static! {
static ref BVM: BoardValueMap = BoardValueMap::new();
}
impl Move {
pub const fn coords(&self) -> (usize, usize) {
(self.i, self.j)
@@ -47,17 +55,6 @@ impl Move {
return 0;
}
let mut self_value = self.board.net_score(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
BVM.board_value(&self.board, agent_color) as i64
}
}