From 6d9be3dbe6e386f4ced17608d12a54a36bf971f5 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Sat, 22 Feb 2025 18:36:59 -0500 Subject: [PATCH] nits --- benches/future_children.rs | 2 +- src/complexagent.rs | 2 +- src/logic/future_moves.rs | 25 +++++++++++++++---------- src/logic/move.rs | 11 ++++++----- src/repr/bitboard.rs | 2 +- src/repr/board.rs | 2 +- 6 files changed, 25 insertions(+), 19 deletions(-) diff --git a/benches/future_children.rs b/benches/future_children.rs index ee0bf8c..003e7e3 100644 --- a/benches/future_children.rs +++ b/benches/future_children.rs @@ -7,7 +7,7 @@ use std::time::Duration; fn extend_layers_test(depth: usize, expire: usize) { let mut fut = FutureMoves::new(Piece::Black, depth, expire); - fut.create_root_raw(Board::new().starting_pos()); + fut.set_root_from_board(Board::new().starting_pos()); fut.extend_layers(); } diff --git a/src/complexagent.rs b/src/complexagent.rs index 19295a4..a5d5458 100644 --- a/src/complexagent.rs +++ b/src/complexagent.rs @@ -23,7 +23,7 @@ impl ComplexAgent { impl Agent for ComplexAgent { fn next_move(&mut self, board: &Board) -> Option<(usize, usize)> { - self.future_moves.update(board); + self.future_moves.update_from_board(board); println!("# of moves stored: {}", self.future_moves.arena_len()); diff --git a/src/logic/future_moves.rs b/src/logic/future_moves.rs index 3178d0e..578f92b 100644 --- a/src/logic/future_moves.rs +++ b/src/logic/future_moves.rs @@ -1,11 +1,9 @@ -use std::collections::HashMap; - -use indicatif::{ProgressIterator, ProgressStyle}; - use crate::{ logic::r#move::Move, repr::{Board, Piece, Winner}, }; +use indicatif::{ProgressIterator, ProgressStyle}; +use std::collections::HashMap; pub struct FutureMoves { /// Arena containing all [`Move`] @@ -39,6 +37,7 @@ impl FutureMoves { } } + /// Return the length of the Arena pub fn arena_len(&self) -> usize { self.arena.len() } @@ -165,6 +164,8 @@ impl FutureMoves { depth } + /// Compute `Move.value`, propegating upwards from the furthest out Moves + /// in the Arena. fn compute_values(&mut self, indexes: impl Iterator) { // PERF! pre-organize all indexes based on what depth they're at // previously, I did a lookup map based on if a node was visited, still resulted in a full @@ -209,6 +210,7 @@ impl FutureMoves { } } + /// Return the best move which is a child of `self.current_root` pub fn best_move(&self) -> Option<(usize, usize)> { self.current_root .and_then(|x| { @@ -228,7 +230,7 @@ impl FutureMoves { /// Updates `FutureMoves` based on the current state of the board /// The board is supposed to be after the opposing move - pub fn update(&mut self, board: &Board) { + pub fn update_from_board(&mut self, board: &Board) { let curr_board = self .arena .iter() @@ -239,14 +241,14 @@ impl FutureMoves { .map(|(idx, _)| idx); if let Some(curr_board_idx) = curr_board { - self.update_root_idx(curr_board_idx); + self.set_root_idx_raw(curr_board_idx); } else { - self.create_root_raw(*board); - self.update_root_idx(0); + self.set_root_from_board(*board); } } - pub fn create_root_raw(&mut self, board: Board) { + /// Clear the arena and create and set a root which contains a Board + pub fn set_root_from_board(&mut self, board: Board) { self.arena.clear(); self.arena.push(Move::new( 0, @@ -257,6 +259,7 @@ impl FutureMoves { self.agent_color, None, )); + self.set_root_idx_raw(0); } /// Update the root based on the coordinate of the move @@ -277,12 +280,14 @@ impl FutureMoves { .is_some() } + /// Update current root without modifying or pruning the Arena fn update_root_idx_raw(&mut self, idx: usize) { self.current_root = Some(idx); self.current_depth -= self.depth_of(idx) - 1; } - fn update_root_idx(&mut self, idx: usize) { + /// Update current root index while pruning and extending the tree (also recalculate values) + fn set_root_idx_raw(&mut self, idx: usize) { self.update_root_idx_raw(idx); self.refocus_tree(); diff --git a/src/logic/move.rs b/src/logic/move.rs index d576292..c532f04 100644 --- a/src/logic/move.rs +++ b/src/logic/move.rs @@ -1,8 +1,6 @@ -use lazy_static::lazy_static; - -use crate::repr::{Board, Piece, Winner}; - use super::board_value::BoardValueMap; +use crate::repr::{Board, Piece, Winner}; +use lazy_static::lazy_static; #[derive(Clone, Debug)] pub struct Move { @@ -27,10 +25,13 @@ pub struct Move { /// Value of this move (including children) pub value: i128, + /// What is the inherit value of this move (not including children) pub self_value: i64, + /// Which color made a move on this move? pub color: Piece, + /// Should the children of this move be lazily generated? pub lazy_children: bool, } @@ -69,7 +70,7 @@ impl Move { (self.i, self.j) } - pub fn compute_self_value(&self, agent_color: Piece) -> i64 { + fn compute_self_value(&self, agent_color: Piece) -> i64 { if self.winner == Winner::Player(!agent_color) { // 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. diff --git a/src/repr/bitboard.rs b/src/repr/bitboard.rs index 83e5573..f19f620 100644 --- a/src/repr/bitboard.rs +++ b/src/repr/bitboard.rs @@ -1,4 +1,4 @@ -use crate::repr::board::Board; +use super::board::Board; use const_fn::const_fn; use static_assertions::const_assert; diff --git a/src/repr/board.rs b/src/repr/board.rs index 70693aa..88b74ef 100644 --- a/src/repr/board.rs +++ b/src/repr/board.rs @@ -1,4 +1,4 @@ -use crate::repr::{ +use super::{ bitboard::BitBoard, misc::{diag_raw, split_from}, piece::Piece,