summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2020-04-19 19:31:55 +0200
committerJustin Wernick <justin@worthe-it.co.za>2020-04-19 19:31:55 +0200
commit8eebb5cb47f91b4b8d5fb8dd3a30d932ad4c8134 (patch)
tree9ab1f8080f13b3097c84502f12be5b1b56350c2f
parent943887fd7bcb031828b989a3e49876658fa0faff (diff)
Experimenting with if I need decelerate
-rw-r--r--vroomba-analysis/src/main.rs55
1 files changed, 39 insertions, 16 deletions
diff --git a/vroomba-analysis/src/main.rs b/vroomba-analysis/src/main.rs
index 931832f..ca22abf 100644
--- a/vroomba-analysis/src/main.rs
+++ b/vroomba-analysis/src/main.rs
@@ -10,6 +10,9 @@ use vroomba::state::*;
#[derive(StructOpt, Debug)]
#[structopt(name = "vroomba-analysis")]
struct Opt {
+ /// Find out if there's a shorter path that uses the decelerate move
+ #[structopt(long)]
+ decelerate_experiment: bool,
/// Path to GlobalState.json
path: PathBuf,
}
@@ -19,13 +22,36 @@ fn main() {
let initial_state =
global_json::read_initial_state_from_global_json_file(opt.path.to_str().unwrap()).unwrap();
- let shortest_path = astar(
- &initial_state,
+ if opt.decelerate_experiment {
+ let shortest_path_with_decelerate = shortest_path(&initial_state, false);
+ let shortest_path_without_decelerate = shortest_path(&initial_state, true);
+ println!("With Decelerate");
+ log_shortest_path(&initial_state, &shortest_path_with_decelerate);
+ println!("Without Decelerate");
+ log_shortest_path(&initial_state, &shortest_path_without_decelerate);
+ if shortest_path_with_decelerate.len() < shortest_path_without_decelerate.len() {
+ println!("With decelerate is faster!");
+ } else {
+ println!("Same length!");
+ }
+ } else {
+ let shortest_path_actions = shortest_path(&initial_state, false);
+ log_shortest_path(&initial_state, &shortest_path_actions);
+ }
+}
+
+fn shortest_path(
+ initial_state: &GameState,
+ exclude_decelerate: bool,
+) -> Vec<(Position, Position, GameState, GameState, Command)> {
+ let shortest_path_states = astar(
+ initial_state,
|state| {
let player_moves = state.valid_moves(0);
player_moves
.into_iter()
.filter(|player_move| *player_move != Command::UseOil)
+ .filter(|player_move| !exclude_decelerate || *player_move != Command::Decelerate)
.map(|player_move| {
let mut state = state.clone();
state.update([player_move, Command::Decelerate]);
@@ -38,10 +64,10 @@ fn main() {
)
.unwrap();
- let shortest_path_actions = shortest_path
+ shortest_path_states
.0
.iter()
- .zip(shortest_path.0.iter().skip(1))
+ .zip(shortest_path_states.0.iter().skip(1))
.map(|(state, next)| {
let player = &state.players[0];
let player_move = state
@@ -56,13 +82,18 @@ fn main() {
(
player.position,
next.players[0].position,
- state,
- next,
+ state.clone(),
+ next.clone(),
player_move,
)
})
- .collect::<Vec<_>>();
+ .collect()
+}
+fn log_shortest_path(
+ initial_state: &GameState,
+ shortest_path_actions: &Vec<(Position, Position, GameState, GameState, Command)>,
+) {
let chunk_size = 100;
for chunk in 0..(WIDTH / chunk_size) {
let start_x = chunk * chunk_size;
@@ -126,13 +157,5 @@ fn main() {
);
}
- // println!(
- // "{:?}",
- // shortest_path
- // .0
- // .into_iter()
- // .map(|node| node.last_command)
- // .collect::<Vec<_>>()
- // );
- println!("{} moves", shortest_path.1);
+ println!("{} moves", shortest_path_actions.len());
}