summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJustin Worthe <justin@worthe-it.co.za>2018-05-16 00:01:27 +0200
committerJustin Worthe <justin@worthe-it.co.za>2018-05-16 00:01:27 +0200
commitc6a7a3154b7ba59f3abc7581b35dd460023cc8f9 (patch)
treeac2fc54bffc8ce8fcc65bbc9942e3736be39b8a6 /src
parent29fb64e557e40afc8d58ae34c65650da9ea3c511 (diff)
Moved away from special benchmarking suite
Just using normal monte carlo. More iterations -> better.
Diffstat (limited to 'src')
-rw-r--r--src/bin/perf-test.rs46
-rw-r--r--src/strategy/monte_carlo.rs24
2 files changed, 35 insertions, 35 deletions
diff --git a/src/bin/perf-test.rs b/src/bin/perf-test.rs
index 835267e..03da160 100644
--- a/src/bin/perf-test.rs
+++ b/src/bin/perf-test.rs
@@ -3,35 +3,20 @@ extern crate time;
use time::{PreciseTime, Duration};
use zombot::*;
-use zombot::engine::command::Command;
-
-use std::error::Error;
const STATE_PATH: &str = "tests/state0.json";
+const STATE_BIG_PATH: &str = "tests/bigstate.json";
-const COMMAND_PATH: &str = "command.txt";
-
-use std::fs::File;
-use std::io::prelude::*;
use std::process;
-fn choose_move(settings: &engine::settings::GameSettings, state: &engine::GameState, start_time: &PreciseTime) -> Command {
- let max_time = Duration::milliseconds(1950);
- strategy::monte_carlo::choose_move(settings, state, start_time, max_time)
-}
-
-
-fn write_command(filename: &str, command: Command) -> Result<(), Box<Error> > {
- let mut file = File::create(filename)?;
- write!(file, "{}", command)?;
- Ok(())
+fn main() {
+ normal_state();
+ big_state();
}
-
-fn main() {
+fn normal_state() {
+ println!("Normal size state file");
let start_time = PreciseTime::now();
-
- println!("Reading in state.json file");
let (settings, state) = match json::read_state_from_file(STATE_PATH) {
Ok(ok) => ok,
Err(error) => {
@@ -39,15 +24,20 @@ fn main() {
process::exit(1);
}
};
- let command = choose_move(&settings, &state, &start_time);
+ let max_time = Duration::milliseconds(1950);
+ strategy::monte_carlo::choose_move(&settings, &state, &start_time, max_time);
+}
- match write_command(COMMAND_PATH, command) {
- Ok(()) => {}
+fn big_state() {
+ println!("Big state file");
+ let start_time = PreciseTime::now();
+ let (settings, state) = match json::read_state_from_file(STATE_BIG_PATH) {
+ Ok(ok) => ok,
Err(error) => {
- println!("Error while writing command file: {}", error);
+ println!("Error while parsing JSON file: {}", error);
process::exit(1);
}
- }
-
- println!("Elapsed time: {}", start_time.to(PreciseTime::now()));
+ };
+ let max_time = Duration::milliseconds(1950);
+ strategy::monte_carlo::choose_move(&settings, &state, &start_time, max_time);
}
diff --git a/src/strategy/monte_carlo.rs b/src/strategy/monte_carlo.rs
index b7025c5..fe0462f 100644
--- a/src/strategy/monte_carlo.rs
+++ b/src/strategy/monte_carlo.rs
@@ -8,19 +8,29 @@ const MAX_MOVES: u16 = 400;
use time::{Duration, PreciseTime};
+#[cfg(not(feature = "single-threaded"))]
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 command_scores = CommandScore::init_command_scores(settings, state);
loop {
- command_scores.par_iter_mut()
- .for_each(|score| {
- let mut rng = thread_rng();
- simulate_to_endstate(score, settings, state, &mut rng);
- });
+ #[cfg(feature = "single-threaded")]
+ {
+ command_scores.iter_mut()
+ .for_each(|score| {
+ let mut rng = thread_rng();
+ simulate_to_endstate(score, settings, state, &mut rng);
+ });
+ }
+ #[cfg(not(feature = "single-threaded"))]
+ {
+ 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;
}