FutureMoves: initial move to not storing board

This commit is contained in:
Simon Gardling 2025-03-06 16:23:49 -05:00
parent 6958df9df4
commit efd762e5d2
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D
2 changed files with 14 additions and 15 deletions

View File

@ -19,6 +19,8 @@ pub struct FutureMoves {
agent_color: Piece, agent_color: Piece,
config: FutureMoveConfig, config: FutureMoveConfig,
board: Board,
} }
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
@ -87,6 +89,7 @@ impl FutureMoves {
current_depth: 0, current_depth: 0,
agent_color, agent_color,
config, config,
board: Board::new(),
} }
} }
@ -196,13 +199,15 @@ impl FutureMoves {
let parent = &self.arena[parent_idx]; let parent = &self.arena[parent_idx];
let new_color = !parent.color; let new_color = !parent.color;
let parent_board = self
.get_board_from_idx(parent_idx)
.expect("unable to get board");
// use [`Board::all_positions`] here instead of [`Board::possible_moves`] // use [`Board::all_positions`] here instead of [`Board::possible_moves`]
// because we use [`Board::what_if`] later and we want to reduce calls to [`Board::propegate_from_dry`] // because we use [`Board::what_if`] later and we want to reduce calls to [`Board::propegate_from_dry`]
let mut new: Vec<Move> = Board::all_positions() let mut new: Vec<Move> = Board::all_positions()
.flat_map(|coord| { .flat_map(|coord| {
parent parent_board
.board
.what_if(coord, new_color) .what_if(coord, new_color)
.map(move |x| (coord, x)) .map(move |x| (coord, x))
}) })
@ -212,7 +217,7 @@ impl FutureMoves {
.collect(); .collect();
if new.is_empty() { if new.is_empty() {
new.push(Move::new(None, parent.board, new_color, self.agent_color)); new.push(Move::new(None, parent_board, new_color, self.agent_color));
} }
let start_idx = self.arena.len(); let start_idx = self.arena.len();
@ -321,7 +326,7 @@ impl FutureMoves {
fn get_board_from_idx(&self, idx: usize) -> Option<Board> { fn get_board_from_idx(&self, idx: usize) -> Option<Board> {
if let Some(hist) = self.move_history(idx) { if let Some(hist) = self.move_history(idx) {
let mut board = self.arena[self.current_root.unwrap()].board; let mut board = self.board;
for (m, c) in hist { for (m, c) in hist {
if let Some(m) = m { if let Some(m) = m {
board.place(m, c).expect("move would not propegate"); board.place(m, c).expect("move would not propegate");
@ -361,7 +366,8 @@ impl FutureMoves {
// match the agent_color usually root or great-grand child // match the agent_color usually root or great-grand child
.filter(|&idx| self.depth_of(idx) % 2 == 0) .filter(|&idx| self.depth_of(idx) % 2 == 0)
.find(|&idx| { .find(|&idx| {
&self.arena[idx].board == board && self.arena[idx].color == !self.agent_color self.get_board_from_idx(idx).as_ref() == Some(board)
&& self.arena[idx].color == !self.agent_color
}); });
if let Some(curr_board_idx) = curr_board { if let Some(curr_board_idx) = curr_board {
@ -577,7 +583,6 @@ mod tests {
futm.arena.push(Move { futm.arena.push(Move {
coord: None, coord: None,
board: Board::new(),
winner: Winner::None, winner: Winner::None,
parent: None, parent: None,
children: Vec::new(), children: Vec::new(),
@ -664,7 +669,6 @@ mod tests {
futm.arena.push(Move { futm.arena.push(Move {
coord: None, coord: None,
board: Board::new(),
winner: Winner::None, winner: Winner::None,
parent: None, parent: None,
children: vec![], children: vec![],
@ -705,7 +709,6 @@ mod tests {
futm.arena.push(Move { futm.arena.push(Move {
coord: None, coord: None,
board: Board::new(),
winner: Winner::None, winner: Winner::None,
parent: None, parent: None,
children: vec![1], children: vec![1],

View File

@ -7,9 +7,6 @@ pub struct Move {
/// Coordinates (i, j) of the move (if it exists) /// Coordinates (i, j) of the move (if it exists)
pub coord: Option<CoordPair>, pub coord: Option<CoordPair>,
/// [`Board`] state after move is made
pub board: Board,
/// Current winner of the match /// Current winner of the match
pub winner: Winner, pub winner: Winner,
@ -41,7 +38,6 @@ 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 {
let mut m = Move { let mut m = Move {
coord, coord,
board,
winner: board.game_winner(), winner: board.game_winner(),
parent: None, parent: None,
children: Vec::new(), children: Vec::new(),
@ -51,11 +47,11 @@ impl Move {
self_value: 0, self_value: 0,
tried_children: false, tried_children: false,
}; };
m.self_value = m.compute_self_value(agent_color); m.self_value = m.compute_self_value(agent_color, &board);
m m
} }
fn compute_self_value(&self, agent_color: Piece) -> i16 { fn compute_self_value(&self, agent_color: Piece, board: &Board) -> i16 {
if self.winner == Winner::Player(!agent_color) { if self.winner == Winner::Player(!agent_color) {
// if this board results in the opponent winning, MAJORLY negatively weigh this move // if this board results in the opponent winning, MAJORLY negatively weigh this move
// NOTE! this branch isn't completely deleted because if so, the bot wouldn't make a move. // NOTE! this branch isn't completely deleted because if so, the bot wouldn't make a move.
@ -72,7 +68,7 @@ impl Move {
// I guess ignore Ties here, don't give them an explicit value, // I guess ignore Ties here, don't give them an explicit value,
// because even in the case of ties, we want to have a higher score // because even in the case of ties, we want to have a higher score
BVM.board_value(&self.board, agent_color) BVM.board_value(board, agent_color)
} }
/// Sort children of the [`Move`] by their self_value in `arena` /// Sort children of the [`Move`] by their self_value in `arena`