feat(GUI): Add Download functionality,
update Rust code, update Python code - Add Download functionality to GUI - Update rust code for use with GUI - Add callbacks to rust code - Add python modules for routing and download
This commit is contained in:
parent
a647d26337
commit
ec3972b06c
16 changed files with 775 additions and 1687 deletions
1233
rust/Cargo.lock
generated
1233
rust/Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -11,15 +11,13 @@ license = "WTFPL"
|
|||
crate-type = ["cdylib"]
|
||||
|
||||
[profile.release]
|
||||
# debug=true
|
||||
debug=true
|
||||
|
||||
[dependencies]
|
||||
csv = "1.1.1"
|
||||
serde = "1.0.94"
|
||||
serde_derive = "1.0.94"
|
||||
rstar = {version="0.4.0"}
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
rstar = "0.4.0"
|
||||
humantime = "1.2.0"
|
||||
structopt = "0.2.18"
|
||||
permutohedron = "0.2.4"
|
||||
serde_json = "1.0.40"
|
||||
indicatif = "0.11.0"
|
||||
|
@ -27,7 +25,6 @@ fnv = "1.0.6"
|
|||
bincode = "1.1.4"
|
||||
sha3 = "0.8.2"
|
||||
byteorder = "1.3.2"
|
||||
reqwest = "0.9.18"
|
||||
|
||||
[dependencies.pyo3]
|
||||
version = "0.7.0"
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct SystemSerde {
|
||||
pub id: u32,
|
||||
|
|
|
@ -1,63 +0,0 @@
|
|||
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::path::PathBuf;
|
||||
use structopt::StructOpt;
|
||||
|
||||
#[derive(Debug, StructOpt, Clone)]
|
||||
pub struct DownloadOpts {
|
||||
#[structopt(
|
||||
long = "bodies",
|
||||
default_value = "https://www.edsm.net/dump/bodies.json"
|
||||
)]
|
||||
/// Url to bodies.json
|
||||
bodies_url: String,
|
||||
#[structopt(
|
||||
long = "systems",
|
||||
default_value = "https://www.edsm.net/dump/systemsWithCoordinates.json"
|
||||
)]
|
||||
/// Url to systemsWithCoordinates.json
|
||||
systems_url: String,
|
||||
}
|
||||
|
||||
fn fetch_url(url: &str, prog_bar: &ProgressBar) -> std::io::Result<()> {
|
||||
let outfile = url.split('/').last().unwrap();
|
||||
let template = format!("{} {}", "[{elapsed_precise}] {binary_bytes}", outfile);
|
||||
prog_bar.set_style(ProgressStyle::default_bar().template(&template));
|
||||
|
||||
let client = reqwest::Client::builder().gzip(true).build().unwrap();
|
||||
let resp = client.get(url).send().unwrap();
|
||||
let target_path = PathBuf::from(format!("dumps/{}", outfile));
|
||||
if target_path.exists() {
|
||||
eprintln!("Error: Target {} exists!", outfile);
|
||||
return Ok(());
|
||||
}
|
||||
let mut target = File::create(target_path)?;
|
||||
io::copy(&mut prog_bar.wrap_read(resp), &mut target).unwrap();
|
||||
prog_bar.finish();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn download(opts: DownloadOpts) -> std::io::Result<()> {
|
||||
let mut threads = Vec::new();
|
||||
let m = MultiProgress::new();
|
||||
{
|
||||
let opts = opts.clone();
|
||||
let pb = m.add(ProgressBar::new(0));
|
||||
threads.push(std::thread::spawn(move || {
|
||||
fetch_url(&opts.bodies_url, &pb).unwrap();
|
||||
}));
|
||||
};
|
||||
{
|
||||
let opts = opts.clone();
|
||||
let pb = m.add(ProgressBar::new(0));
|
||||
threads.push(std::thread::spawn(move || {
|
||||
fetch_url(&opts.systems_url, &pb).unwrap();
|
||||
}));
|
||||
}
|
||||
m.join_and_clear().unwrap();
|
||||
for th in threads {
|
||||
th.join().unwrap();
|
||||
}
|
||||
Ok(())
|
||||
}
|
135
rust/src/lib.rs
135
rust/src/lib.rs
|
@ -1,56 +1,120 @@
|
|||
mod common;
|
||||
mod download;
|
||||
mod preprocess;
|
||||
mod route;
|
||||
use std::collections::HashMap;
|
||||
use pyo3::prelude::*;
|
||||
use pyo3::types::PyDict;
|
||||
use pyo3::types::{PyDict,PyList};
|
||||
use pyo3::exceptions::*;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[pymodule]
|
||||
pub fn _ed_lrr(py: Python, m: &PyModule) -> PyResult<()> {
|
||||
/// Test
|
||||
#[pyfn(m,"test")]
|
||||
fn test(py:Python,o:PyObject) -> PyResult<()> {
|
||||
println!("OBJ: {:?}",o);
|
||||
return Ok(());
|
||||
}
|
||||
pub fn _ed_lrr(_py: Python, m: &PyModule) -> PyResult<()> {
|
||||
|
||||
/// download(systems_url,systems_file, bodies_url, bodies_file, callback)
|
||||
/// --
|
||||
///
|
||||
/// Download bodies.json and systemsWithCoordinates.json
|
||||
#[pyfn(m, "download")]
|
||||
fn download(py: Python, systems_url: String, systems_file: String, bodies_url:String,bodies_file:String,callback: PyObject) -> PyResult<PyObject> {
|
||||
let state = PyDict::new(py);
|
||||
state.set_item("systems_url", systems_url)?;
|
||||
state.set_item("systems_file", systems_file)?;
|
||||
state.set_item("bodies_url", bodies_url)?;
|
||||
state.set_item("bodies_file", bodies_file)?;
|
||||
state.set_item("callback", callback)?;
|
||||
return Ok(state.to_object(py));
|
||||
}
|
||||
|
||||
/// preprocess(infile_systems, infile_bodies, outfile, callback)
|
||||
/// --
|
||||
/// --
|
||||
///
|
||||
/// Preprocess bodies.json and systemsWithCoordinates.json into stars.csv
|
||||
#[pyfn(m, "preprocess")]
|
||||
fn preprocess(py: Python, infile_systems: String, infile_bodies: String, outfile:String,callback: PyObject) -> PyResult<PyObject> {
|
||||
fn ed_lrr_preprocess(
|
||||
py: Python<'static>,
|
||||
infile_systems: String,
|
||||
infile_bodies: String,
|
||||
outfile: String,
|
||||
callback: PyObject,
|
||||
) -> PyResult<PyObject> {
|
||||
use preprocess::*;
|
||||
let state = PyDict::new(py);
|
||||
state.set_item("infile_systems", infile_systems)?;
|
||||
state.set_item("infile_bodies", infile_bodies)?;
|
||||
state.set_item("outfile", outfile)?;
|
||||
state.set_item("callback", callback)?;
|
||||
let state_dict = PyDict::new(py);
|
||||
callback.call(py,(state_dict,),None).unwrap();
|
||||
let callback_wrapped = move |state: &PreprocessState| {
|
||||
// println!("SEND: {:?}",state);
|
||||
state_dict.set_item("file",state.file.clone())?;
|
||||
state_dict.set_item("total",state.total)?;
|
||||
state_dict.set_item("done",state.done)?;
|
||||
callback.call(py,(state_dict,),None)
|
||||
};
|
||||
preprocess_files(&PathBuf::from(infile_bodies), &PathBuf::from(infile_systems), &PathBuf::from(outfile), Box::new(callback_wrapped)).unwrap();
|
||||
return Ok(state.to_object(py));
|
||||
}
|
||||
|
||||
/// route(infile, source, dest, range, mode, greedyness, precomp, callback)
|
||||
/// route(infile, hops, range, mode,primary, greedyness, precomp, callback)
|
||||
/// --
|
||||
///
|
||||
/// Compute a Route using the suplied parameters
|
||||
#[pyfn(m, "route")]
|
||||
fn route(py: Python, infile: String, source: String, dest:String, range: f32, mode: String,greedyness: Option<f32>, precomp: Option<String>,callback: PyObject) -> PyResult<PyObject> {
|
||||
fn route(
|
||||
py: Python<'static>,
|
||||
hops: Vec<String>,
|
||||
range: f32,
|
||||
mode: String,
|
||||
primary: bool,
|
||||
permute: bool,
|
||||
greedyness: Option<f32>,
|
||||
precomp: Option<String>,
|
||||
path: String,
|
||||
callback: PyObject,
|
||||
) -> PyResult<PyObject> {
|
||||
use route::*;
|
||||
// TODO: Verify Parameters
|
||||
let mode = match Mode::parse(&mode) {
|
||||
Ok(val) => val,
|
||||
Err(e) => {
|
||||
return Err(PyErr::new::<ValueError,_>(e));
|
||||
}
|
||||
};
|
||||
let state_dict = PyDict::new(py);
|
||||
callback.call(py,(state_dict,),None).unwrap();
|
||||
let callback_wrapped = move |state: &SearchState| {
|
||||
println!("SEND: {:?}",state);
|
||||
state_dict.set_item("mode",state.mode.clone())?;
|
||||
state_dict.set_item("system",state.system.clone())?;
|
||||
state_dict.set_item("body",state.body.clone())?;
|
||||
state_dict.set_item("depth",state.depth)?;
|
||||
state_dict.set_item("queue_size",state.queue_size)?;
|
||||
state_dict.set_item("d_rem",state.d_rem)?;
|
||||
state_dict.set_item("d_total",state.d_total)?;
|
||||
state_dict.set_item("prc_done",state.prc_done)?;
|
||||
state_dict.set_item("n_seen",state.n_seen)?;
|
||||
state_dict.set_item("prc_seen",state.prc_seen)?;
|
||||
callback.call(py,(state_dict,),None)
|
||||
};
|
||||
let opts=RouteOpts{
|
||||
systems:hops,
|
||||
range:Some(range),
|
||||
file_path: PathBuf::from(path),
|
||||
precomp_file: precomp.map(PathBuf::from),
|
||||
callback: Box::new(callback_wrapped),
|
||||
mode,
|
||||
factor: greedyness,
|
||||
precompute: false,
|
||||
permute: permute,
|
||||
keep_first: true,
|
||||
keep_last: true,
|
||||
primary
|
||||
};
|
||||
let none=().to_object(py);
|
||||
match route(opts) {
|
||||
Ok(Some(route)) => {
|
||||
let hops=route.iter().map(|hop| {
|
||||
let pos=PyList::new(py,hop.pos.iter());
|
||||
let elem=PyDict::new(py);
|
||||
elem.set_item("star_type",hop.star_type.clone()).unwrap();
|
||||
elem.set_item("system",hop.system.clone()).unwrap();
|
||||
elem.set_item("body",hop.body.clone()).unwrap();
|
||||
elem.set_item("distance",hop.distance).unwrap();
|
||||
elem.set_item("pos",pos).unwrap();
|
||||
return elem;
|
||||
});
|
||||
let lst=PyList::new(py,hops);
|
||||
return Ok(lst.to_object(py));
|
||||
}
|
||||
Ok(None) => {
|
||||
return Ok(none);
|
||||
},
|
||||
Err(e) => {
|
||||
return Err(PyErr::new::<ValueError,_>(e));
|
||||
}
|
||||
};
|
||||
/*
|
||||
let state = PyDict::new(py);
|
||||
state.set_item("infile", infile)?;
|
||||
state.set_item("source", source)?;
|
||||
|
@ -60,7 +124,8 @@ pub fn _ed_lrr(py: Python, m: &PyModule) -> PyResult<()> {
|
|||
state.set_item("greedyness", greedyness)?;
|
||||
state.set_item("precomp", precomp)?;
|
||||
state.set_item("callback", callback)?;
|
||||
return Ok(state.to_object(py));
|
||||
return callback.call(py,(state.to_object(py),),None);
|
||||
*/
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,33 +1,14 @@
|
|||
use crate::common::SystemSerde;
|
||||
use fnv::FnvHashMap;
|
||||
use humantime::format_duration;
|
||||
use indicatif::{ProgressBar, ProgressStyle};
|
||||
use serde::Deserialize;
|
||||
use serde_json::Result;
|
||||
use std::fs::File;
|
||||
use std::io::Seek;
|
||||
use std::io::{BufRead, BufReader, BufWriter, SeekFrom};
|
||||
use std::path::PathBuf;
|
||||
use std::str;
|
||||
use std::time::Instant;
|
||||
use structopt::StructOpt;
|
||||
|
||||
#[derive(Debug, StructOpt)]
|
||||
pub struct PreprocessOpts {
|
||||
#[structopt(short, long = "bodies", default_value = "dumps/bodies.json")]
|
||||
/// Path to bodies.json
|
||||
pub bodies: PathBuf,
|
||||
#[structopt(
|
||||
short,
|
||||
long = "systems",
|
||||
default_value = "dumps/systemsWithCoordinates.json"
|
||||
)]
|
||||
/// Path to systemsWithCoordinates.json
|
||||
pub systems: PathBuf,
|
||||
#[structopt(default_value = "stars")]
|
||||
/// outfile prefix
|
||||
pub prefix: String,
|
||||
}
|
||||
use std::str;
|
||||
use pyo3::prelude::*;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[allow(non_snake_case)]
|
||||
|
@ -58,25 +39,15 @@ struct System {
|
|||
date: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, StructOpt)]
|
||||
#[structopt(
|
||||
name = "ed_lrr_pp",
|
||||
about = "Preprocessor for Elite: Dangerous Long-Range Router",
|
||||
rename_all = "snake_case"
|
||||
)]
|
||||
/// Preprocess data for ed_lrr
|
||||
struct Opt {
|
||||
#[structopt(short, long = "bodies")]
|
||||
/// Path to bodies.json
|
||||
bodies: PathBuf,
|
||||
#[structopt(short, long = "systems")]
|
||||
/// Path to systemsWithCoordinates.json
|
||||
systems: PathBuf,
|
||||
#[structopt(default_value = "stars")]
|
||||
/// outfile prefix
|
||||
prefix: String,
|
||||
#[derive(Debug)]
|
||||
pub struct PreprocessState {
|
||||
pub file: String,
|
||||
pub total: u64,
|
||||
pub done: u64,
|
||||
pub count: usize,
|
||||
}
|
||||
|
||||
|
||||
fn get_mult(star_type: &str) -> f32 {
|
||||
if star_type.contains("White Dwarf") {
|
||||
return 1.5;
|
||||
|
@ -87,22 +58,18 @@ fn get_mult(star_type: &str) -> f32 {
|
|||
1.0
|
||||
}
|
||||
|
||||
fn process(path: &PathBuf, func: &mut dyn for<'r> FnMut(&'r str) -> ()) -> std::io::Result<()> {
|
||||
let mut cnt = 0;
|
||||
fn process(path: &PathBuf, func: &mut dyn for<'r> FnMut(&'r str) -> (),callback: &Box<dyn Fn(&PreprocessState) -> PyResult<PyObject>>) -> std::io::Result<()> {
|
||||
let mut buffer = String::new();
|
||||
let t_start = Instant::now();
|
||||
let fh = File::open(path)?;
|
||||
let prog_bar = ProgressBar::new(fh.metadata()?.len());
|
||||
prog_bar.set_style(
|
||||
ProgressStyle::default_bar()
|
||||
.template(
|
||||
"[{elapsed_precise}/{eta_precise}]{spinner} [{wide_bar}] {binary_bytes}/{binary_total_bytes} ({percent}%)",
|
||||
)
|
||||
.progress_chars("#9876543210 ")
|
||||
.tick_chars("/-\\|"),
|
||||
);
|
||||
prog_bar.set_draw_delta(1024 * 1024);
|
||||
let total_size= fh.metadata()?.len();
|
||||
let mut t_last=Instant::now();
|
||||
let mut reader = BufReader::new(fh);
|
||||
let mut state=PreprocessState {
|
||||
file: path.to_str().unwrap().to_owned(),
|
||||
total: total_size,
|
||||
done: 0,
|
||||
count: 0
|
||||
};
|
||||
println!("Loading {} ...", path.to_str().unwrap());
|
||||
while let Ok(n) = reader.read_line(&mut buffer) {
|
||||
if n == 0 {
|
||||
|
@ -112,20 +79,19 @@ fn process(path: &PathBuf, func: &mut dyn for<'r> FnMut(&'r str) -> ()) -> std::
|
|||
if !buffer.is_empty() {
|
||||
func(&buffer);
|
||||
}
|
||||
prog_bar.set_position(reader.seek(SeekFrom::Current(0)).unwrap());
|
||||
cnt += 1;
|
||||
let pos=reader.seek(SeekFrom::Current(0)).unwrap();
|
||||
state.done=pos;
|
||||
state.count += 1;
|
||||
if (t_last.elapsed().as_millis()>100) {
|
||||
callback(&state)?;
|
||||
t_last=Instant::now();
|
||||
}
|
||||
buffer.clear();
|
||||
}
|
||||
prog_bar.finish_and_clear();
|
||||
println!(
|
||||
"Processed {} lines in {} ...",
|
||||
cnt,
|
||||
format_duration(t_start.elapsed())
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn process_systems(path: &PathBuf) -> FnvHashMap<i32, System> {
|
||||
fn process_systems(path: &PathBuf,callback: &Box<dyn Fn(&PreprocessState) -> PyResult<PyObject>> ) -> FnvHashMap<i32, System> {
|
||||
let mut ret = FnvHashMap::default();
|
||||
process(path, &mut |line| {
|
||||
let sys_res: Result<System> = serde_json::from_str(&line);
|
||||
|
@ -134,7 +100,7 @@ fn process_systems(path: &PathBuf) -> FnvHashMap<i32, System> {
|
|||
} else {
|
||||
eprintln!("\nError parsing: {}\n\t{:?}\n", line, sys_res.unwrap_err());
|
||||
}
|
||||
})
|
||||
},callback)
|
||||
.unwrap();
|
||||
ret
|
||||
}
|
||||
|
@ -155,10 +121,10 @@ fn build_index(path: &PathBuf) -> std::io::Result<()> {
|
|||
|
||||
fn process_bodies(
|
||||
path: &PathBuf,
|
||||
out_prefix: &str,
|
||||
out_path: &PathBuf,
|
||||
systems: &mut FnvHashMap<i32, System>,
|
||||
callback: &Box<dyn Fn(&PreprocessState) -> PyResult<PyObject>>,
|
||||
) -> std::io::Result<()> {
|
||||
let out_path = PathBuf::from(format!("{}.csv", out_prefix));
|
||||
println!(
|
||||
"Processing {} into {} ...",
|
||||
path.to_str().unwrap(),
|
||||
|
@ -196,18 +162,17 @@ fn process_bodies(
|
|||
} else {
|
||||
eprintln!("\nError parsing: {}\n\t{:?}\n", line, body_res.unwrap_err());
|
||||
}
|
||||
})
|
||||
},callback)
|
||||
.unwrap();
|
||||
println!("Total Systems: {}", n);
|
||||
systems.clear();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn preprocess_files(opts: PreprocessOpts) -> std::io::Result<()> {
|
||||
let out_path = PathBuf::from(format!("{}.csv", &opts.prefix));
|
||||
pub fn preprocess_files(bodies: &PathBuf,systems:&PathBuf,out_path:&PathBuf,callback: Box<dyn Fn(&PreprocessState) -> PyResult<PyObject>>) -> std::io::Result<()> {
|
||||
if !out_path.exists() {
|
||||
let mut systems = process_systems(&opts.systems);
|
||||
process_bodies(&opts.bodies, &opts.prefix, &mut systems)?;
|
||||
let mut systems = process_systems(systems,&callback);
|
||||
process_bodies(bodies, out_path, &mut systems,&callback)?;
|
||||
} else {
|
||||
println!(
|
||||
"File '{}' exists, not overwriting it",
|
||||
|
|
|
@ -11,25 +11,25 @@ use std::hash::{Hash, Hasher};
|
|||
use std::io::Seek;
|
||||
use std::io::{BufRead, BufReader, BufWriter, Write};
|
||||
use std::path::PathBuf;
|
||||
use std::str::FromStr;
|
||||
use std::time::Instant;
|
||||
use structopt::StructOpt;
|
||||
|
||||
use pyo3::prelude::*;
|
||||
use crate::common::{System, SystemSerde};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct SearchState {
|
||||
pub mode: String,
|
||||
pub system: String,
|
||||
pub body: String,
|
||||
pub depth: usize,
|
||||
pub queue_size: usize,
|
||||
pub d_rem: f64,
|
||||
pub prc_done: f64,
|
||||
pub d_rem: f32,
|
||||
pub d_total: f32,
|
||||
pub prc_done: f32,
|
||||
pub n_seen: usize,
|
||||
pub prc_seen: f64
|
||||
pub prc_seen: f32
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct RouteOpts {
|
||||
pub range: Option<f32>,
|
||||
pub file_path: PathBuf,
|
||||
|
@ -37,10 +37,12 @@ pub struct RouteOpts {
|
|||
pub precompute: bool,
|
||||
pub permute: bool,
|
||||
pub primary: bool,
|
||||
pub full_permute: bool,
|
||||
pub keep_first: bool,
|
||||
pub keep_last: bool,
|
||||
pub factor: Option<f32>,
|
||||
pub mode: Mode,
|
||||
pub systems: Vec<String>,
|
||||
pub callback: Box<dyn Fn(&SearchState) -> PyResult<PyObject>>
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
@ -50,10 +52,10 @@ pub enum Mode {
|
|||
AStar,
|
||||
}
|
||||
|
||||
impl FromStr for Mode {
|
||||
type Err = String;
|
||||
fn from_str(s: &str) -> Result<Mode, String> {
|
||||
match s {
|
||||
impl Mode {
|
||||
pub fn parse(s: &str) -> Result<Mode, String> {
|
||||
let s=s.to_lowercase();
|
||||
match s.as_ref() {
|
||||
"bfs" => Ok(Mode::BFS),
|
||||
"greedy" => Ok(Mode::Greedy),
|
||||
"astar" => Ok(Mode::AStar),
|
||||
|
@ -138,8 +140,6 @@ struct LineCache {
|
|||
impl LineCache {
|
||||
pub fn new(path: &PathBuf) -> Option<Self> {
|
||||
let idx_path = path.with_extension("idx");
|
||||
let t_load = Instant::now();
|
||||
println!("Loading Index from {}", idx_path.to_str().unwrap());
|
||||
let cache =
|
||||
bincode::deserialize_from(&mut BufReader::new(File::open(idx_path).ok()?)).ok()?;
|
||||
let mut reader = BufReader::new(File::open(path).ok()?);
|
||||
|
@ -149,7 +149,6 @@ impl LineCache {
|
|||
cache,
|
||||
header,
|
||||
};
|
||||
println!("Done in {}!", format_duration(t_load.elapsed()));
|
||||
Some(ret)
|
||||
}
|
||||
fn read_record(reader: &mut BufReader<File>) -> Option<StringRecord> {
|
||||
|
@ -176,19 +175,18 @@ pub struct Router {
|
|||
range: f32,
|
||||
primary_only: bool,
|
||||
path: PathBuf,
|
||||
callback: Box<Fn(SearchState) -> ()>,
|
||||
callback: Box<dyn Fn(&SearchState) -> PyResult<PyObject>>
|
||||
}
|
||||
|
||||
impl Router {
|
||||
pub fn new(path: &PathBuf, range: f32, primary_only: bool,callback: Box<Fn(SearchState) -> ()>) -> Self {
|
||||
pub fn new(path: &PathBuf, range: f32, primary_only: bool,callback: Box<dyn Fn(&SearchState) -> PyResult<PyObject>>) -> Result<Self,String> {
|
||||
let mut scoopable = FnvHashSet::default();
|
||||
let mut reader = csv::ReaderBuilder::new()
|
||||
.from_path(path)
|
||||
.unwrap_or_else(|e| {
|
||||
println!("Error opening {}: {}", path.to_str().unwrap(), e);
|
||||
std::process::exit(1);
|
||||
});
|
||||
let t_load = Instant::now();
|
||||
let mut reader = match csv::ReaderBuilder::new().from_path(path) {
|
||||
Ok(rdr) => rdr,
|
||||
Err(e) => {
|
||||
return Err(format!("Error opening {}: {}", path.to_str().unwrap(), e).to_string());
|
||||
}
|
||||
};
|
||||
println!("Loading {}...", path.to_str().unwrap());
|
||||
let systems: Vec<System> = reader
|
||||
.deserialize::<SystemSerde>()
|
||||
|
@ -214,7 +212,6 @@ impl Router {
|
|||
sys.build()
|
||||
})
|
||||
.collect();
|
||||
println!("Building RTree...");
|
||||
let ret = Self {
|
||||
tree: RTree::bulk_load(systems),
|
||||
scoopable,
|
||||
|
@ -225,18 +222,11 @@ impl Router {
|
|||
path: path.clone(),
|
||||
callback: callback,
|
||||
};
|
||||
println!(
|
||||
"{} Systems loaded in {}",
|
||||
ret.tree.size(),
|
||||
format_duration(t_load.elapsed())
|
||||
);
|
||||
ret
|
||||
Ok(ret)
|
||||
}
|
||||
|
||||
pub fn from_file(filename: &PathBuf) -> (PathBuf, Self) {
|
||||
let t_load = Instant::now();
|
||||
pub fn from_file(filename: &PathBuf,callback: Box<dyn Fn(&SearchState) -> PyResult<PyObject>>) -> Result<(PathBuf, Self),String> {
|
||||
let mut reader = BufReader::new(File::open(&filename).unwrap());
|
||||
println!("Loading {}", filename.to_str().unwrap());
|
||||
let (primary, range, file_hash, path, route_tree): (
|
||||
bool,
|
||||
f32,
|
||||
|
@ -245,12 +235,11 @@ impl Router {
|
|||
FnvHashMap<u32, u32>,
|
||||
) = bincode::deserialize_from(&mut reader).unwrap();
|
||||
let path = PathBuf::from(path);
|
||||
println!("Done in {}!", format_duration(t_load.elapsed()));
|
||||
if hash_file(&path) != file_hash {
|
||||
panic!("File hash mismatch!")
|
||||
return Err("File hash mismatch!".to_string());
|
||||
}
|
||||
let cache = LineCache::new(&path);
|
||||
(
|
||||
Ok((
|
||||
path.clone(),
|
||||
Self {
|
||||
tree: RTree::default(),
|
||||
|
@ -260,9 +249,9 @@ impl Router {
|
|||
cache,
|
||||
primary_only: primary,
|
||||
path,
|
||||
callback: Box::new(|s| {}),
|
||||
callback
|
||||
},
|
||||
)
|
||||
))
|
||||
}
|
||||
|
||||
fn closest(&self, point: &[f32; 3]) -> &System {
|
||||
|
@ -284,34 +273,34 @@ impl Router {
|
|||
&self,
|
||||
waypoints: &[String],
|
||||
range: f32,
|
||||
full: bool,
|
||||
keep: (bool,bool),
|
||||
mode: Mode,
|
||||
factor: f32,
|
||||
) -> Vec<System> {
|
||||
) -> Result<Vec<System>,String> {
|
||||
let mut best_score: f32 = std::f32::MAX;
|
||||
let mut waypoints = waypoints.to_owned();
|
||||
let mut best_permutation_waypoints = waypoints.to_owned();
|
||||
let first = waypoints.first().cloned();
|
||||
let last = waypoints.last().cloned();
|
||||
let t_start = Instant::now();
|
||||
println!("Finding best permutation of hops...");
|
||||
while waypoints.prev_permutation() {}
|
||||
loop {
|
||||
let c_first = waypoints.first().cloned();
|
||||
let c_last = waypoints.last().cloned();
|
||||
if full || ((c_first == first) && (c_last == last)) {
|
||||
let valid = (keep.0 && (c_first == first)) && (keep.1 && (c_last==last));
|
||||
if valid {
|
||||
let mut total_d = 0.0;
|
||||
for pair in waypoints.windows(2) {
|
||||
match pair {
|
||||
[src, dst] => {
|
||||
let (mut src, dst) =
|
||||
(self.name_to_systems(&src), self.name_to_systems(&dst));
|
||||
(self.name_to_systems(&src)?, self.name_to_systems(&dst)?);
|
||||
src.sort_by_key(|&p| (p.mult * 10.0) as u8);
|
||||
let src = src.last().unwrap();
|
||||
let dst = dst.last().unwrap();
|
||||
total_d += src.distp2(dst);
|
||||
}
|
||||
_ => panic!("Invalid routing parameters!"),
|
||||
_ => return Err("Invalid routing parameters!".to_string()),
|
||||
}
|
||||
}
|
||||
if total_d < best_score {
|
||||
|
@ -323,8 +312,6 @@ impl Router {
|
|||
break;
|
||||
}
|
||||
}
|
||||
|
||||
println!("Done in {}!", format_duration(t_start.elapsed()));
|
||||
println!("Best permutation: {:?}", best_permutation_waypoints);
|
||||
self.name_multiroute(&best_permutation_waypoints, range, mode, factor)
|
||||
}
|
||||
|
@ -335,10 +322,10 @@ impl Router {
|
|||
range: f32,
|
||||
mode: Mode,
|
||||
factor: f32,
|
||||
) -> Vec<System> {
|
||||
) -> Result<Vec<System>,String> {
|
||||
let mut coords = Vec::new();
|
||||
for p_name in waypoints {
|
||||
let mut p_l = self.name_to_systems(p_name);
|
||||
let mut p_l = self.name_to_systems(p_name)?;
|
||||
p_l.sort_by_key(|&p| (p.mult * 10.0) as u8);
|
||||
let p = p_l.last().unwrap();
|
||||
coords.push((p_name, p.pos));
|
||||
|
@ -351,15 +338,15 @@ impl Router {
|
|||
range: f32,
|
||||
mode: Mode,
|
||||
factor: f32,
|
||||
) -> Vec<System> {
|
||||
) -> Result<Vec<System>,String> {
|
||||
let mut route: Vec<System> = Vec::new();
|
||||
for pair in waypoints.windows(2) {
|
||||
match *pair {
|
||||
[src, dst] => {
|
||||
let block = match mode {
|
||||
Mode::BFS => self.route_bfs(&src, &dst, range),
|
||||
Mode::Greedy => self.route_greedy(&src, &dst, range),
|
||||
Mode::AStar => self.route_astar(&src, &dst, range, factor),
|
||||
Mode::BFS => self.route_bfs(&src, &dst, range)?,
|
||||
Mode::Greedy => self.route_greedy(&src, &dst, range)?,
|
||||
Mode::AStar => self.route_astar(&src, &dst, range, factor)?,
|
||||
};
|
||||
if route.is_empty() {
|
||||
for sys in block.iter() {
|
||||
|
@ -371,20 +358,19 @@ impl Router {
|
|||
}
|
||||
}
|
||||
}
|
||||
_ => panic!("Invalid routing parameters!"),
|
||||
_ => return Err("Invalid routing parameters!".to_string()),
|
||||
}
|
||||
}
|
||||
route
|
||||
Ok(route)
|
||||
}
|
||||
|
||||
fn name_to_systems(&self, name: &str) -> Vec<&System> {
|
||||
fn name_to_systems(&self, name: &str) -> Result<Vec<&System>,String> {
|
||||
for sys in &self.tree {
|
||||
if sys.system == name {
|
||||
return self.neighbours(&sys, 0.0).collect();
|
||||
return Ok(self.neighbours(&sys, 0.0).collect());
|
||||
}
|
||||
}
|
||||
eprintln!("System not found: \"{}\"", name);
|
||||
std::process::exit(1);
|
||||
Err(format!("System not found: \"{}\"", name))
|
||||
}
|
||||
|
||||
pub fn route_astar(
|
||||
|
@ -393,29 +379,33 @@ impl Router {
|
|||
dst: &(&String, [f32; 3]),
|
||||
range: f32,
|
||||
factor: f32,
|
||||
) -> Vec<System> {
|
||||
) -> Result<Vec<System>,String> {
|
||||
if factor == 0.0 {
|
||||
return self.route_bfs(src, dst, range);
|
||||
}
|
||||
println!("Running A-Star with greedy factor of {}", factor);
|
||||
let (src_name, src) = src;
|
||||
let (dst_name, dst) = dst;
|
||||
let start_sys = self.closest(src);
|
||||
let goal_sys = self.closest(dst);
|
||||
{
|
||||
let d = dist(src, dst);
|
||||
println!("Plotting route from {} to {}...", src_name, dst_name);
|
||||
println!(
|
||||
"Jump Range: {} Ly, Distance: {} Ly, Theoretical Jumps: {}",
|
||||
range,
|
||||
d,
|
||||
d / range
|
||||
);
|
||||
}
|
||||
let d_total = dist(&start_sys.pos,&goal_sys.pos);
|
||||
let mut d_rem=d_total;
|
||||
|
||||
let mut state=SearchState {
|
||||
mode:"A-Star".into(),
|
||||
depth:0,
|
||||
queue_size:0,
|
||||
d_rem: d_total,
|
||||
d_total: d_total,
|
||||
prc_done: 0.0,
|
||||
n_seen:0,
|
||||
prc_seen: 0.0,
|
||||
body: start_sys.body.clone(),
|
||||
system: start_sys.system.clone()
|
||||
};
|
||||
let total = self.tree.size() as f32;
|
||||
let t_start = Instant::now();
|
||||
let mut prev = FnvHashMap::default();
|
||||
let mut seen = FnvHashSet::default();
|
||||
let t_start = Instant::now();
|
||||
let mut found = false;
|
||||
let mut maxd = 0;
|
||||
let mut queue: Vec<(usize, usize, &System)> = Vec::new();
|
||||
|
@ -425,11 +415,24 @@ impl Router {
|
|||
&start_sys,
|
||||
));
|
||||
seen.insert(start_sys.id);
|
||||
|
||||
while !(queue.is_empty() || found) {
|
||||
while let Some((depth, _, sys)) = queue.pop() {
|
||||
if depth > maxd {
|
||||
maxd = depth;
|
||||
if (depth+1) > maxd {
|
||||
maxd = depth+1;
|
||||
state.depth=depth;
|
||||
state.queue_size=queue.len();
|
||||
state.prc_done=((d_total-d_rem)*100f32) / d_total;
|
||||
state.d_rem=d_rem;
|
||||
state.n_seen=seen.len();
|
||||
state.prc_seen=((seen.len()*100) as f32) / total;
|
||||
state.body=sys.body.clone();
|
||||
state.system=sys.system.clone();
|
||||
match (self.callback)(&state) {
|
||||
Ok(_) => (),
|
||||
Err(e) => {
|
||||
return Err(format!("{:?}",e).to_string());
|
||||
}
|
||||
};
|
||||
print!(
|
||||
"[{}] Depth: {}, Queue: {}, Seen: {} ({:.02}%) \r",
|
||||
format_duration(t_start.elapsed()),
|
||||
|
@ -451,6 +454,10 @@ impl Router {
|
|||
.map(|nb| {
|
||||
prev.insert(nb.id, sys);
|
||||
let d_g = (nb.distp(goal_sys) / range) as usize;
|
||||
let dist = dist(&nb.pos, &goal_sys.pos);
|
||||
if dist < d_rem {
|
||||
d_rem = dist;
|
||||
}
|
||||
(depth + 1, d_g, nb)
|
||||
}),
|
||||
);
|
||||
|
@ -468,8 +475,7 @@ impl Router {
|
|||
|
||||
println!();
|
||||
if !found {
|
||||
eprintln!("No route from {} to {} found!", src_name, dst_name);
|
||||
return Vec::new();
|
||||
return Err(format!("No route from {} to {} found!", src_name, dst_name).to_string());
|
||||
}
|
||||
let mut v: Vec<System> = Vec::new();
|
||||
let mut curr_sys = goal_sys;
|
||||
|
@ -483,7 +489,7 @@ impl Router {
|
|||
}
|
||||
}
|
||||
v.reverse();
|
||||
v
|
||||
Ok(v)
|
||||
}
|
||||
|
||||
pub fn route_greedy(
|
||||
|
@ -491,26 +497,28 @@ impl Router {
|
|||
src: &(&String, [f32; 3]),
|
||||
dst: &(&String, [f32; 3]),
|
||||
range: f32,
|
||||
) -> Vec<System> {
|
||||
println!("Running Greedy-Search");
|
||||
) -> Result<Vec<System>,String> {
|
||||
let (src_name, src) = src;
|
||||
let (dst_name, dst) = dst;
|
||||
let start_sys = self.closest(src);
|
||||
let goal_sys = self.closest(dst);
|
||||
{
|
||||
let d = dist(src, dst);
|
||||
println!("Plotting route from {} to {}...", src_name, dst_name);
|
||||
println!(
|
||||
"Jump Range: {} Ly, Distance: {} Ly, Theoretical Jumps: {}",
|
||||
range,
|
||||
d,
|
||||
d / range
|
||||
);
|
||||
}
|
||||
let d_total = dist(&start_sys.pos, &goal_sys.pos);
|
||||
let mut d_rem=d_total;
|
||||
let mut state=SearchState {
|
||||
mode:"Greedy".into(),
|
||||
depth:0,
|
||||
queue_size:0,
|
||||
d_rem: d_total,
|
||||
d_total: d_total,
|
||||
prc_done: 0.0,
|
||||
n_seen:0,
|
||||
prc_seen: 0.0,
|
||||
body:start_sys.body.clone(),
|
||||
system:start_sys.system.clone(),
|
||||
};
|
||||
let total = self.tree.size() as f32;
|
||||
let mut prev = FnvHashMap::default();
|
||||
let mut seen = FnvHashSet::default();
|
||||
let t_start = Instant::now();
|
||||
let mut found = false;
|
||||
let mut maxd = 0;
|
||||
let mut queue: Vec<(f32, f32, usize, &System)> = Vec::new();
|
||||
|
@ -518,17 +526,22 @@ impl Router {
|
|||
seen.insert(start_sys.id);
|
||||
while !(queue.is_empty() || found) {
|
||||
while let Some((_, _, depth, sys)) = queue.pop() {
|
||||
if depth > maxd {
|
||||
maxd = depth;
|
||||
print!(
|
||||
"[{}] Depth: {}, Queue: {}, Seen: {} ({:.02}%) \r",
|
||||
format_duration(t_start.elapsed()),
|
||||
depth,
|
||||
queue.len(),
|
||||
seen.len(),
|
||||
((seen.len() * 100) as f32) / total
|
||||
);
|
||||
std::io::stdout().flush().unwrap();
|
||||
if (depth+1) > maxd {
|
||||
state.depth=depth;
|
||||
state.queue_size=queue.len();
|
||||
state.prc_done=((d_total-d_rem)*100f32) / d_total;
|
||||
state.d_rem=d_rem;
|
||||
state.n_seen=seen.len();
|
||||
state.prc_seen=((seen.len()*100) as f32) / total;
|
||||
state.body=sys.body.clone();
|
||||
state.system=sys.system.clone();
|
||||
match (self.callback)(&state) {
|
||||
Ok(_) => (),
|
||||
Err(e) => {
|
||||
return Err(format!("{:?}",e).to_string());
|
||||
}
|
||||
};
|
||||
maxd = depth+1;
|
||||
}
|
||||
if sys.id == goal_sys.id {
|
||||
found = true;
|
||||
|
@ -540,6 +553,10 @@ impl Router {
|
|||
.filter(|&nb| seen.insert(nb.id))
|
||||
.map(|nb| {
|
||||
prev.insert(nb.id, sys);
|
||||
let dist = dist(&nb.pos, &goal_sys.pos);
|
||||
if dist < d_rem {
|
||||
d_rem = dist;
|
||||
}
|
||||
(-nb.mult, nb.distp2(goal_sys), depth + 1, nb)
|
||||
}),
|
||||
);
|
||||
|
@ -547,11 +564,8 @@ impl Router {
|
|||
queue.reverse();
|
||||
}
|
||||
}
|
||||
println!();
|
||||
println!();
|
||||
if !found {
|
||||
eprintln!("No route from {} to {} found!", src_name, dst_name);
|
||||
return Vec::new();
|
||||
return Err(format!("No route from {} to {} found!", src_name, dst_name).to_string());
|
||||
}
|
||||
let mut v: Vec<System> = Vec::new();
|
||||
let mut curr_sys = goal_sys;
|
||||
|
@ -565,18 +579,17 @@ impl Router {
|
|||
}
|
||||
}
|
||||
v.reverse();
|
||||
v
|
||||
Ok(v)
|
||||
}
|
||||
|
||||
pub fn precompute(&mut self, src: &str) {
|
||||
let mut sys_l = self.name_to_systems(src);
|
||||
pub fn precompute(&mut self, src: &str) -> Result<(),String> {
|
||||
let mut sys_l = self.name_to_systems(src)?;
|
||||
sys_l.sort_by_key(|&sys| (sys.mult * 10.0) as u8);
|
||||
let sys = sys_l.last().unwrap();
|
||||
println!("Precomputing routes starting at {} ...", sys.system);
|
||||
let total = self.tree.size() as f32;
|
||||
let t_start = Instant::now();
|
||||
let mut prev = FnvHashMap::default();
|
||||
let mut seen = FnvHashSet::default();
|
||||
let t_start = Instant::now();
|
||||
let mut depth = 0;
|
||||
let mut queue: VecDeque<(usize, &System)> = VecDeque::new();
|
||||
let mut queue_next: VecDeque<(usize, &System)> = VecDeque::new();
|
||||
|
@ -613,9 +626,7 @@ impl Router {
|
|||
self.range,
|
||||
if self.primary_only { "_primary" } else { "" }
|
||||
);
|
||||
println!("\nSaving to {}", ofn);
|
||||
let mut out_fh = BufWriter::new(File::create(&ofn).unwrap());
|
||||
// (range, path, route_tree)
|
||||
let data = (
|
||||
self.primary_only,
|
||||
self.range,
|
||||
|
@ -623,11 +634,13 @@ impl Router {
|
|||
String::from(self.path.to_str().unwrap()),
|
||||
self.route_tree.as_ref().unwrap(),
|
||||
);
|
||||
bincode::serialize_into(&mut out_fh, &data).unwrap();
|
||||
return match bincode::serialize_into(&mut out_fh, &data) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(e) => Err(format!("Error: {}",e).to_string())
|
||||
};
|
||||
}
|
||||
|
||||
fn get_systems_by_ids(&mut self, path: &PathBuf, ids: &[u32]) -> FnvHashMap<u32, System> {
|
||||
println!("Processing {}", path.to_str().unwrap());
|
||||
fn get_systems_by_ids(&mut self, path: &PathBuf, ids: &[u32]) -> Result<FnvHashMap<u32, System>,String> {
|
||||
let mut ret = FnvHashMap::default();
|
||||
if let Some(c) = &mut self.cache.as_mut() {
|
||||
let mut missing = false;
|
||||
|
@ -637,22 +650,22 @@ impl Router {
|
|||
ret.insert(*id, sys);
|
||||
}
|
||||
None => {
|
||||
println!("ID {} not found in cache", id);
|
||||
missing = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if !missing {
|
||||
return ret;
|
||||
return Ok(ret);
|
||||
}
|
||||
}
|
||||
let mut reader = csv::ReaderBuilder::new()
|
||||
.from_path(path)
|
||||
.unwrap_or_else(|e| {
|
||||
println!("Error opening {}: {}", path.to_str().unwrap(), e);
|
||||
std::process::exit(1);
|
||||
});
|
||||
let mut reader = match csv::ReaderBuilder::new()
|
||||
.from_path(path) {
|
||||
Ok(reader) => reader,
|
||||
Err(e) => {
|
||||
return Err(format!("Error opening {}: {}", path.to_str().unwrap(), e));
|
||||
}
|
||||
};
|
||||
reader
|
||||
.deserialize::<SystemSerde>()
|
||||
.map(|res| res.unwrap())
|
||||
|
@ -660,15 +673,12 @@ impl Router {
|
|||
.map(|sys| {
|
||||
ret.insert(sys.id, sys.build());
|
||||
})
|
||||
.last()
|
||||
.unwrap_or_else(|| {
|
||||
eprintln!("Error: No systems matching {:?} found!", ids);
|
||||
std::process::exit(1);
|
||||
});
|
||||
ret
|
||||
.count();
|
||||
Ok(ret)
|
||||
}
|
||||
|
||||
fn get_system_by_name(path: &PathBuf, name: &str) -> System {
|
||||
fn get_system_by_name(path: &PathBuf, name: &str) -> Result<System,String> {
|
||||
// TODO: Fuzzy search (https://github.com/andylokandy/simsearch-rs)
|
||||
let mut reader = csv::ReaderBuilder::new()
|
||||
.from_path(path)
|
||||
.unwrap_or_else(|e| {
|
||||
|
@ -678,20 +688,18 @@ impl Router {
|
|||
let sys = reader
|
||||
.deserialize::<SystemSerde>()
|
||||
.map(|res| res.unwrap())
|
||||
.find(|sys| sys.system == name)
|
||||
.unwrap_or_else(|| {
|
||||
eprintln!("Error: System '{}' not found!", name);
|
||||
std::process::exit(1);
|
||||
});
|
||||
sys.build()
|
||||
.find(|sys| sys.system == name);
|
||||
match sys {
|
||||
Some(system) => Ok(system.build()),
|
||||
None => Err(format!("System {} not found!",name).to_string())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn route_to(&mut self, dst: &str, systems_path: &PathBuf) -> Vec<System> {
|
||||
pub fn route_to(&mut self, dst: &str, systems_path: &PathBuf) -> Result<Vec<System>,String> {
|
||||
let prev = self.route_tree.as_ref().unwrap();
|
||||
let dst = Self::get_system_by_name(&systems_path, dst);
|
||||
let dst = Self::get_system_by_name(&systems_path, dst)?;
|
||||
if !prev.contains_key(&dst.id) {
|
||||
eprintln!("System-ID {} not found", dst.id);
|
||||
std::process::exit(1);
|
||||
return Err(format!("System-ID {} not found", dst.id).to_string());
|
||||
};
|
||||
let mut v_ids: Vec<u32> = Vec::new();
|
||||
let mut v: Vec<System> = Vec::new();
|
||||
|
@ -706,12 +714,17 @@ impl Router {
|
|||
}
|
||||
}
|
||||
v_ids.reverse();
|
||||
let id_map = self.get_systems_by_ids(&systems_path, &v_ids);
|
||||
let id_map = self.get_systems_by_ids(&systems_path, &v_ids)?;
|
||||
for sys_id in v_ids {
|
||||
let sys = id_map.get(&sys_id).unwrap();
|
||||
let sys = match id_map.get(&sys_id) {
|
||||
Some(sys) => sys,
|
||||
None => {
|
||||
return Err(format!("System-ID {} not found!",sys_id));
|
||||
}
|
||||
};
|
||||
v.push(sys.clone())
|
||||
}
|
||||
v
|
||||
Ok(v)
|
||||
}
|
||||
|
||||
pub fn route_bfs(
|
||||
|
@ -719,13 +732,25 @@ impl Router {
|
|||
src: &(&String, [f32; 3]),
|
||||
dst: &(&String, [f32; 3]),
|
||||
range: f32,
|
||||
) -> Vec<System> {
|
||||
) -> Result<Vec<System>,String> {
|
||||
println!("Running BFS");
|
||||
let (src_name, src) = src;
|
||||
let (dst_name, dst) = dst;
|
||||
let start_sys = self.closest(src);
|
||||
let goal_sys = self.closest(dst);
|
||||
let d_total = dist(src, dst);
|
||||
let d_total = dist(&start_sys.pos, &goal_sys.pos);
|
||||
let mut state=SearchState {
|
||||
mode:"BFS".into(),
|
||||
depth:0,
|
||||
queue_size:0,
|
||||
d_rem: d_total,
|
||||
d_total: d_total,
|
||||
prc_done: 0.0,
|
||||
n_seen:0,
|
||||
prc_seen: 0.0,
|
||||
system: start_sys.system.clone(),
|
||||
body: start_sys.body.clone()
|
||||
};
|
||||
{
|
||||
println!("Plotting route from {} to {}...", src_name, dst_name);
|
||||
println!(
|
||||
|
@ -738,26 +763,31 @@ impl Router {
|
|||
let total = self.tree.size() as f32;
|
||||
let mut prev = FnvHashMap::default();
|
||||
let mut seen = FnvHashSet::default();
|
||||
let t_start = Instant::now();
|
||||
let mut depth = 0;
|
||||
let mut found = false;
|
||||
let mut queue: VecDeque<(usize, &System)> = VecDeque::new();
|
||||
let mut queue_next: VecDeque<(usize, &System)> = VecDeque::new();
|
||||
let mut d_rem = dist2(&start_sys.pos, &goal_sys.pos);
|
||||
let mut d_rem = dist(&start_sys.pos, &goal_sys.pos);
|
||||
queue.push_front((0, &start_sys));
|
||||
seen.insert(start_sys.id);
|
||||
while !(queue.is_empty() || found) {
|
||||
print!(
|
||||
"[{}] {:.02}% | Depth: {}, Queue: {}, Seen: {} ({:.02}%), Remaining Distance: {} \r",
|
||||
format_duration(t_start.elapsed()),
|
||||
(((d_total-d_rem.sqrt())*100f32)/d_total),
|
||||
depth,
|
||||
queue.len(),
|
||||
seen.len(),
|
||||
((seen.len() * 100) as f32) / total,
|
||||
d_rem.sqrt()
|
||||
);
|
||||
std::io::stdout().flush().unwrap();
|
||||
state.depth=depth;
|
||||
state.queue_size=queue.len();
|
||||
state.prc_done=((d_total-d_rem)*100f32) / d_total;
|
||||
state.d_rem=d_rem;
|
||||
state.n_seen=seen.len();
|
||||
state.prc_seen=((seen.len()*100) as f32) / total;
|
||||
{
|
||||
let s=queue.get(0).unwrap().1;
|
||||
state.system=s.system.clone();
|
||||
state.body=s.body.clone();
|
||||
}
|
||||
match (self.callback)(&state) {
|
||||
Ok(_) => (),
|
||||
Err(e) => {
|
||||
return Err(format!("{:?}",e).to_string());
|
||||
}
|
||||
};
|
||||
while let Some((d, sys)) = queue.pop_front() {
|
||||
if sys.id == goal_sys.id {
|
||||
found = true;
|
||||
|
@ -769,7 +799,7 @@ impl Router {
|
|||
.filter(|&nb| seen.insert(nb.id))
|
||||
.map(|nb| {
|
||||
prev.insert(nb.id, sys);
|
||||
let dist = dist2(&nb.pos, &goal_sys.pos);
|
||||
let dist = dist(&nb.pos, &goal_sys.pos);
|
||||
if dist < d_rem {
|
||||
d_rem = dist;
|
||||
}
|
||||
|
@ -783,8 +813,7 @@ impl Router {
|
|||
println!();
|
||||
println!();
|
||||
if !found {
|
||||
eprintln!("No route from {} to {} found!", src_name, dst_name);
|
||||
return Vec::new();
|
||||
return Err(format!("No route from {} to {} found!", src_name, dst_name).to_string());
|
||||
}
|
||||
let mut v: Vec<System> = Vec::new();
|
||||
let mut curr_sys = goal_sys;
|
||||
|
@ -798,82 +827,57 @@ impl Router {
|
|||
}
|
||||
}
|
||||
v.reverse();
|
||||
v
|
||||
Ok(v)
|
||||
}
|
||||
}
|
||||
pub fn route(opts: RouteOpts) -> std::io::Result<()> {
|
||||
pub fn route(opts: RouteOpts) -> Result<Option<Vec<System>>,String> {
|
||||
|
||||
if opts.systems.is_empty() {
|
||||
if opts.precomp_file.is_some() {
|
||||
eprintln!("Error: Please specify exatly one system");
|
||||
return if opts.precomp_file.is_some() {
|
||||
Err("Error: Please specify exatly one system".to_string())
|
||||
} else if opts.precompute {
|
||||
eprintln!("Error: Please specify at least one system");
|
||||
Err("Error: Please specify at least one system".to_string())
|
||||
} else {
|
||||
eprintln!("Error: Please specify at least two systems");
|
||||
Err("Error: Please specify at least two systems".to_string())
|
||||
}
|
||||
std::process::exit(1);
|
||||
}
|
||||
let mut path = opts.file_path;
|
||||
let mut router: Router = if opts.precomp_file.is_some() {
|
||||
let ret = Router::from_file(&opts.precomp_file.clone().unwrap());
|
||||
path = ret.0;
|
||||
ret.1
|
||||
let (path_,ret) = Router::from_file(&opts.precomp_file.clone().unwrap(),Box::new(opts.callback))?;
|
||||
path=path_;
|
||||
ret
|
||||
} else if opts.range.is_some() {
|
||||
Router::new(&path, opts.range.unwrap(), opts.primary, Box::new(opts.callback))?
|
||||
} else {
|
||||
Router::new(&path, opts.range.unwrap(), opts.primary, Box::new(|state| {
|
||||
println!("State: {:?}",state);
|
||||
}))
|
||||
return Err("Please specify a jump range!".to_string());
|
||||
};
|
||||
if opts.precompute {
|
||||
for sys in opts.systems {
|
||||
router.route_tree = None;
|
||||
router.precompute(&sys);
|
||||
router.precompute(&sys)?;
|
||||
}
|
||||
std::process::exit(0);
|
||||
return Ok(None)
|
||||
}
|
||||
let t_route = Instant::now();
|
||||
let route = if router.route_tree.is_some() {
|
||||
router.route_to(opts.systems.first().unwrap(), &path)
|
||||
} else if opts.permute || opts.full_permute {
|
||||
router.route_to(opts.systems.first().unwrap(), &path)?
|
||||
} else if opts.permute {
|
||||
router.best_name_multiroute(
|
||||
&opts.systems,
|
||||
opts.range.unwrap(),
|
||||
opts.full_permute,
|
||||
(opts.keep_first,opts.keep_last),
|
||||
opts.mode,
|
||||
opts.factor.unwrap_or(1.0),
|
||||
)
|
||||
)?
|
||||
} else {
|
||||
router.name_multiroute(
|
||||
&opts.systems,
|
||||
opts.range.unwrap(),
|
||||
opts.mode,
|
||||
opts.factor.unwrap_or(1.0),
|
||||
)
|
||||
)?
|
||||
};
|
||||
println!("Route computed in {}\n", format_duration(t_route.elapsed()));
|
||||
if route.is_empty() {
|
||||
eprintln!("No route found!");
|
||||
return Ok(());
|
||||
return Err("No route found!".to_string())
|
||||
}
|
||||
let mut total: f32 = 0.0;
|
||||
for (sys1, sys2) in route.iter().zip(route.iter().skip(1)) {
|
||||
let dist = sys1.distp(sys2);
|
||||
total += dist;
|
||||
println!(
|
||||
"{} ({}) [{}] ({},{},{}) [{} Ls]: {:.2} Ly",
|
||||
sys1.body,
|
||||
sys1.system,
|
||||
sys1.star_type,
|
||||
sys1.pos[0],
|
||||
sys1.pos[1],
|
||||
sys1.pos[2],
|
||||
sys1.distance,
|
||||
dist
|
||||
);
|
||||
}
|
||||
let sys = route.iter().last().unwrap();
|
||||
println!(
|
||||
"{} ({}) [{}] ({},{},{}) [{} Ls]: {:.2} Ly",
|
||||
sys.body, sys.system, sys.star_type, sys.pos[0], sys.pos[1], sys.pos[2], sys.distance, 0.0
|
||||
);
|
||||
println!("Total: {:.2} Ly ({} Jumps)", total, route.len());
|
||||
Ok(())
|
||||
return Ok(Some(route));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue