This commit is contained in:
Simon Gardling 2025-04-24 11:10:11 -04:00
parent 23e7ae2822
commit e8d05e0f9d
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D
4 changed files with 87 additions and 84 deletions

View File

@ -1,5 +1,5 @@
use crate::{ use crate::{
agent::Agent, agent::{Agent, RandomAgent},
complexagent::ComplexAgent, complexagent::ComplexAgent,
game_inner::GameInner, game_inner::GameInner,
logic::{ChildrenEvalMethod, FutureMoveConfig}, logic::{ChildrenEvalMethod, FutureMoveConfig},
@ -110,7 +110,7 @@ pub fn run() {
.collect() .collect()
}); });
let vec: Vec<(String, AgentMaker)> = configs let mut vec: Vec<(String, AgentMaker)> = configs
.into_iter() .into_iter()
.map(move |config| -> (String, AgentMaker) { .map(move |config| -> (String, AgentMaker) {
( (
@ -119,10 +119,10 @@ pub fn run() {
) )
}) })
.collect(); .collect();
// vec.push(( vec.push((
// "RandomAgent".to_string(), "RandomAgent".to_string(),
// Box::new(move |piece| Box::new(RandomAgent::new(piece))), Box::new(move |piece| Box::new(RandomAgent::new(piece))),
// )); ));
let mut arena = PlayerArena::new(vec); let mut arena = PlayerArena::new(vec);

View File

@ -1,5 +1,6 @@
mod board_value; mod board_value;
mod future_moves; mod future_moves;
mod r#move; mod r#move;
mod mvs;
pub use future_moves::{ChildrenEvalMethod, FutureMoveConfig, FutureMoves}; pub use future_moves::{ChildrenEvalMethod, FutureMoveConfig, FutureMoves};
pub use r#move::MoveCoord; pub use r#move::MoveCoord;

View File

@ -1,78 +1,12 @@
use std::cmp::Ordering; use super::{
board_value::BoardValueMap,
use super::board_value::BoardValueMap; mvs::{MVSGameState, MoveValueStats},
};
use crate::repr::{Board, CoordPair, Piece, Winner}; use crate::repr::{Board, CoordPair, Piece, Winner};
use allocative::Allocative; use allocative::Allocative;
pub type MoveCoord = Option<CoordPair>; pub type MoveCoord = Option<CoordPair>;
#[derive(Clone, Copy, PartialEq, Eq, Allocative, Debug, PartialOrd, Ord)]
pub enum MVSGameState {
Win = 1,
Loss = 0,
Tie = -1,
}
#[derive(Clone, Copy, Debug, Allocative, PartialEq, Eq, Default)]
pub struct MoveValueStats {
state: Option<MVSGameState>,
wins: u16,
losses: u16,
pub value: i32,
}
impl MoveValueStats {
fn chance_win(&self) -> Option<f32> {
let sum = self.losses + self.wins;
if sum == 0 {
return None;
}
Some(self.wins as f32 / sum as f32)
}
pub fn populate_self_from_children(&mut self, others: &[Self]) {
let wins = others.iter().map(|x| x.wins).sum::<u16>()
+ others
.iter()
.filter(|x| x.state == Some(MVSGameState::Win))
.count() as u16;
let losses = others.iter().map(|x| x.losses).sum::<u16>()
+ others
.iter()
.filter(|x| x.state == Some(MVSGameState::Loss))
.count() as u16;
self.wins = wins;
self.losses = losses;
}
}
impl PartialOrd for MoveValueStats {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for MoveValueStats {
fn cmp(&self, other: &Self) -> Ordering {
if self.state.is_some() && other.state.is_some() {
return self.state.cmp(&other.state);
}
let s_cw = self.chance_win();
let o_cw = other.chance_win();
if s_cw.is_some() && o_cw.is_some() {
if s_cw > o_cw {
return Ordering::Greater;
} else if o_cw > s_cw {
return Ordering::Less;
}
}
self.value.cmp(&other.value)
}
}
#[derive(Clone, Debug, Allocative)] #[derive(Clone, Debug, Allocative)]
pub struct Move { pub struct Move {
/// Coordinates (i, j) of the move (if it exists) /// Coordinates (i, j) of the move (if it exists)
@ -131,23 +65,21 @@ impl Move {
match m.winner { match m.winner {
Winner::Player(piece) => { Winner::Player(piece) => {
if piece == agent_color { if piece == agent_color {
m.value.wins += 1; m.value.set_state(Some(MVSGameState::Win));
m.value.state = Some(MVSGameState::Win);
} else { } else {
m.value.losses += 1; m.value.set_state(Some(MVSGameState::Loss));
m.value.state = Some(MVSGameState::Loss);
} }
} }
Winner::Tie => { Winner::Tie => {
m.value.state = Some(MVSGameState::Tie); m.value.set_state(Some(MVSGameState::Tie));
} }
Winner::None => {} Winner::None => {}
} }
if !mvc.self_value_raw { if mvc.self_value_raw {
m.self_value = m.compute_self_value(agent_color, &board, mvc);
} else {
m.self_value = const { BoardValueMap::weighted() }.board_value(&board, agent_color); m.self_value = const { BoardValueMap::weighted() }.board_value(&board, agent_color);
} else {
m.self_value = m.compute_self_value(agent_color, &board, mvc);
} }
m m
} }

70
src/logic/mvs.rs Normal file
View File

@ -0,0 +1,70 @@
use allocative::Allocative;
use std::cmp::Ordering;
#[derive(Clone, Copy, PartialEq, Eq, Allocative, Debug, PartialOrd, Ord)]
pub enum MVSGameState {
Win = 1,
Tie = 0,
Loss = -1,
}
#[derive(Clone, Copy, Debug, Allocative, PartialEq, Eq, Default)]
pub struct MoveValueStats {
state: Option<MVSGameState>,
wins: u16,
losses: u16,
pub value: i32,
}
impl MoveValueStats {
fn chance_win(&self) -> Option<f32> {
let sum = self.losses + self.wins;
if sum == 0 {
return None;
}
Some(self.wins as f32 / sum as f32)
}
pub const fn set_state(&mut self, state: Option<MVSGameState>) {
self.state = state;
}
pub fn populate_self_from_children(&mut self, others: &[Self]) {
self.wins = others.iter().map(|x| x.wins).sum::<u16>()
+ others
.iter()
.filter(|x| x.state == Some(MVSGameState::Win))
.count() as u16;
self.losses = others.iter().map(|x| x.losses).sum::<u16>()
+ others
.iter()
.filter(|x| x.state == Some(MVSGameState::Loss))
.count() as u16;
}
}
impl PartialOrd for MoveValueStats {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for MoveValueStats {
fn cmp(&self, other: &Self) -> Ordering {
if self.state.is_some() || other.state.is_some() {
return self.state.cmp(&other.state);
}
if let Some(s_cw) = self.chance_win() {
if let Some(o_cw) = other.chance_win() {
if s_cw > o_cw {
return Ordering::Greater;
} else if o_cw > s_cw {
return Ordering::Less;
}
}
}
self.value.cmp(&other.value)
}
}