Improved diffuse agent

master
Mark 2024-03-05 10:18:19 -08:00
parent 3e080ad89d
commit fb69990b9d
Signed by: Mark
GPG Key ID: C6D63995FE72FD80
1 changed files with 22 additions and 7 deletions

View File

@ -1,6 +1,6 @@
use anyhow::Result; use anyhow::Result;
use super::{MinimizerAgent, RandomAgent}; use super::{MaximizerAgent, MinimizerAgent, SimpleMinimax};
use crate::{ use crate::{
board::{Board, PlayerAction}, board::{Board, PlayerAction},
util::Symb, util::Symb,
@ -9,9 +9,9 @@ use crate::{
/// A simple "operator diffusion" MINIMIZER agent. /// A simple "operator diffusion" MINIMIZER agent.
/// ///
/// Tries to keep operators as far apart as possible, denying large numbers. /// Tries to keep operators as far apart as possible, denying large numbers.
pub struct DiffuseAgent {} pub struct Diffuse {}
impl DiffuseAgent { impl Diffuse {
/// Place a symbol on the board. /// Place a symbol on the board.
/// Assumes `symb` is not already on the board /// Assumes `symb` is not already on the board
fn step_symb(&self, board: &Board, symb: Symb) -> PlayerAction { fn step_symb(&self, board: &Board, symb: Symb) -> PlayerAction {
@ -25,8 +25,8 @@ impl DiffuseAgent {
// Set up initial distances // Set up initial distances
dist[0] = 1; dist[0] = 1;
*dist.last_mut().unwrap() = 1; *dist.last_mut().unwrap() = 1;
for (i, o) in board.iter().enumerate() { for (i, o) in board.get_board().iter().enumerate() {
if let Some((s, _)) = o { if let Some(s) = o {
if s.is_op() { if s.is_op() {
dist[i] = 0 dist[i] = 0
} }
@ -68,7 +68,7 @@ impl DiffuseAgent {
} }
} }
impl MinimizerAgent for DiffuseAgent { impl MinimizerAgent for Diffuse {
fn step_min(&mut self, board: &Board) -> Result<PlayerAction> { fn step_min(&mut self, board: &Board) -> Result<PlayerAction> {
let symb = [Symb::Minus, Symb::Times, Symb::Plus, Symb::Div] let symb = [Symb::Minus, Symb::Times, Symb::Plus, Symb::Div]
.iter() .iter()
@ -78,7 +78,22 @@ impl MinimizerAgent for DiffuseAgent {
Ok(self.step_symb(board, *symb)) Ok(self.step_symb(board, *symb))
} else { } else {
// No symbols available, play a random number // No symbols available, play a random number
RandomAgent {}.step_min(board) SimpleMinimax {}.step_min(board)
}
}
}
impl MaximizerAgent for Diffuse {
fn step_max(&mut self, board: &Board) -> Result<PlayerAction> {
let symb = [Symb::Minus, Symb::Times, Symb::Plus, Symb::Div]
.iter()
.find(|x| !board.contains(**x));
if let Some(symb) = symb {
Ok(self.step_symb(board, *symb))
} else {
// No symbols available, play a random number
SimpleMinimax {}.step_max(board)
} }
} }
} }