From 366bba8426cd766e40db964be103098a3f6148d3 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Fri, 7 Feb 2025 00:31:25 -0500 Subject: [PATCH] fiddling with the algo --- src/complexagent.rs | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/src/complexagent.rs b/src/complexagent.rs index 46b0f77..b5b9f22 100644 --- a/src/complexagent.rs +++ b/src/complexagent.rs @@ -1,3 +1,5 @@ +use num::CheckedDiv; + use crate::{agent::Agent, board::Board, piece::Piece}; pub struct ComplexAgent { @@ -29,22 +31,31 @@ impl Move { .for_each(|x| x.populate_next_moves(i - 1)); } - fn value(&self, from_perspective_of: Piece) -> i64 { + fn value(&self, agent_color: Piece, depth: usize) -> i64 { let mut captured_value = self.captured as i64; - if from_perspective_of != self.color { + if agent_color != self.color { captured_value = -captured_value; } - let value = captured_value - + self - .next_move - .iter() - .map(|x| x.value(from_perspective_of)) - .max() - .unwrap_or(0); + // Reduce value of capture based on depth of prediction + captured_value /= depth as i64; - value + let avg_next_move_value = (self + .next_move + .iter() + .map(|x| x.value(agent_color, depth + 1)) + .sum::() + .checked_div(self.next_move.len() as i64)) + .unwrap_or(0); + + captured_value + avg_next_move_value + } + + fn best_move(&self, agent_color: Piece) -> Option<&Move> { + self.next_move + .iter() + .max_by_key(|m| m.value(agent_color, 1)) } pub fn len(&self) -> u32 { @@ -111,10 +122,7 @@ impl Agent for ComplexAgent { other_player_move.populate_next_moves(LOOPS); println!("depth: {}", other_player_move.len()); - self.curr_move = other_player_move - .next_move - .into_iter() - .max_by_key(|m| m.value(self.color)); + self.curr_move = other_player_move.best_move(self.color).cloned(); self.curr_move.as_ref().map(Move::coords) }