fiddling with the algo

This commit is contained in:
Simon Gardling 2025-02-07 00:31:25 -05:00
parent 4407af62ab
commit 366bba8426
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D

View File

@ -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::<i64>()
.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)
}