From 9ffc6ecd016fc72b46949a8338ae26ed2f0bb074 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Sat, 15 Feb 2025 23:31:18 -0500 Subject: [PATCH] prune agent move tree (8->17 depth) --- src/complexagent.rs | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/src/complexagent.rs b/src/complexagent.rs index 15f7fa4..71f082d 100644 --- a/src/complexagent.rs +++ b/src/complexagent.rs @@ -30,7 +30,7 @@ struct Move { impl Move { fn compute_self_value(&self, agent_color: Piece, depth: usize) -> i64 { - let mut self_value = self.board.count(agent_color) as i64; + let mut self_value = self.value; if self.winner == Some(!agent_color) { // if this board results in the opponent winning, MAJORLY negatively weigh this move @@ -100,6 +100,14 @@ impl FutureMoves { let arena_len = self.arena.len(); next_nodes = next_nodes .into_iter() + .progress_with( + ProgressBar::new(arena_len as u64).with_style( + ProgressStyle::with_template( + "Generating children: ({pos}/{len}) {per_sec}", + ) + .unwrap(), + ), + ) .flat_map(|node_idx| { self.generate_children( Some(node_idx), @@ -107,9 +115,6 @@ impl FutureMoves { next_color, ) }) - .progress_with(ProgressBar::new(arena_len as u64).with_style( - ProgressStyle::with_template("Children: ({pos}/{len}) {per_sec}").unwrap(), - )) .collect(); next_color = !next_color; } @@ -122,7 +127,7 @@ impl FutureMoves { color: Piece, ) -> impl Iterator { let start_idx = self.arena.len(); - self.arena.extend( + let mut new: Vec = // 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`] Board::all_positions() @@ -134,9 +139,18 @@ impl FutureMoves { winner: new_board.game_winner(color), parent, children: Vec::new(), - value: 0, - }), - ); + value: new_board.count(self.agent_color) as i64 - new_board.count(!self.agent_color) as i64, + }).collect(); + + // we want to keep only the best move of the agent + if color == self.agent_color { + if new.len() > 1 { + new.sort_by_key(|x| x.compute_self_value(self.agent_color, 1)); + new.drain(1..); + } + } + + self.arena.extend(new); let new_indices = start_idx..self.arena.len(); @@ -277,7 +291,7 @@ pub struct ComplexAgent { impl ComplexAgent { pub const fn new(color: Piece) -> Self { - const MAX_DEPTH: usize = 8; + const MAX_DEPTH: usize = 17; const MAX_ARENA: usize = 100_000_000; Self { color,