summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJustin Worthe <justin@worthe-it.co.za>2018-05-15 23:15:02 +0200
committerJustin Worthe <justin@worthe-it.co.za>2018-05-15 23:15:11 +0200
commit4c0eed0ed7e7cc0fcec60624c0a0251a9e525fb1 (patch)
tree707fecfd39efaf01b12eacd478d96834bf3204df /src
parent0ebebeccd72ba76a2a9a9ad94c38feaed9b6ab1c (diff)
Rayon for threading
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs2
-rw-r--r--src/strategy/monte_carlo.rs11
2 files changed, 9 insertions, 4 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 728f747..6bcfc00 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -7,6 +7,8 @@ extern crate serde_derive;
extern crate rand;
extern crate time;
+extern crate rayon;
+
pub mod json;
pub mod engine;
pub mod strategy;
diff --git a/src/strategy/monte_carlo.rs b/src/strategy/monte_carlo.rs
index 6f0f681..b7025c5 100644
--- a/src/strategy/monte_carlo.rs
+++ b/src/strategy/monte_carlo.rs
@@ -8,16 +8,19 @@ const MAX_MOVES: u16 = 400;
use time::{Duration, PreciseTime};
+use rayon::prelude::*;
+
pub fn choose_move(settings: &GameSettings, state: &GameState, start_time: &PreciseTime, max_time: Duration) -> Command {
println!("Using MONTE_CARLO strategy");
- let mut rng = thread_rng();
let mut command_scores = CommandScore::init_command_scores(settings, state);
loop {
- for mut score in &mut command_scores {
- simulate_to_endstate(score, settings, state, &mut rng);
- }
+ command_scores.par_iter_mut()
+ .for_each(|score| {
+ let mut rng = thread_rng();
+ simulate_to_endstate(score, settings, state, &mut rng);
+ });
if start_time.to(PreciseTime::now()) > max_time {
break;
}