elo: multithreading improvements
This commit is contained in:
parent
c368ffc495
commit
6d920ce5af
@ -4,7 +4,8 @@ use std::io;
|
||||
use std::io::prelude::*;
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub trait Agent {
|
||||
// implements `Send` so we can use it in a multithreaded `EloArena`
|
||||
pub trait Agent: Send {
|
||||
/// Returns the move of an [`Agent`]
|
||||
fn next_move(&mut self, board: &Board) -> Option<CoordPair>;
|
||||
/// Returns the name of the [`Agent`]
|
||||
|
||||
41
src/elo.rs
41
src/elo.rs
@ -65,23 +65,19 @@ pub fn run() {
|
||||
),
|
||||
]);
|
||||
|
||||
// for _ in 0..100 {
|
||||
// arena.prop_arena();
|
||||
// }
|
||||
|
||||
for _ in 0..20 {
|
||||
arena.play_all_against(0);
|
||||
}
|
||||
|
||||
// for _ in 0..2 {
|
||||
// arena.play_two(1, 2);
|
||||
// }
|
||||
arena.play(
|
||||
&(0..arena.players.len())
|
||||
.zip([0].into_iter().cycle())
|
||||
.filter(|(i, j)| i != j)
|
||||
.collect::<Vec<_>>()
|
||||
.repeat(20),
|
||||
);
|
||||
|
||||
println!("{}", arena);
|
||||
}
|
||||
|
||||
pub struct PlayerArena {
|
||||
/// Name, Creator, Elo, freq (mod)
|
||||
/// Name, Creator, Elo
|
||||
players: Vec<(String, Box<dyn Fn(Piece) -> Box<dyn Agent>>, EloRating)>,
|
||||
}
|
||||
|
||||
@ -124,27 +120,26 @@ impl PlayerArena {
|
||||
)
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
// .into_iter()
|
||||
// after the agents are created, we can multithread the games being played
|
||||
.into_par_iter()
|
||||
.map(|((i, j), (p1, p2))| (i, j, Self::play_two_inner(p1, p2)))
|
||||
.collect::<Vec<_>>()
|
||||
// collect and process the outcomes of all the games
|
||||
.into_iter()
|
||||
.for_each(|(i, j, o)| self.process_outcome(i, j, &o));
|
||||
}
|
||||
|
||||
fn play_all_against(&mut self, i: usize) {
|
||||
fn prop_arena(&mut self) {
|
||||
self.play(
|
||||
&(0..self.players.len())
|
||||
.zip([i].into_iter().cycle())
|
||||
.filter(|(i, j)| i != j)
|
||||
.flat_map(|i| {
|
||||
(0..self.players.len())
|
||||
.map(move |j| (i, j))
|
||||
.filter(|(i, j)| i != j)
|
||||
.collect::<Vec<_>>()
|
||||
})
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
}
|
||||
|
||||
fn prop_arena(&mut self) {
|
||||
for i in 0..self.players.len() {
|
||||
self.play_all_against(i);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
fn process_outcome(&mut self, player1: usize, player2: usize, outcome: &Outcomes) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user