made simulations parallel
This commit is contained in:
parent
de7b51acb4
commit
0080b94fd2
5 changed files with 21 additions and 33 deletions
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "rs48_lib"
|
||||
version = "1.2.0"
|
||||
version = "1.3.0"
|
||||
edition = "2021"
|
||||
description = "components of rs48"
|
||||
license = "MIT"
|
||||
|
@ -8,4 +8,5 @@ authors = ["JOLIMAITRE Matthieu <matthieu@imagevo.fr>"]
|
|||
|
||||
[dependencies]
|
||||
rand = "0.8"
|
||||
rayon = "1.5.3"
|
||||
termion = "1.5"
|
||||
|
|
|
@ -4,14 +4,9 @@ use termion::{event::Key, input::TermRead, raw::IntoRawMode};
|
|||
use super::{Controller, ControllerError, Move};
|
||||
use crate::game::Game;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct PlayerController;
|
||||
|
||||
impl PlayerController {
|
||||
pub fn new() -> Self {
|
||||
Self
|
||||
}
|
||||
}
|
||||
|
||||
impl Controller for PlayerController {
|
||||
fn next_move(&mut self, _game: &Game) -> Result<Move, ControllerError> {
|
||||
let stdin = stdin();
|
||||
|
|
|
@ -3,14 +3,9 @@ use rand::random;
|
|||
use super::{Controller, ControllerError, Move};
|
||||
use crate::game::Game;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct RandomController;
|
||||
|
||||
impl RandomController {
|
||||
pub fn new() -> Self {
|
||||
Self
|
||||
}
|
||||
}
|
||||
|
||||
impl Controller for RandomController {
|
||||
fn next_move(&mut self, _game: &Game) -> Result<Move, ControllerError> {
|
||||
let movement = random();
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
use std::ops::Add;
|
||||
|
||||
use rayon::prelude::{IntoParallelIterator, ParallelIterator};
|
||||
|
||||
use crate::{game::Game, prelude::RandomController};
|
||||
|
||||
use super::{Controller, ControllerError, Move};
|
||||
|
||||
pub enum Objective {
|
||||
Score,
|
||||
TileCount,
|
||||
}
|
||||
|
||||
pub struct SimulatedController {
|
||||
simulations_per_move: usize,
|
||||
length_of_simulation: usize,
|
||||
|
@ -26,35 +25,33 @@ impl Controller for SimulatedController {
|
|||
let initial_score = game.get_score();
|
||||
|
||||
let mut scores: Vec<_> = Move::all()
|
||||
.into_iter()
|
||||
.into_par_iter()
|
||||
.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();
|
||||
|
||||
game.turn(initial_move.clone()).ok();
|
||||
let mut controller = RandomController::new();
|
||||
let mut controller = RandomController::default();
|
||||
for _ in 1..self.length_of_simulation {
|
||||
let movement = controller.next_move(&game).ok();
|
||||
let result = movement.and_then(|movement| game.turn(movement).ok());
|
||||
if result.is_none() {
|
||||
continue;
|
||||
break;
|
||||
}
|
||||
}
|
||||
game.get_score() - initial_score
|
||||
});
|
||||
|
||||
let mut res = 0;
|
||||
let mut it = 0;
|
||||
for score in sim_scores {
|
||||
it += 1;
|
||||
res += score;
|
||||
}
|
||||
|
||||
(initial_move, res / it)
|
||||
let sim_scores: Vec<_> = sim_scores.collect();
|
||||
let avg =
|
||||
sim_scores.iter().cloned().reduce(Add::add).unwrap_or(0) / sim_scores.len();
|
||||
(initial_move, avg)
|
||||
})
|
||||
.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())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -69,7 +69,7 @@ impl Game {
|
|||
let Rules {
|
||||
size,
|
||||
spawn_per_turn,
|
||||
} = rules.clone();
|
||||
} = rules;
|
||||
|
||||
Self {
|
||||
board: Grid::new(size),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue