summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2020-04-18 23:25:16 +0200
committerJustin Wernick <justin@worthe-it.co.za>2020-04-18 23:25:16 +0200
commit91c144969f6dd2daa7d9fb2e5d59059ecd556b79 (patch)
treecdc07ae81805657bb6ca4b6bd0ec0cafbfa691bd
parentf3a65c87313d2952f83d5f43327260b7a9ab81cc (diff)
Optimimze to have a shortest path that actually completes
-rw-r--r--src/global_json.rs59
-rw-r--r--src/json.rs67
-rw-r--r--src/state.rs15
-rw-r--r--vroomba-analysis/src/main.rs29
4 files changed, 92 insertions, 78 deletions
diff --git a/src/global_json.rs b/src/global_json.rs
index a27cd00..189a97b 100644
--- a/src/global_json.rs
+++ b/src/global_json.rs
@@ -1,5 +1,6 @@
use std::fs::File;
use std::io::prelude::*;
+use std::rc::Rc;
use anyhow::Result;
use serde::{Deserialize, Serialize};
@@ -99,33 +100,37 @@ impl JsonState {
GameState {
status: GameStatus::Continue,
players: [self.players[0].to_player(), self.players[1].to_player()],
- obstacles: self
- .blocks
- .iter()
- .filter(|cell| {
- cell.surface_object == JsonSurfaceObject::Mud
- || cell.surface_object == JsonSurfaceObject::OilSpill
- })
- .map(|cell| cell.position.to_position())
- .collect(),
- powerup_oils: self
- .blocks
- .iter()
- .filter(|cell| cell.surface_object == JsonSurfaceObject::OilItem)
- .map(|cell| cell.position.to_position())
- .collect(),
- powerup_boosts: self
- .blocks
- .iter()
- .filter(|cell| cell.surface_object == JsonSurfaceObject::Boost)
- .map(|cell| cell.position.to_position())
- .collect(),
- finish_lines: self
- .blocks
- .iter()
- .filter(|cell| cell.surface_object == JsonSurfaceObject::FinishLine)
- .map(|cell| cell.position.to_position())
- .collect(),
+ obstacles: Rc::new(
+ self.blocks
+ .iter()
+ .filter(|cell| {
+ cell.surface_object == JsonSurfaceObject::Mud
+ || cell.surface_object == JsonSurfaceObject::OilSpill
+ })
+ .map(|cell| cell.position.to_position())
+ .collect(),
+ ),
+ powerup_oils: Rc::new(
+ self.blocks
+ .iter()
+ .filter(|cell| cell.surface_object == JsonSurfaceObject::OilItem)
+ .map(|cell| cell.position.to_position())
+ .collect(),
+ ),
+ powerup_boosts: Rc::new(
+ self.blocks
+ .iter()
+ .filter(|cell| cell.surface_object == JsonSurfaceObject::Boost)
+ .map(|cell| cell.position.to_position())
+ .collect(),
+ ),
+ finish_lines: Rc::new(
+ self.blocks
+ .iter()
+ .filter(|cell| cell.surface_object == JsonSurfaceObject::FinishLine)
+ .map(|cell| cell.position.to_position())
+ .collect(),
+ ),
}
}
}
diff --git a/src/json.rs b/src/json.rs
index 1726f9f..fa90e06 100644
--- a/src/json.rs
+++ b/src/json.rs
@@ -1,5 +1,6 @@
use std::fs::File;
use std::io::prelude::*;
+use std::rc::Rc;
use anyhow::Result;
use serde::{Deserialize, Serialize};
@@ -102,37 +103,41 @@ impl JsonState {
GameState {
status: GameStatus::Continue,
players: [self.player.to_player(), self.opponent.to_player()],
- obstacles: self
- .world_map
- .iter()
- .flatten()
- .filter(|cell| {
- cell.surface_object == JsonSurfaceObject::Mud
- || cell.surface_object == JsonSurfaceObject::OilSpill
- })
- .map(|cell| cell.position.to_position())
- .collect(),
- powerup_oils: self
- .world_map
- .iter()
- .flatten()
- .filter(|cell| cell.surface_object == JsonSurfaceObject::OilItem)
- .map(|cell| cell.position.to_position())
- .collect(),
- powerup_boosts: self
- .world_map
- .iter()
- .flatten()
- .filter(|cell| cell.surface_object == JsonSurfaceObject::Boost)
- .map(|cell| cell.position.to_position())
- .collect(),
- finish_lines: self
- .world_map
- .iter()
- .flatten()
- .filter(|cell| cell.surface_object == JsonSurfaceObject::FinishLine)
- .map(|cell| cell.position.to_position())
- .collect(),
+ obstacles: Rc::new(
+ self.world_map
+ .iter()
+ .flatten()
+ .filter(|cell| {
+ cell.surface_object == JsonSurfaceObject::Mud
+ || cell.surface_object == JsonSurfaceObject::OilSpill
+ })
+ .map(|cell| cell.position.to_position())
+ .collect(),
+ ),
+ powerup_oils: Rc::new(
+ self.world_map
+ .iter()
+ .flatten()
+ .filter(|cell| cell.surface_object == JsonSurfaceObject::OilItem)
+ .map(|cell| cell.position.to_position())
+ .collect(),
+ ),
+ powerup_boosts: Rc::new(
+ self.world_map
+ .iter()
+ .flatten()
+ .filter(|cell| cell.surface_object == JsonSurfaceObject::Boost)
+ .map(|cell| cell.position.to_position())
+ .collect(),
+ ),
+ finish_lines: Rc::new(
+ self.world_map
+ .iter()
+ .flatten()
+ .filter(|cell| cell.surface_object == JsonSurfaceObject::FinishLine)
+ .map(|cell| cell.position.to_position())
+ .collect(),
+ ),
}
}
}
diff --git a/src/state.rs b/src/state.rs
index 177db45..78f8267 100644
--- a/src/state.rs
+++ b/src/state.rs
@@ -1,5 +1,6 @@
use crate::command::Command;
use crate::consts::*;
+use std::rc::Rc;
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum GameStatus {
@@ -13,10 +14,10 @@ pub enum GameStatus {
pub struct GameState {
pub status: GameStatus,
pub players: [Player; 2],
- pub obstacles: Vec<Position>,
- pub powerup_oils: Vec<Position>,
- pub powerup_boosts: Vec<Position>,
- pub finish_lines: Vec<Position>,
+ pub obstacles: Rc<Vec<Position>>,
+ pub powerup_oils: Rc<Vec<Position>>,
+ pub powerup_boosts: Rc<Vec<Position>>,
+ pub finish_lines: Rc<Vec<Position>>,
}
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
@@ -91,10 +92,12 @@ impl GameState {
debug_assert!(self.players[player_index].oils > 0);
self.players[player_index].oils = self.players[player_index].oils.saturating_sub(1);
let player_position = self.players[player_index].position;
- self.obstacles.push(Position {
+ let mut obstacles = (*self.obstacles).clone();
+ obstacles.push(Position {
x: player_position.x.saturating_sub(1),
y: player_position.y,
});
+ self.obstacles = Rc::new(obstacles);
}
}
@@ -155,7 +158,7 @@ impl GameState {
if player.boosts > 0 {
result.push(Command::UseBoost);
}
- if player.oils > 0 {
+ if false && player.oils > 0 {
result.push(Command::UseOil);
}
result
diff --git a/vroomba-analysis/src/main.rs b/vroomba-analysis/src/main.rs
index e1d244b..3abcfc6 100644
--- a/vroomba-analysis/src/main.rs
+++ b/vroomba-analysis/src/main.rs
@@ -1,4 +1,4 @@
-use pathfinding::directed::astar;
+use pathfinding::prelude::*;
use std::path::PathBuf;
use structopt::StructOpt;
use vroomba::command::Command;
@@ -18,10 +18,10 @@ fn main() {
let initial_node = Node {
state: global_json::read_initial_state_from_global_json_file(opt.path.to_str().unwrap())
.unwrap(),
- last_command: Command::Nothing,
+ //last_command: Command::Nothing,
};
- let shortest_path = astar::astar(
+ let shortest_path = astar(
&initial_node,
|node| {
let player_moves = node.state.valid_moves(0);
@@ -29,11 +29,11 @@ fn main() {
.into_iter()
.map(|player_move| {
let mut state = node.state.clone();
- state.update([player_move, Command::Accelerate]);
+ state.update([player_move, Command::Decelerate]);
(
Node {
state,
- last_command: player_move,
+ //last_command: player_move,
},
1,
)
@@ -45,19 +45,20 @@ fn main() {
)
.unwrap();
- println!(
- "{:?}",
- shortest_path
- .0
- .into_iter()
- .map(|node| node.last_command)
- .collect::<Vec<_>>()
- );
+ // println!(
+ // "{:?}",
+ // shortest_path
+ // .0
+ // .into_iter()
+ // .map(|node| node.last_command)
+ // .collect::<Vec<_>>()
+ // );
+ println!("{:?}", shortest_path.0);
println!("{}", shortest_path.1);
}
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
struct Node {
state: GameState,
- last_command: Command,
+ //last_command: Command,
}