Add second queue to make paralellization easier

This commit is contained in:
Daniel S. 2019-06-06 18:22:45 +02:00
parent 6365c58bd4
commit f00237cb65

View file

@ -5,6 +5,8 @@ extern crate serde_derive;
extern crate fnv; extern crate fnv;
extern crate humantime; extern crate humantime;
// extern crate rayon;
// use rayon::prelude::*;
use fnv::{FnvHashMap, FnvHashSet}; use fnv::{FnvHashMap, FnvHashSet};
use humantime::format_duration; use humantime::format_duration;
use rstar::{PointDistance, RTree, RTreeObject, AABB}; use rstar::{PointDistance, RTree, RTreeObject, AABB};
@ -181,7 +183,7 @@ impl Router {
return self.scoopable.contains(&sys.id); return self.scoopable.contains(&sys.id);
} }
pub fn route(&mut self, src: &[f32; 3], dst: &[f32; 3]) -> Vec<(&System, &Point)> { pub fn route(&mut self, src: &[f32; 3], dst: &[f32; 3]) -> Option<Vec<(&System, &Point)>> {
let start_sys = self.closest(src); let start_sys = self.closest(src);
let goal_sys = self.closest(dst); let goal_sys = self.closest(dst);
let total = self.tree.size() as f32; let total = self.tree.size() as f32;
@ -189,11 +191,12 @@ impl Router {
let mut seen = FnvHashSet::default(); let mut seen = FnvHashSet::default();
let t_start = Instant::now(); let t_start = Instant::now();
let mut depth = 0; let mut depth = 0;
let mut found = false;
let mut queue: VecDeque<(usize, &Point)> = VecDeque::new(); let mut queue: VecDeque<(usize, &Point)> = VecDeque::new();
let mut r_queue: VecDeque<(usize, &Point)> = VecDeque::new(); let mut queue_next: VecDeque<(usize, &Point)> = VecDeque::new();
queue.push_front((0, &start_sys)); queue.push_front((0, &start_sys));
r_queue.push_front((0, &goal_sys));
seen.insert(start_sys.id); seen.insert(start_sys.id);
while !queue.is_empty() {
while let Some((d, sys)) = queue.pop_front() { while let Some((d, sys)) = queue.pop_front() {
if d != depth { if d != depth {
depth = d; depth = d;
@ -208,10 +211,28 @@ impl Router {
std::io::stdout().flush().unwrap(); std::io::stdout().flush().unwrap();
} }
if sys.id == goal_sys.id { if sys.id == goal_sys.id {
found = true;
break;
}
let nbs = self
.neighbours(&sys)
.filter(|&nb| (self.valid(nb) || (nb.id == goal_sys.id)));
for nb in nbs {
if seen.insert(nb.id) {
prev.insert(nb.id, sys.id);
queue_next.push_back((d + 1, nb));
}
}
}
std::mem::swap(&mut queue, &mut queue_next);
}
println!(); println!();
if !found {
return None;
}
let points = self.preload_points(); let points = self.preload_points();
let mut v: Vec<(&System, &Point)> = Vec::new(); let mut v: Vec<(&System, &Point)> = Vec::new();
let mut prev_sys_id = sys.id; let mut prev_sys_id = goal_sys.id;
loop { loop {
if let Some(sys) = self.systems.get(&prev_sys_id) { if let Some(sys) = self.systems.get(&prev_sys_id) {
v.push((sys, points[&sys.id])); v.push((sys, points[&sys.id]));
@ -226,20 +247,7 @@ impl Router {
} }
} }
v.reverse(); v.reverse();
return v; return Some(v);
}
let nbs = self
.neighbours(&sys)
.filter(|&nb| (self.valid(nb) || (nb.id == goal_sys.id)));
for nb in nbs {
if seen.insert(nb.id) {
prev.insert(nb.id, sys.id);
queue.push_back((d + 1, nb));
}
}
}
println!("No route found!");
return Vec::new();
} }
} }
@ -254,6 +262,10 @@ fn main() {
&[-1111.5625, -134.21875, 65269.75], // Beagle Point &[-1111.5625, -134.21875, 65269.75], // Beagle Point
// &[-7095.375, 401.25, 2396.8125], // V1357 Cygni, // &[-7095.375, 401.25, 2396.8125], // V1357 Cygni,
); // Ix -> BP 537 ); // Ix -> BP 537
let route = match route {
Some(r) => r,
None => Vec::new(),
};
println!( println!(
"Done in {} ({} Jumps)!\n", "Done in {} ({} Jumps)!\n",
format_duration(t_route.elapsed()), format_duration(t_route.elapsed()),