Skip to content

Commit

Permalink
refactor: split out heuristics function for experimentation
Browse files Browse the repository at this point in the history
  • Loading branch information
glitchassassin committed Dec 20, 2024
1 parent 1cb326a commit fe17228
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 19 deletions.
11 changes: 11 additions & 0 deletions lib/algorithms/astar/heuristics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use screeps::Position;

/// A basic global range heuristic.
pub fn range_heuristic<'a>(goal: &'a [Position]) -> impl Fn(Position) -> usize + 'a {
move |position| {
goal.iter()
.map(|g| position.get_range_to(*g))
.min()
.unwrap_or(0) as usize
}
}
1 change: 1 addition & 0 deletions lib/algorithms/astar/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod heuristics;
pub mod multiroom_distance_map;
30 changes: 16 additions & 14 deletions lib/algorithms/astar/multiroom_distance_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use std::convert::TryFrom;
use wasm_bindgen::prelude::*;
use wasm_bindgen::throw_val;

use super::heuristics::range_heuristic;

#[derive(Copy, Clone, Eq, PartialEq)]
struct State {
f_score: usize,
Expand All @@ -33,13 +35,6 @@ impl PartialOrd for State {
}
}

fn heuristic(position: Position, goal: &[Position]) -> usize {
goal.iter()
.map(|g| position.get_range_to(*g))
.min()
.unwrap_or(0) as usize
}

lazy_static! {
static ref DIRECTION_LOOKUP: [Vec<Direction>; 9] = [
// Any direction
Expand Down Expand Up @@ -117,7 +112,8 @@ pub fn astar_multiroom_distance_map(
get_cost_matrix: impl Fn(RoomName) -> Option<ClockworkCostMatrix>,
max_tiles: usize,
max_tile_distance: usize,
goals: Vec<Position>,
goal_fn: impl Fn(Position) -> bool,
heuristic_fn: impl Fn(Position) -> usize,
) -> MultiroomDistanceMap {
set_panic_hook();
let mut frontier = BinaryHeap::new();
Expand Down Expand Up @@ -189,7 +185,7 @@ pub fn astar_multiroom_distance_map(
continue;
}

let h_score = heuristic(neighbor, &goals);
let h_score = heuristic_fn(neighbor);
let f_score = next_cost.saturating_add(h_score);
frontier.push(State {
f_score,
Expand All @@ -200,7 +196,7 @@ pub fn astar_multiroom_distance_map(
current_room_distance_map[neighbor.xy()] = next_cost;
visited += 1;

if goals.contains(&neighbor) || visited >= max_tiles {
if goal_fn(neighbor) || visited >= max_tiles {
return multiroom_distance_map;
}
}
Expand All @@ -221,6 +217,14 @@ pub fn js_astar_multiroom_distance_map(
.iter()
.map(|pos| Position::from_packed(*pos))
.collect();

let destinations: Vec<Position> = destinations
.iter()
.map(|pos| Position::from_packed(*pos))
.collect();

let heuristic_fn = range_heuristic(&destinations);

astar_multiroom_distance_map(
start_positions,
|room| {
Expand Down Expand Up @@ -248,9 +252,7 @@ pub fn js_astar_multiroom_distance_map(
},
max_tiles,
max_tile_distance,
destinations
.iter()
.map(|pos| Position::from_packed(*pos))
.collect(),
|pos| destinations.contains(&pos),
heuristic_fn,
)
}
10 changes: 5 additions & 5 deletions lib/datatypes/optional_cache.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use std::cell::RefCell;
use std::collections::HashMap;
use std::hash::Hash;
use std::rc::Rc;
use std::sync::{Arc, Mutex};

#[derive(Debug, Clone)]
pub struct OptionalCache<K, V, F>
where
F: Fn(K) -> Option<V>,
{
cache: Rc<RefCell<HashMap<K, Option<V>>>>,
cache: Arc<Mutex<HashMap<K, Option<V>>>>,
creator: F,
}

Expand All @@ -20,14 +19,15 @@ where
{
pub fn new(creator: F) -> Self {
Self {
cache: Rc::new(RefCell::new(HashMap::new())),
cache: Arc::new(Mutex::new(HashMap::new())),
creator,
}
}

pub fn get_or_create(&self, key: K) -> Option<V> {
self.cache
.borrow_mut()
.lock()
.unwrap()
.entry(key.clone())
.or_insert_with(|| (self.creator)(key))
.clone()
Expand Down

0 comments on commit fe17228

Please sign in to comment.