prune agent move tree (8->17 depth)

This commit is contained in:
Simon Gardling 2025-02-15 23:31:18 -05:00
parent e257f81fcb
commit 9ffc6ecd01
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D

View File

@ -30,7 +30,7 @@ struct Move {
impl Move { impl Move {
fn compute_self_value(&self, agent_color: Piece, depth: usize) -> i64 { 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 self.winner == Some(!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
@ -100,6 +100,14 @@ impl FutureMoves {
let arena_len = self.arena.len(); let arena_len = self.arena.len();
next_nodes = next_nodes next_nodes = next_nodes
.into_iter() .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| { .flat_map(|node_idx| {
self.generate_children( self.generate_children(
Some(node_idx), Some(node_idx),
@ -107,9 +115,6 @@ impl FutureMoves {
next_color, next_color,
) )
}) })
.progress_with(ProgressBar::new(arena_len as u64).with_style(
ProgressStyle::with_template("Children: ({pos}/{len}) {per_sec}").unwrap(),
))
.collect(); .collect();
next_color = !next_color; next_color = !next_color;
} }
@ -122,7 +127,7 @@ impl FutureMoves {
color: Piece, color: Piece,
) -> impl Iterator<Item = usize> { ) -> impl Iterator<Item = usize> {
let start_idx = self.arena.len(); let start_idx = self.arena.len();
self.arena.extend( let mut new: Vec<Move> =
// use [`Board::all_positions`] here instead of [`Board::possible_moves`] // 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`] // because we use [`Board::what_if`] later and we want to reduce calls to [`Board::propegate_from_dry`]
Board::all_positions() Board::all_positions()
@ -134,9 +139,18 @@ impl FutureMoves {
winner: new_board.game_winner(color), winner: new_board.game_winner(color),
parent, parent,
children: Vec::new(), 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(); let new_indices = start_idx..self.arena.len();
@ -277,7 +291,7 @@ pub struct ComplexAgent {
impl ComplexAgent { impl ComplexAgent {
pub const fn new(color: Piece) -> Self { pub const fn new(color: Piece) -> Self {
const MAX_DEPTH: usize = 8; const MAX_DEPTH: usize = 17;
const MAX_ARENA: usize = 100_000_000; const MAX_ARENA: usize = 100_000_000;
Self { Self {
color, color,