summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2020-04-19 21:48:50 +0200
committerJustin Wernick <justin@worthe-it.co.za>2020-04-19 21:48:50 +0200
commit7162aa803abd69a24f9e85c6681561d97fa79c15 (patch)
treebc34759af4677a4256299a4d1234d6ef6efb760b
parent8eebb5cb47f91b4b8d5fb8dd3a30d932ad4c8134 (diff)
Fixed accelerate implementation
-rw-r--r--src/state.rs5
-rw-r--r--vroomba-analysis/src/main.rs37
2 files changed, 31 insertions, 11 deletions
diff --git a/src/state.rs b/src/state.rs
index 47a2168..6590d5e 100644
--- a/src/state.rs
+++ b/src/state.rs
@@ -145,9 +145,7 @@ impl GameState {
let player = &self.players[player_index];
let mut result = Vec::with_capacity(7);
result.push(Command::Nothing);
- if player.speed < SPEED_4 {
- result.push(Command::Accelerate);
- }
+ result.push(Command::Accelerate);
if player.speed > SPEED_0 {
result.push(Command::Decelerate);
}
@@ -173,6 +171,7 @@ impl Player {
i if i < SPEED_1 => SPEED_1,
i if i < SPEED_2 => SPEED_2,
i if i < SPEED_3 => SPEED_3,
+ SPEED_BOOST => SPEED_BOOST,
_ => SPEED_4,
};
}
diff --git a/vroomba-analysis/src/main.rs b/vroomba-analysis/src/main.rs
index ca22abf..721064a 100644
--- a/vroomba-analysis/src/main.rs
+++ b/vroomba-analysis/src/main.rs
@@ -13,6 +13,9 @@ struct Opt {
/// Find out if there's a shorter path that uses the decelerate move
#[structopt(long)]
decelerate_experiment: bool,
+ /// Find out if there's a shorter path that uses the nothing move (instead of accelerate)
+ #[structopt(long)]
+ nothing_experiment: bool,
/// Path to GlobalState.json
path: PathBuf,
}
@@ -23,8 +26,9 @@ fn main() {
global_json::read_initial_state_from_global_json_file(opt.path.to_str().unwrap()).unwrap();
if opt.decelerate_experiment {
- let shortest_path_with_decelerate = shortest_path(&initial_state, false);
- let shortest_path_without_decelerate = shortest_path(&initial_state, true);
+ let shortest_path_with_decelerate = shortest_path(&initial_state, &[]);
+ let shortest_path_without_decelerate =
+ shortest_path(&initial_state, &[Command::Decelerate]);
println!("With Decelerate");
log_shortest_path(&initial_state, &shortest_path_with_decelerate);
println!("Without Decelerate");
@@ -34,24 +38,38 @@ fn main() {
} else {
println!("Same length!");
}
+ }
+ if opt.nothing_experiment {
+ let shortest_path_with = shortest_path(&initial_state, &[]);
+ let shortest_path_without = shortest_path(&initial_state, &[Command::Nothing]);
+ println!("With Nothing");
+ log_shortest_path(&initial_state, &shortest_path_with);
+ println!("Without Nothing");
+ log_shortest_path(&initial_state, &shortest_path_without);
+ if shortest_path_with.len() < shortest_path_without.len() {
+ println!("With nothing is faster!");
+ } else {
+ println!("Same length!");
+ }
} else {
- let shortest_path_actions = shortest_path(&initial_state, false);
+ let shortest_path_actions = shortest_path(&initial_state, &[]);
log_shortest_path(&initial_state, &shortest_path_actions);
}
}
fn shortest_path(
initial_state: &GameState,
- exclude_decelerate: bool,
+ blacklist: &[Command],
) -> Vec<(Position, Position, GameState, GameState, Command)> {
let shortest_path_states = astar(
initial_state,
|state| {
- let player_moves = state.valid_moves(0);
- player_moves
+ state
+ .valid_moves(0)
.into_iter()
- .filter(|player_move| *player_move != Command::UseOil)
- .filter(|player_move| !exclude_decelerate || *player_move != Command::Decelerate)
+ .filter(|player_move| {
+ *player_move != Command::UseOil && !blacklist.contains(player_move)
+ })
.map(|player_move| {
let mut state = state.clone();
state.update([player_move, Command::Decelerate]);
@@ -73,6 +91,9 @@ fn shortest_path(
let player_move = state
.valid_moves(0)
.into_iter()
+ .filter(|player_move| {
+ *player_move != Command::UseOil && !blacklist.contains(player_move)
+ })
.find(|player_move| {
let mut state = state.clone();
state.update([*player_move, Command::Decelerate]);