MinMax is much better
This commit is contained in:
parent
a50ca2c1b1
commit
cb63b49f7a
@ -1,6 +1,6 @@
|
|||||||
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
|
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
|
||||||
use othello::{
|
use othello::{
|
||||||
logic::{ChildrenEvalMethod, FutureMoveConfig, FutureMoves},
|
logic::{FutureMoveConfig, FutureMoves},
|
||||||
repr::{Board, Piece},
|
repr::{Board, Piece},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -13,7 +13,7 @@ fn extend_layers_no_pruning(depth: usize) -> usize {
|
|||||||
max_arena_size: usize::MAX,
|
max_arena_size: usize::MAX,
|
||||||
do_prune: false,
|
do_prune: false,
|
||||||
print: false,
|
print: false,
|
||||||
children_eval_method: ChildrenEvalMethod::AverageDivDepth,
|
children_eval_method: Default::default(),
|
||||||
};
|
};
|
||||||
let mut fut = FutureMoves::new(Piece::Black, config);
|
let mut fut = FutureMoves::new(Piece::Black, config);
|
||||||
fut.update_from_board(&Board::STARTING_POSITION);
|
fut.update_from_board(&Board::STARTING_POSITION);
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
logic::{ChildrenEvalMethod, FutureMoveConfig, FutureMoves},
|
logic::{FutureMoveConfig, FutureMoves},
|
||||||
repr::{Board, Piece, Winner},
|
repr::{Board, Piece, Winner},
|
||||||
};
|
};
|
||||||
use allocative::FlameGraphBuilder;
|
use allocative::FlameGraphBuilder;
|
||||||
@ -18,7 +18,7 @@ pub fn run() {
|
|||||||
max_arena_size: 100_000_000,
|
max_arena_size: 100_000_000,
|
||||||
do_prune: true,
|
do_prune: true,
|
||||||
print: true,
|
print: true,
|
||||||
children_eval_method: ChildrenEvalMethod::AverageDivDepth,
|
children_eval_method: Default::default(),
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
59
src/elo.rs
59
src/elo.rs
@ -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},
|
||||||
@ -18,7 +18,7 @@ type AgentMaker = Box<dyn Fn(Piece) -> Box<dyn Agent>>;
|
|||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn run() {
|
pub fn run() {
|
||||||
const FMV_BASE: FutureMoveConfig = FutureMoveConfig {
|
let fmv_base = FutureMoveConfig {
|
||||||
max_depth: 20,
|
max_depth: 20,
|
||||||
min_arena_depth: 14,
|
min_arena_depth: 14,
|
||||||
top_k_children: 2,
|
top_k_children: 2,
|
||||||
@ -26,26 +26,26 @@ pub fn run() {
|
|||||||
max_arena_size: usize::MAX,
|
max_arena_size: usize::MAX,
|
||||||
do_prune: false,
|
do_prune: false,
|
||||||
print: false,
|
print: false,
|
||||||
children_eval_method: ChildrenEvalMethod::AverageDivDepth,
|
children_eval_method: Default::default(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let configs = [6]
|
let configs = [6]
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(move |d| FutureMoveConfig {
|
.map(move |d| FutureMoveConfig {
|
||||||
max_depth: d,
|
max_depth: d,
|
||||||
..FMV_BASE
|
..fmv_base
|
||||||
})
|
})
|
||||||
.flat_map(move |prev_c| {
|
.flat_map(move |prev_c| {
|
||||||
// create children which enable, and disable pruning
|
// create children which enable, and disable pruning
|
||||||
[true, false].map(move |do_prune| FutureMoveConfig { do_prune, ..prev_c })
|
[false].map(move |do_prune| FutureMoveConfig { do_prune, ..prev_c })
|
||||||
})
|
|
||||||
.filter(move |move_c| {
|
|
||||||
if move_c.do_prune {
|
|
||||||
move_c.max_depth >= 8
|
|
||||||
} else {
|
|
||||||
move_c.max_depth < 8
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
// .filter(move |move_c| {
|
||||||
|
// if move_c.do_prune {
|
||||||
|
// move_c.max_depth >= 8
|
||||||
|
// } else {
|
||||||
|
// move_c.max_depth < 8
|
||||||
|
// }
|
||||||
|
// })
|
||||||
// .flat_map(move |prev_c| {
|
// .flat_map(move |prev_c| {
|
||||||
// [
|
// [
|
||||||
// ChildrenEvalMethod::Average,
|
// ChildrenEvalMethod::Average,
|
||||||
@ -64,12 +64,23 @@ pub fn run() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// different values of top_k_children
|
// different values of top_k_children
|
||||||
[1, 2, 3]
|
[2].map(move |top_k_children| FutureMoveConfig {
|
||||||
.map(move |top_k_children| FutureMoveConfig {
|
top_k_children,
|
||||||
top_k_children,
|
..prev_c
|
||||||
..prev_c
|
})
|
||||||
})
|
.to_vec()
|
||||||
.to_vec()
|
})
|
||||||
|
.flat_map(move |prev_c| {
|
||||||
|
[
|
||||||
|
ChildrenEvalMethod::Average,
|
||||||
|
ChildrenEvalMethod::AverageDivDepth,
|
||||||
|
ChildrenEvalMethod::MinAvgDivDepth,
|
||||||
|
ChildrenEvalMethod::MinMax,
|
||||||
|
]
|
||||||
|
.map(move |method| FutureMoveConfig {
|
||||||
|
children_eval_method: method,
|
||||||
|
..prev_c
|
||||||
|
})
|
||||||
})
|
})
|
||||||
.flat_map(move |prev_c| {
|
.flat_map(move |prev_c| {
|
||||||
if !prev_c.do_prune {
|
if !prev_c.do_prune {
|
||||||
@ -79,8 +90,7 @@ pub fn run() {
|
|||||||
|
|
||||||
// different values to be subtracted from max_depth
|
// different values to be subtracted from max_depth
|
||||||
// to become min_arena_depth
|
// to become min_arena_depth
|
||||||
[1, 2, 3]
|
[2].into_iter()
|
||||||
.into_iter()
|
|
||||||
.filter(|&x| x <= prev_c.max_depth)
|
.filter(|&x| x <= prev_c.max_depth)
|
||||||
.map(move |ad_offset| FutureMoveConfig {
|
.map(move |ad_offset| FutureMoveConfig {
|
||||||
min_arena_depth: prev_c.max_depth - ad_offset,
|
min_arena_depth: prev_c.max_depth - ad_offset,
|
||||||
@ -95,8 +105,7 @@ pub fn run() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// different values of up_to_minus
|
// different values of up_to_minus
|
||||||
[prev_c.max_depth, 1, 2, 3]
|
[3].into_iter()
|
||||||
.into_iter()
|
|
||||||
.filter(|&x| x <= prev_c.max_depth)
|
.filter(|&x| x <= prev_c.max_depth)
|
||||||
.map(move |up_to_minus| FutureMoveConfig {
|
.map(move |up_to_minus| FutureMoveConfig {
|
||||||
up_to_minus,
|
up_to_minus,
|
||||||
@ -105,7 +114,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) {
|
||||||
(
|
(
|
||||||
@ -114,6 +123,10 @@ pub fn run() {
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
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);
|
||||||
|
|
||||||
|
|||||||
@ -79,10 +79,18 @@ impl std::fmt::Display for FutureMoveConfig {
|
|||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub enum ChildrenEvalMethod {
|
pub enum ChildrenEvalMethod {
|
||||||
Average,
|
Average,
|
||||||
/// AverageDivDepth gives the agent a sense of
|
|
||||||
/// time when it comes to how far away a potential win or gain
|
|
||||||
/// is. This performs much better in the Elo Arena than `Average`
|
|
||||||
AverageDivDepth,
|
AverageDivDepth,
|
||||||
|
|
||||||
|
MinAvgDivDepth,
|
||||||
|
|
||||||
|
/// Best so far?
|
||||||
|
MinMax,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for ChildrenEvalMethod {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::MinMax
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FutureMoves {
|
impl FutureMoves {
|
||||||
@ -287,6 +295,31 @@ impl FutureMoves {
|
|||||||
.sum::<i32>()
|
.sum::<i32>()
|
||||||
.checked_div(self.arena[idx].children.len() as i32)
|
.checked_div(self.arena[idx].children.len() as i32)
|
||||||
.and_then(|x| x.checked_div(depth as i32)),
|
.and_then(|x| x.checked_div(depth as i32)),
|
||||||
|
ChildrenEvalMethod::MinAvgDivDepth => {
|
||||||
|
if self.arena[idx].color == self.agent_color {
|
||||||
|
// get best (for the adversary) enemy play
|
||||||
|
// this assumes the adversary is playing optimally
|
||||||
|
|
||||||
|
children_values.into_iter().min()
|
||||||
|
} else {
|
||||||
|
children_values
|
||||||
|
.into_iter()
|
||||||
|
.sum::<i32>()
|
||||||
|
.checked_div(self.arena[idx].children.len() as i32)
|
||||||
|
.and_then(|x| x.checked_div(depth as i32))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ChildrenEvalMethod::MinMax => {
|
||||||
|
if self.arena[idx].color == self.agent_color {
|
||||||
|
// get best (for the adversary) enemy play
|
||||||
|
// this assumes the adversary is playing optimally
|
||||||
|
|
||||||
|
children_values.into_iter().min()
|
||||||
|
} else {
|
||||||
|
children_values.into_iter().max()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
.unwrap_or(0);
|
.unwrap_or(0);
|
||||||
|
|
||||||
@ -571,22 +604,26 @@ impl FutureMoves {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use std::sync::LazyLock;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
const FUTURE_MOVES_CONFIG: FutureMoveConfig = FutureMoveConfig {
|
static FUTURE_MOVES_CONFIG: LazyLock<FutureMoveConfig> = LazyLock::new(|| {
|
||||||
max_depth: 3, // we want great-grand children for traversing moves
|
FutureMoveConfig {
|
||||||
min_arena_depth: 0,
|
max_depth: 3, // we want great-grand children for traversing moves
|
||||||
top_k_children: 1,
|
min_arena_depth: 0,
|
||||||
up_to_minus: 0,
|
top_k_children: 1,
|
||||||
max_arena_size: 100,
|
up_to_minus: 0,
|
||||||
do_prune: false,
|
max_arena_size: 100,
|
||||||
print: false,
|
do_prune: false,
|
||||||
children_eval_method: ChildrenEvalMethod::AverageDivDepth,
|
print: false,
|
||||||
};
|
children_eval_method: Default::default(),
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn prune_tree_test() {
|
fn prune_tree_test() {
|
||||||
let mut futm = FutureMoves::new(Piece::Black, FUTURE_MOVES_CONFIG);
|
let mut futm = FutureMoves::new(Piece::Black, *FUTURE_MOVES_CONFIG);
|
||||||
|
|
||||||
futm.update_from_board(&Board::new());
|
futm.update_from_board(&Board::new());
|
||||||
|
|
||||||
@ -628,7 +665,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn expand_layer_test() {
|
fn expand_layer_test() {
|
||||||
let mut futm = FutureMoves::new(Piece::Black, FUTURE_MOVES_CONFIG);
|
let mut futm = FutureMoves::new(Piece::Black, *FUTURE_MOVES_CONFIG);
|
||||||
futm.config.max_depth = 1;
|
futm.config.max_depth = 1;
|
||||||
|
|
||||||
futm.update_from_board(&Board::STARTING_POSITION);
|
futm.update_from_board(&Board::STARTING_POSITION);
|
||||||
@ -653,7 +690,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn depth_of_test() {
|
fn depth_of_test() {
|
||||||
let mut futm = FutureMoves::new(Piece::Black, FUTURE_MOVES_CONFIG);
|
let mut futm = FutureMoves::new(Piece::Black, *FUTURE_MOVES_CONFIG);
|
||||||
|
|
||||||
futm.update_from_board(&Board::new());
|
futm.update_from_board(&Board::new());
|
||||||
|
|
||||||
@ -681,7 +718,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn by_depth_test() {
|
fn by_depth_test() {
|
||||||
let mut futm = FutureMoves::new(Piece::Black, FUTURE_MOVES_CONFIG);
|
let mut futm = FutureMoves::new(Piece::Black, *FUTURE_MOVES_CONFIG);
|
||||||
|
|
||||||
futm.update_from_board(&Board::new());
|
futm.update_from_board(&Board::new());
|
||||||
|
|
||||||
@ -707,7 +744,7 @@ mod tests {
|
|||||||
/// tests whether or not FutureMoves can recover from multiple skips and then manually regenerating the arena
|
/// tests whether or not FutureMoves can recover from multiple skips and then manually regenerating the arena
|
||||||
#[test]
|
#[test]
|
||||||
fn skip_move_recovery() {
|
fn skip_move_recovery() {
|
||||||
let mut futm = FutureMoves::new(Piece::Black, FUTURE_MOVES_CONFIG);
|
let mut futm = FutureMoves::new(Piece::Black, *FUTURE_MOVES_CONFIG);
|
||||||
let mut board = Board::STARTING_POSITION;
|
let mut board = Board::STARTING_POSITION;
|
||||||
|
|
||||||
// replay of a test I did
|
// replay of a test I did
|
||||||
@ -770,7 +807,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn derive_board() {
|
fn derive_board() {
|
||||||
let mut futm = FutureMoves::new(Piece::White, FUTURE_MOVES_CONFIG);
|
let mut futm = FutureMoves::new(Piece::White, *FUTURE_MOVES_CONFIG);
|
||||||
|
|
||||||
let mut b = Board::STARTING_POSITION;
|
let mut b = Board::STARTING_POSITION;
|
||||||
futm.update_from_board(&b);
|
futm.update_from_board(&b);
|
||||||
@ -838,7 +875,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut futm = FutureMoves::new(Piece::White, FUTURE_MOVES_CONFIG);
|
let mut futm = FutureMoves::new(Piece::White, *FUTURE_MOVES_CONFIG);
|
||||||
futm.update_from_board(&board);
|
futm.update_from_board(&board);
|
||||||
futm.generate();
|
futm.generate();
|
||||||
|
|
||||||
|
|||||||
@ -14,6 +14,8 @@ pub mod repr;
|
|||||||
// TODO! make this agent configuration a config option via `clap-rs`
|
// TODO! make this agent configuration a config option via `clap-rs`
|
||||||
// or maybe even like a TUI menu?
|
// or maybe even like a TUI menu?
|
||||||
fn main() {
|
fn main() {
|
||||||
|
// elo::run();
|
||||||
|
// return;
|
||||||
let player1 = complexagent::ComplexAgent::new(
|
let player1 = complexagent::ComplexAgent::new(
|
||||||
Piece::Black,
|
Piece::Black,
|
||||||
FutureMoveConfig {
|
FutureMoveConfig {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user