made simulations parallel

This commit is contained in:
JOLIMAITRE Matthieu 2022-09-29 15:47:32 +02:00
parent de7b51acb4
commit 0080b94fd2
5 changed files with 21 additions and 33 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "rs48_lib" name = "rs48_lib"
version = "1.2.0" version = "1.3.0"
edition = "2021" edition = "2021"
description = "components of rs48" description = "components of rs48"
license = "MIT" license = "MIT"
@ -8,4 +8,5 @@ authors = ["JOLIMAITRE Matthieu <matthieu@imagevo.fr>"]
[dependencies] [dependencies]
rand = "0.8" rand = "0.8"
rayon = "1.5.3"
termion = "1.5" termion = "1.5"

View file

@ -4,14 +4,9 @@ use termion::{event::Key, input::TermRead, raw::IntoRawMode};
use super::{Controller, ControllerError, Move}; use super::{Controller, ControllerError, Move};
use crate::game::Game; use crate::game::Game;
#[derive(Debug, Default)]
pub struct PlayerController; pub struct PlayerController;
impl PlayerController {
pub fn new() -> Self {
Self
}
}
impl Controller for PlayerController { impl Controller for PlayerController {
fn next_move(&mut self, _game: &Game) -> Result<Move, ControllerError> { fn next_move(&mut self, _game: &Game) -> Result<Move, ControllerError> {
let stdin = stdin(); let stdin = stdin();

View file

@ -3,14 +3,9 @@ use rand::random;
use super::{Controller, ControllerError, Move}; use super::{Controller, ControllerError, Move};
use crate::game::Game; use crate::game::Game;
#[derive(Debug, Default)]
pub struct RandomController; pub struct RandomController;
impl RandomController {
pub fn new() -> Self {
Self
}
}
impl Controller for RandomController { impl Controller for RandomController {
fn next_move(&mut self, _game: &Game) -> Result<Move, ControllerError> { fn next_move(&mut self, _game: &Game) -> Result<Move, ControllerError> {
let movement = random(); let movement = random();

View file

@ -1,12 +1,11 @@
use std::ops::Add;
use rayon::prelude::{IntoParallelIterator, ParallelIterator};
use crate::{game::Game, prelude::RandomController}; use crate::{game::Game, prelude::RandomController};
use super::{Controller, ControllerError, Move}; use super::{Controller, ControllerError, Move};
pub enum Objective {
Score,
TileCount,
}
pub struct SimulatedController { pub struct SimulatedController {
simulations_per_move: usize, simulations_per_move: usize,
length_of_simulation: usize, length_of_simulation: usize,
@ -26,35 +25,33 @@ impl Controller for SimulatedController {
let initial_score = game.get_score(); let initial_score = game.get_score();
let mut scores: Vec<_> = Move::all() let mut scores: Vec<_> = Move::all()
.into_iter() .into_par_iter()
.map(|initial_move| { .map(|initial_move| {
let sim_scores = (0..self.simulations_per_move).map(|_| { let sim_scores = (0..self.simulations_per_move).into_par_iter().map(|_| {
let mut game = game.clone(); let mut game = game.clone();
game.turn(initial_move.clone()).ok(); game.turn(initial_move.clone()).ok();
let mut controller = RandomController::new(); let mut controller = RandomController::default();
for _ in 1..self.length_of_simulation { for _ in 1..self.length_of_simulation {
let movement = controller.next_move(&game).ok(); let movement = controller.next_move(&game).ok();
let result = movement.and_then(|movement| game.turn(movement).ok()); let result = movement.and_then(|movement| game.turn(movement).ok());
if result.is_none() { if result.is_none() {
continue; break;
} }
} }
game.get_score() - initial_score game.get_score() - initial_score
}); });
let mut res = 0; let sim_scores: Vec<_> = sim_scores.collect();
let mut it = 0; let avg =
for score in sim_scores { sim_scores.iter().cloned().reduce(Add::add).unwrap_or(0) / sim_scores.len();
it += 1; (initial_move, avg)
res += score;
}
(initial_move, res / it)
}) })
.collect(); .collect();
scores.sort_by(|(_, a), (_, b)| a.cmp(b)); scores.sort_by(|(_, a), (_, b)| b.cmp(a));
let (m, _) = scores.last().unwrap(); dbg!(&scores);
let (m, _) = scores.first().unwrap();
Ok(m.clone()) Ok(m.clone())
} }
} }

View file

@ -69,7 +69,7 @@ impl Game {
let Rules { let Rules {
size, size,
spawn_per_turn, spawn_per_turn,
} = rules.clone(); } = rules;
Self { Self {
board: Grid::new(size), board: Grid::new(size),