probabilistic progress

This commit is contained in:
Simon Gardling 2025-04-28 01:12:15 -04:00
parent 2586b43294
commit c3c07fcb28
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D
4 changed files with 58 additions and 66 deletions

View File

@ -29,7 +29,7 @@ pub fn run() {
children_eval_method: Default::default(), children_eval_method: Default::default(),
}; };
let configs = [4, 5, 6] let configs = [6]
.into_iter() .into_iter()
.map(move |d| FutureMoveConfig { .map(move |d| FutureMoveConfig {
max_depth: d, max_depth: d,
@ -119,14 +119,17 @@ pub fn run() {
) )
}) })
.collect(); .collect();
vec.push((
"RandomAgent".to_string(), if false {
Box::new(move |piece| Box::new(RandomAgent::new(piece))), vec.push((
)); "RandomAgent".to_string(),
Box::new(move |piece| Box::new(RandomAgent::new(piece))),
));
}
let mut arena = PlayerArena::new(vec); let mut arena = PlayerArena::new(vec);
arena.prop_arena(1000); arena.prop_arena(100);
println!("{}", arena); println!("{}", arena);
} }
@ -172,7 +175,10 @@ impl PlayerArena {
.map(|&(i, j)| { .map(|&(i, j)| {
( (
(i, j), (i, j),
Self::create_agents(&self.players[i].1, &self.players[j].1), (
(&self.players[i].1)(Piece::Black),
(&self.players[j].1)(Piece::White),
),
) )
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
@ -221,14 +227,16 @@ impl PlayerArena {
while let Ok((i, j, o)) = receiver.recv() { while let Ok((i, j, o)) = receiver.recv() {
self.process_outcome(i, j, &o); self.process_outcome(i, j, &o);
received_num += 1;
p.inc(1);
term.clear_last_lines(self.players.len()) if received_num > 0 {
.expect("unable to clear prev lines"); term.clear_last_lines(self.players.len())
.expect("unable to clear prev lines");
}
term.write_str(format!("{}", self).as_str()) term.write_str(format!("{}", self).as_str())
.expect("unable to write leaderboard"); .expect("unable to write leaderboard");
received_num += 1;
p.inc(1);
// break if all pairs were recieved // break if all pairs were recieved
if received_num == num { if received_num == num {
break; break;
@ -265,19 +273,13 @@ impl PlayerArena {
self.players[player2].2 = np2; self.players[player2].2 = np2;
} }
fn create_agents(
player_1_fn: &AgentMaker,
player_2_fn: &AgentMaker,
) -> (Box<dyn Agent>, Box<dyn Agent>) {
(player_1_fn(Piece::Black), player_2_fn(Piece::White))
}
fn play_two_inner(player_1: Box<dyn Agent>, player_2: Box<dyn Agent>) -> Outcomes { fn play_two_inner(player_1: Box<dyn Agent>, player_2: Box<dyn Agent>) -> Outcomes {
let result = GameInner::new( let result = GameInner::new(
player_1, player_1,
player_2, player_2,
false, false,
Board::random(rand::random_range(1..=15)), // Board::random(rand::random_range(20..=30)),
Board::STARTING_POSITION,
) )
.expect("unable to create game") .expect("unable to create game")
.loop_until_result(); .loop_until_result();

View File

@ -315,23 +315,14 @@ impl FutureMoves {
// get best (for the adversary) enemy play // get best (for the adversary) enemy play
// this assumes the adversary is playing optimally // this assumes the adversary is playing optimally
children_values children_values.into_iter().map(|x| x.value).min()
.into_iter()
.min_by_key(|x| x.value)
.map(|x| x.value)
} else { } else {
children_values children_values.into_iter().map(|x| x.value).max()
.into_iter()
.max_by_key(|x| x.value)
.map(|x| x.value)
} }
.unwrap_or(0); .unwrap_or(0);
// we use `depth` and divided `self_value` by it, idk if this is worth it
// we should really setup some sort of ELO rating for each commit, playing them against
// each other or something, could be cool to benchmark these more subjective things, not
// just performance (cycles/time wise)
self.arena[idx].value.value = self.arena[idx].value.value =
self.arena[idx].self_value as i32 + child_value; self.arena[idx].self_value.value as i32 + child_value;
} }
ChildrenEvalMethod::MinMaxProb => { ChildrenEvalMethod::MinMaxProb => {
let child_value = if self.arena[idx].color == self.agent_color { let child_value = if self.arena[idx].color == self.agent_color {
@ -344,16 +335,12 @@ impl FutureMoves {
} }
.cloned() .cloned()
.unwrap_or(Default::default()); .unwrap_or(Default::default());
// we use `depth` and divided `self_value` by it, idk if this is worth it
// we should really setup some sort of ELO rating for each commit, playing them against self.arena[idx].value = self.arena[idx].self_value;
// each other or something, could be cool to benchmark these more subjective things, not
// just performance (cycles/time wise)
self.arena[idx] self.arena[idx]
.value .value
.populate_self_from_children(&children_values); .populate_self_from_children(&children_values);
self.arena[idx].value.value += child_value.value;
self.arena[idx].value.value =
self.arena[idx].self_value as i32 + child_value.value;
} }
} }
} }

View File

@ -29,7 +29,7 @@ pub struct Move {
pub value: MoveValueStats, pub value: MoveValueStats,
/// What is the inherit value of this move (not including children) /// What is the inherit value of this move (not including children)
pub self_value: i16, pub self_value: MoveValueStats,
/// Which color made a move on this move? /// Which color made a move on this move?
pub color: Piece, pub color: Piece,
@ -58,28 +58,29 @@ impl Move {
value: Default::default(), value: Default::default(),
color, color,
is_trimmed: false, is_trimmed: false,
self_value: 0, self_value: Default::default(),
}; };
// set wins/losses values appropriately // set wins/losses values appropriately
match m.winner { match m.winner {
Winner::Player(piece) => { Winner::Player(piece) => {
if piece == agent_color { if piece == agent_color {
m.value.set_state(Some(MVSGameState::Win)); m.self_value.set_state(Some(MVSGameState::Win));
} else { } else {
m.value.set_state(Some(MVSGameState::Loss)); m.self_value.set_state(Some(MVSGameState::Loss));
} }
} }
Winner::Tie => { Winner::Tie => {
m.value.set_state(Some(MVSGameState::Tie)); m.self_value.set_state(Some(MVSGameState::Tie));
} }
Winner::None => {} Winner::None => {}
} }
if mvc.self_value_raw { if mvc.self_value_raw {
m.self_value = const { BoardValueMap::weighted() }.board_value(&board, agent_color); m.self_value.value =
const { BoardValueMap::weighted() }.board_value(&board, agent_color) as i32;
} else { } else {
m.self_value = m.compute_self_value(agent_color, &board, mvc); m.self_value.value = m.compute_self_value(agent_color, &board, mvc) as i32;
} }
m m
} }

View File

@ -8,48 +8,44 @@ pub enum MVSGameState {
Loss = -1, Loss = -1,
} }
#[derive(Clone, Copy, Debug, Allocative, PartialEq, Eq, Default)] #[derive(Clone, Copy, Debug, Allocative, PartialEq, Eq, Default, PartialOrd)]
pub struct MoveValueStats { pub struct MoveValueStats {
state: Option<MVSGameState>, state: Option<MVSGameState>,
wins: u16, wins: u16,
losses: u16, losses: u16,
ties: u16,
pub value: i32, pub value: i32,
} }
impl MoveValueStats { impl MoveValueStats {
#[cfg(test)] #[cfg(test)]
pub const fn new_from_wins_losses(wins: u16, losses: u16) -> Self { pub fn new_from_wins_losses(wins: u16, losses: u16) -> Self {
Self { Self {
state: None,
wins, wins,
losses, losses,
value: 0, ..Default::default()
} }
} }
#[cfg(test)] #[cfg(test)]
pub const fn new_from_value(value: i32) -> Self { pub fn new_from_value(value: i32) -> Self {
Self { Self {
state: None,
wins: 0,
losses: 0,
value, value,
..Default::default()
} }
} }
#[cfg(test)] #[cfg(test)]
pub const fn new_from_state(state: Option<MVSGameState>) -> Self { pub fn new_from_state(state: Option<MVSGameState>) -> Self {
Self { Self {
state, state,
wins: 0, ..Default::default()
losses: 0,
value: 0,
} }
} }
fn chance_win(&self) -> Option<f32> { fn chance_win(&self) -> Option<f32> {
let sum = self.losses + self.wins; let sum = self.losses + self.wins + self.ties;
if sum == 0 { if 20 > sum {
return None; return None;
} }
Some(self.wins as f32 / sum as f32) Some(self.wins as f32 / sum as f32)
@ -70,12 +66,11 @@ impl MoveValueStats {
.iter() .iter()
.filter(|x| x.state == Some(MVSGameState::Loss)) .filter(|x| x.state == Some(MVSGameState::Loss))
.count() as u16; .count() as u16;
} self.ties = others.iter().map(|x| x.ties).sum::<u16>()
} + others
.iter()
impl PartialOrd for MoveValueStats { .filter(|x| x.state == Some(MVSGameState::Tie))
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { .count() as u16;
Some(self.cmp(other))
} }
} }
@ -122,4 +117,11 @@ mod tests {
let two = MoveValueStats::new_from_state(Some(MVSGameState::Win)); let two = MoveValueStats::new_from_state(Some(MVSGameState::Win));
assert!(one < two); assert!(one < two);
} }
#[test]
fn two_prob_zero() {
let one = MoveValueStats::new_from_wins_losses(10, 0);
let two = MoveValueStats::new_from_wins_losses(0, 6);
assert!(one > two);
}
} }