This commit is contained in:
Simon Gardling 2025-02-22 18:36:59 -05:00
parent 2305a8065f
commit 6d9be3dbe6
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D
6 changed files with 25 additions and 19 deletions

View File

@ -7,7 +7,7 @@ use std::time::Duration;
fn extend_layers_test(depth: usize, expire: usize) { fn extend_layers_test(depth: usize, expire: usize) {
let mut fut = FutureMoves::new(Piece::Black, depth, expire); 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(); fut.extend_layers();
} }

View File

@ -23,7 +23,7 @@ impl ComplexAgent {
impl Agent for ComplexAgent { impl Agent for ComplexAgent {
fn next_move(&mut self, board: &Board) -> Option<(usize, usize)> { 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()); println!("# of moves stored: {}", self.future_moves.arena_len());

View File

@ -1,11 +1,9 @@
use std::collections::HashMap;
use indicatif::{ProgressIterator, ProgressStyle};
use crate::{ use crate::{
logic::r#move::Move, logic::r#move::Move,
repr::{Board, Piece, Winner}, repr::{Board, Piece, Winner},
}; };
use indicatif::{ProgressIterator, ProgressStyle};
use std::collections::HashMap;
pub struct FutureMoves { pub struct FutureMoves {
/// Arena containing all [`Move`] /// Arena containing all [`Move`]
@ -39,6 +37,7 @@ impl FutureMoves {
} }
} }
/// Return the length of the Arena
pub fn arena_len(&self) -> usize { pub fn arena_len(&self) -> usize {
self.arena.len() self.arena.len()
} }
@ -165,6 +164,8 @@ impl FutureMoves {
depth depth
} }
/// Compute `Move.value`, propegating upwards from the furthest out Moves
/// in the Arena.
fn compute_values(&mut self, indexes: impl Iterator<Item = usize>) { fn compute_values(&mut self, indexes: impl Iterator<Item = usize>) {
// PERF! pre-organize all indexes based on what depth they're at // 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 // 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)> { pub fn best_move(&self) -> Option<(usize, usize)> {
self.current_root self.current_root
.and_then(|x| { .and_then(|x| {
@ -228,7 +230,7 @@ impl FutureMoves {
/// Updates `FutureMoves` based on the current state of the board /// Updates `FutureMoves` based on the current state of the board
/// The board is supposed to be after the opposing move /// 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 let curr_board = self
.arena .arena
.iter() .iter()
@ -239,14 +241,14 @@ impl FutureMoves {
.map(|(idx, _)| idx); .map(|(idx, _)| idx);
if let Some(curr_board_idx) = curr_board { if let Some(curr_board_idx) = curr_board {
self.update_root_idx(curr_board_idx); self.set_root_idx_raw(curr_board_idx);
} else { } else {
self.create_root_raw(*board); self.set_root_from_board(*board);
self.update_root_idx(0);
} }
} }
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.clear();
self.arena.push(Move::new( self.arena.push(Move::new(
0, 0,
@ -257,6 +259,7 @@ impl FutureMoves {
self.agent_color, self.agent_color,
None, None,
)); ));
self.set_root_idx_raw(0);
} }
/// Update the root based on the coordinate of the move /// Update the root based on the coordinate of the move
@ -277,12 +280,14 @@ impl FutureMoves {
.is_some() .is_some()
} }
/// Update current root without modifying or pruning the Arena
fn update_root_idx_raw(&mut self, idx: usize) { fn update_root_idx_raw(&mut self, idx: usize) {
self.current_root = Some(idx); self.current_root = Some(idx);
self.current_depth -= self.depth_of(idx) - 1; 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.update_root_idx_raw(idx);
self.refocus_tree(); self.refocus_tree();

View File

@ -1,8 +1,6 @@
use lazy_static::lazy_static;
use crate::repr::{Board, Piece, Winner};
use super::board_value::BoardValueMap; use super::board_value::BoardValueMap;
use crate::repr::{Board, Piece, Winner};
use lazy_static::lazy_static;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Move { pub struct Move {
@ -27,10 +25,13 @@ pub struct Move {
/// Value of this move (including children) /// Value of this move (including children)
pub value: i128, pub value: i128,
/// What is the inherit value of this move (not including children)
pub self_value: i64, pub self_value: i64,
/// Which color made a move on this move?
pub color: Piece, pub color: Piece,
/// Should the children of this move be lazily generated?
pub lazy_children: bool, pub lazy_children: bool,
} }
@ -69,7 +70,7 @@ impl Move {
(self.i, self.j) (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 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.

View File

@ -1,4 +1,4 @@
use crate::repr::board::Board; use super::board::Board;
use const_fn::const_fn; use const_fn::const_fn;
use static_assertions::const_assert; use static_assertions::const_assert;

View File

@ -1,4 +1,4 @@
use crate::repr::{ use super::{
bitboard::BitBoard, bitboard::BitBoard,
misc::{diag_raw, split_from}, misc::{diag_raw, split_from},
piece::Piece, piece::Piece,