Compare commits
8 Commits
e966a3ac9a
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
4ca6dfc572
|
|||
|
ef2e551085
|
|||
|
b5e73cbbcf
|
|||
|
aeca76fda6
|
|||
|
111ce718da
|
|||
|
9342760d82
|
|||
|
967f1f6efb
|
|||
|
94831b720b
|
44
src/elo.rs
44
src/elo.rs
@@ -9,7 +9,7 @@ use indicatif::{ProgressBar, ProgressStyle};
|
||||
use rand::seq::SliceRandom;
|
||||
use rayon::iter::{IntoParallelIterator, ParallelIterator};
|
||||
use skillratings::{
|
||||
glicko2::{glicko2, Glicko2Rating},
|
||||
glicko2::{confidence_interval, glicko2, Glicko2Rating},
|
||||
Outcomes, Rating,
|
||||
};
|
||||
use std::num::NonZero;
|
||||
@@ -18,25 +18,19 @@ type AgentMaker = Box<dyn Fn(Piece) -> Box<dyn Agent>>;
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn run() {
|
||||
let total_memory = 30_000_000_000;
|
||||
let total_memory = 30_000_000_000; // 30 GB
|
||||
let num_threads = std::thread::available_parallelism()
|
||||
.map(NonZero::get)
|
||||
.expect("unable to get number of threads");
|
||||
let mem_per_thread = total_memory / num_threads;
|
||||
|
||||
let fmv_base = FutureMoveConfig {
|
||||
max_depth: 10,
|
||||
min_arena_depth: 14,
|
||||
top_k_children: 2,
|
||||
up_to_minus: 10,
|
||||
// max_arena_size: usize::MAX,
|
||||
max_arena_size: mem_per_thread / FutureMoves::ARENA_ENTRY_SIZE,
|
||||
do_prune: false,
|
||||
print: false,
|
||||
children_eval_method: Default::default(),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let configs = [4, 5, 6]
|
||||
let configs = [2, 3, 4, 5, 6, 7, 8]
|
||||
.into_iter()
|
||||
.map(move |d| FutureMoveConfig {
|
||||
max_depth: d,
|
||||
@@ -127,7 +121,7 @@ pub fn run() {
|
||||
})
|
||||
.collect();
|
||||
|
||||
if true {
|
||||
if false {
|
||||
vec.push((
|
||||
"RandomAgent".to_string(),
|
||||
Box::new(move |piece| Box::new(RandomAgent::new(piece))),
|
||||
@@ -149,13 +143,22 @@ pub struct PlayerArena {
|
||||
impl std::fmt::Display for PlayerArena {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let mut players_i: Vec<usize> = (0..self.players.len()).collect();
|
||||
players_i.sort_by_key(|&i| -(self.players[i].2.rating() * 100.0) as i64);
|
||||
|
||||
players_i.sort_by(|&a, &b| {
|
||||
self.players[b]
|
||||
.2
|
||||
.rating()
|
||||
.total_cmp(&self.players[a].2.rating())
|
||||
});
|
||||
|
||||
for i in players_i {
|
||||
let conf_interval = confidence_interval(&self.players[i].2);
|
||||
|
||||
writeln!(
|
||||
f,
|
||||
"({:.2}): {}",
|
||||
"({:.2}[+/-{:.2}]): {}",
|
||||
self.players[i].2.rating(),
|
||||
conf_interval.1 - self.players[i].2.rating(),
|
||||
self.players[i].0
|
||||
)?;
|
||||
}
|
||||
@@ -169,9 +172,8 @@ impl PlayerArena {
|
||||
Self {
|
||||
players: players
|
||||
.into_iter()
|
||||
.zip([Default::default()].into_iter().cycle())
|
||||
// flatten tuple
|
||||
.map(|((a, b), c)| (a, b, c))
|
||||
// All starting ratings should be the default
|
||||
.map(|(a, b)| (a, b, Default::default()))
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
@@ -236,7 +238,7 @@ impl PlayerArena {
|
||||
self.process_outcome(i, j, &o);
|
||||
|
||||
if received_num > 0 {
|
||||
term.clear_last_lines(self.players.len())
|
||||
term.clear_last_lines(self.players.len() + 1)
|
||||
.expect("unable to clear prev lines");
|
||||
}
|
||||
term.write_str(format!("{}", self).as_str())
|
||||
@@ -244,8 +246,12 @@ impl PlayerArena {
|
||||
|
||||
received_num += 1;
|
||||
p.inc(1);
|
||||
// add extra newline after progressbar
|
||||
println!();
|
||||
|
||||
// break if all pairs were recieved
|
||||
if received_num == num {
|
||||
drop(receiver);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -270,14 +276,12 @@ impl PlayerArena {
|
||||
}
|
||||
|
||||
fn process_outcome(&mut self, player1: usize, player2: usize, outcome: &Outcomes) {
|
||||
let (np1, np2) = glicko2(
|
||||
(self.players[player1].2, self.players[player2].2) = glicko2(
|
||||
&self.players[player1].2,
|
||||
&self.players[player2].2,
|
||||
outcome,
|
||||
&Default::default(),
|
||||
);
|
||||
self.players[player1].2 = np1;
|
||||
self.players[player2].2 = np2;
|
||||
}
|
||||
|
||||
fn play_two_inner(player_1: Box<dyn Agent>, player_2: Box<dyn Agent>) -> Outcomes {
|
||||
|
||||
@@ -34,7 +34,7 @@ pub struct FutureMoves {
|
||||
board: Board,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Allocative)]
|
||||
#[derive(Copy, Clone, Allocative, Default)]
|
||||
pub struct FutureMoveConfig {
|
||||
/// Max depth of that we should try and traverse
|
||||
pub max_depth: usize,
|
||||
@@ -213,18 +213,7 @@ impl FutureMoves {
|
||||
}
|
||||
|
||||
fn create_move(&self, coord: MoveCoord, board: Board, color: Piece) -> Move {
|
||||
Move::new(
|
||||
coord,
|
||||
board,
|
||||
color,
|
||||
self.agent_color,
|
||||
MoveValueConfig {
|
||||
self_value_raw: matches!(
|
||||
self.config.children_eval_method,
|
||||
ChildrenEvalMethod::MinMaxProb
|
||||
),
|
||||
},
|
||||
)
|
||||
Move::new(coord, board, color, self.agent_color, MoveValueConfig {})
|
||||
}
|
||||
|
||||
fn generate_children_raw(&self, parent_idx: usize) -> Vec<Move> {
|
||||
@@ -307,23 +296,6 @@ impl FutureMoves {
|
||||
.iter()
|
||||
.map(|&child| self.arena[child].value)
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
match self.config.children_eval_method {
|
||||
ChildrenEvalMethod::MinMax => {
|
||||
let child_value = 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().map(|x| x.value).min()
|
||||
} else {
|
||||
children_values.into_iter().map(|x| x.value).max()
|
||||
}
|
||||
.unwrap_or(0);
|
||||
|
||||
self.arena[idx].value.value =
|
||||
self.arena[idx].self_value.value + child_value;
|
||||
}
|
||||
ChildrenEvalMethod::MinMaxProb => {
|
||||
let child_value = if self.arena[idx].color == self.agent_color {
|
||||
// get best (for the adversary) enemy play
|
||||
// this assumes the adversary is playing optimally
|
||||
@@ -334,11 +306,18 @@ impl FutureMoves {
|
||||
}
|
||||
.cloned()
|
||||
.unwrap_or(Default::default());
|
||||
|
||||
self.arena[idx].value = self.arena[idx].self_value;
|
||||
|
||||
match self.config.children_eval_method {
|
||||
ChildrenEvalMethod::MinMax => {
|
||||
self.arena[idx].value.value += child_value.value;
|
||||
self.arena[idx].value.set_state(child_value.state());
|
||||
}
|
||||
ChildrenEvalMethod::MinMaxProb => {
|
||||
self.arena[idx]
|
||||
.value
|
||||
.populate_self_from_children(&children_values);
|
||||
|
||||
self.arena[idx].value.value += child_value.value;
|
||||
}
|
||||
}
|
||||
@@ -389,7 +368,7 @@ impl FutureMoves {
|
||||
ChildrenEvalMethod::MinMax => self.arena[x]
|
||||
.children
|
||||
.iter()
|
||||
.max_by_key(|&&idx| self.arena[idx].value.value),
|
||||
.max_by_key(|&&idx| self.arena[idx].value),
|
||||
ChildrenEvalMethod::MinMaxProb => self.arena[x]
|
||||
.children
|
||||
.iter()
|
||||
|
||||
@@ -38,9 +38,7 @@ pub struct Move {
|
||||
pub is_trimmed: bool,
|
||||
}
|
||||
|
||||
pub struct MoveValueConfig {
|
||||
pub self_value_raw: bool,
|
||||
}
|
||||
pub struct MoveValueConfig {}
|
||||
|
||||
impl Move {
|
||||
pub fn new(
|
||||
@@ -48,7 +46,7 @@ impl Move {
|
||||
board: Board,
|
||||
color: Piece,
|
||||
agent_color: Piece,
|
||||
mvc: MoveValueConfig,
|
||||
_: MoveValueConfig,
|
||||
) -> Self {
|
||||
let mut m = Move {
|
||||
coord,
|
||||
@@ -76,30 +74,11 @@ impl Move {
|
||||
Winner::None => {}
|
||||
}
|
||||
|
||||
if mvc.self_value_raw {
|
||||
m.self_value.value =
|
||||
const { BoardValueMap::weighted() }.board_value(&board, agent_color) as i32;
|
||||
} else {
|
||||
m.self_value.value = m.compute_self_value(agent_color, &board, mvc) as i32;
|
||||
}
|
||||
m
|
||||
}
|
||||
|
||||
fn compute_self_value(&self, agent_color: Piece, board: &Board, _mvc: MoveValueConfig) -> i16 {
|
||||
if self.winner == Winner::Player(!agent_color) {
|
||||
// if this board results in the opponent winning, MAJORLY negatively weigh this move
|
||||
// NOTE! this branch isn't completely deleted because if so, the bot wouldn't make a move.
|
||||
// We shouldn't prune branches because we still need to always react to the opponent's moves
|
||||
return i16::MIN + 1;
|
||||
} else if self.winner == Winner::Player(agent_color) {
|
||||
// results in a win for the agent
|
||||
return i16::MAX - 1;
|
||||
}
|
||||
|
||||
// I guess ignore Ties here, don't give them an explicit value,
|
||||
const { BoardValueMap::weighted() }.board_value(board, agent_color)
|
||||
}
|
||||
|
||||
/// Sort children of the [`Move`] by their self_value in `arena`
|
||||
pub fn sort_children(&mut self, arena: &[Move]) {
|
||||
self.children.sort_by(|&a, &b| {
|
||||
|
||||
@@ -56,19 +56,19 @@ impl MoveValueStats {
|
||||
self.state = state;
|
||||
}
|
||||
|
||||
pub const fn state(&self) -> Option<MVSGameState> {
|
||||
self.state
|
||||
}
|
||||
|
||||
pub fn populate_self_from_children(&mut self, others: &[Self]) {
|
||||
self.wins = others
|
||||
.iter()
|
||||
.map(|x| x.wins + (x.state == Some(MVSGameState::Win)) as u16)
|
||||
.sum::<u16>();
|
||||
self.losses = others
|
||||
.iter()
|
||||
.map(|x| x.losses + (x.state == Some(MVSGameState::Loss)) as u16)
|
||||
.sum::<u16>();
|
||||
self.ties = others
|
||||
.iter()
|
||||
.map(|x| x.ties + (x.state == Some(MVSGameState::Tie)) as u16)
|
||||
.sum::<u16>();
|
||||
(self.wins, self.losses, self.ties) =
|
||||
others.iter().fold((0, 0, 0), |(wins, losses, ties), x| {
|
||||
(
|
||||
wins + x.wins + (x.state == Some(MVSGameState::Win)) as u16,
|
||||
losses + x.losses + (x.state == Some(MVSGameState::Loss)) as u16,
|
||||
ties + x.ties + (x.state == Some(MVSGameState::Tie)) as u16,
|
||||
)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,7 +85,7 @@ impl Ord for MoveValueStats {
|
||||
}
|
||||
|
||||
let (s_cw, o_cw) = (self.chance_win(), other.chance_win());
|
||||
if s_cw.is_some() | o_cw.is_some() {
|
||||
if s_cw.is_some() || o_cw.is_some() {
|
||||
if s_cw > o_cw {
|
||||
return Ordering::Greater;
|
||||
} else if o_cw > s_cw {
|
||||
|
||||
@@ -14,12 +14,12 @@ impl<T: Copy> PosMap<T> {
|
||||
Self(MaybeUninit::zeroed().assume_init())
|
||||
}
|
||||
|
||||
pub const fn from(v: [[T; Board::SIZE as usize]; Board::SIZE as usize]) -> Self {
|
||||
pub const fn from(mut v: [[T; Board::SIZE as usize]; Board::SIZE as usize]) -> Self {
|
||||
let mut n = unsafe { Self::uninit() };
|
||||
|
||||
const_for!(i in 0..Board::SIZE => {
|
||||
const_for!(j in 0..Board::SIZE => {
|
||||
n.set(CoordPair::from_axes(i, j), v[i as usize][j as usize]);
|
||||
std::mem::swap(n.get_mut(CoordPair::from_axes(i, j)), &mut v[i as usize][j as usize]);
|
||||
});
|
||||
});
|
||||
n
|
||||
|
||||
Reference in New Issue
Block a user