refactored data loading code
This commit is contained in:
parent
6f6df97af8
commit
1067c6a514
8 changed files with 74 additions and 128 deletions
|
|
@ -1,7 +1,8 @@
|
|||
use bincode;
|
||||
use clap::Parser;
|
||||
use fapra_osm_2::alt::{LandmarkSet, Landmark};
|
||||
use fapra_osm_2::gridgraph::{GridGraph, EdgeCost};
|
||||
use fapra_osm_2::alt::{Landmark, LandmarkSet};
|
||||
use fapra_osm_2::gridgraph::EdgeCost;
|
||||
use fapra_osm_2::utils::load_graph;
|
||||
use std::fs::File;
|
||||
use std::io::prelude::*;
|
||||
use std::process::exit;
|
||||
|
|
@ -11,7 +12,7 @@ use std::process::exit;
|
|||
struct Args {
|
||||
/// the FMI file to load
|
||||
#[clap(short, long)]
|
||||
input: String,
|
||||
graph: String,
|
||||
|
||||
/// the file to which to write the landmarks
|
||||
#[clap(short, long)]
|
||||
|
|
@ -25,16 +26,7 @@ struct Args {
|
|||
fn main() {
|
||||
let args = Args::parse();
|
||||
|
||||
let file = match File::open(args.input.clone()) {
|
||||
Ok(f) => f,
|
||||
Err(e) => {
|
||||
println!("Error while opening file: {}", e);
|
||||
exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
let graph = GridGraph::from_fmi_file(file).unwrap();
|
||||
println!("finished loading graph from file");
|
||||
let graph = load_graph(&args.graph);
|
||||
|
||||
let mut output = match File::create(args.output.clone()) {
|
||||
Ok(f) => f,
|
||||
|
|
@ -45,7 +37,6 @@ fn main() {
|
|||
};
|
||||
|
||||
fn compute_next_index(costs: &mut Vec<EdgeCost>, landmark: &Landmark) -> usize {
|
||||
|
||||
let mut max_index = 0;
|
||||
let mut max_cost = 0;
|
||||
|
||||
|
|
@ -67,7 +58,6 @@ fn main() {
|
|||
|
||||
println!("selected initial node: {:?}", initial_node);
|
||||
|
||||
|
||||
let mut landmark = Landmark::generate(*initial_node, &graph);
|
||||
|
||||
let mut costs: Vec<EdgeCost> = landmark.distances.clone();
|
||||
|
|
@ -80,7 +70,10 @@ fn main() {
|
|||
while set.landmarks.len() < args.amount {
|
||||
let node = graph.nodes[next];
|
||||
|
||||
println!("the next node will be {:?} with a distance of {}", node, costs[node.index as usize]);
|
||||
println!(
|
||||
"the next node will be {:?} with a distance of {}",
|
||||
node, costs[node.index as usize]
|
||||
);
|
||||
|
||||
landmark = Landmark::generate(node, &graph);
|
||||
next = compute_next_index(&mut costs, &landmark);
|
||||
|
|
@ -89,5 +82,7 @@ fn main() {
|
|||
|
||||
let encoded = bincode::serialize(&set).unwrap();
|
||||
|
||||
output.write_all(&encoded).expect("Error while writing LandmarkSet data");
|
||||
output
|
||||
.write_all(&encoded)
|
||||
.expect("Error while writing LandmarkSet data");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
use bincode;
|
||||
use clap::Parser;
|
||||
use fapra_osm_2::alt::LandmarkSet;
|
||||
use fapra_osm_2::gridgraph::GridGraph;
|
||||
use fapra_osm_2::utils::load_graph;
|
||||
use std::fs::File;
|
||||
use std::io::prelude::*;
|
||||
use std::process::exit;
|
||||
|
|
@ -11,7 +11,7 @@ use std::process::exit;
|
|||
struct Args {
|
||||
/// the FMI file to load
|
||||
#[clap(short, long)]
|
||||
input: String,
|
||||
graph: String,
|
||||
|
||||
/// the file to which to write the landmarks
|
||||
#[clap(short, long)]
|
||||
|
|
@ -25,16 +25,7 @@ struct Args {
|
|||
fn main() {
|
||||
let args = Args::parse();
|
||||
|
||||
let file = match File::open(args.input.clone()) {
|
||||
Ok(f) => f,
|
||||
Err(e) => {
|
||||
println!("Error while opening file: {}", e);
|
||||
exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
let grid = GridGraph::from_fmi_file(file).unwrap();
|
||||
println!("finished loading grid from file");
|
||||
let graph = load_graph(&args.graph);
|
||||
|
||||
let mut output = match File::create(args.output.clone()) {
|
||||
Ok(f) => f,
|
||||
|
|
@ -44,7 +35,7 @@ fn main() {
|
|||
}
|
||||
};
|
||||
|
||||
let set = LandmarkSet::random_set(args.amount, &grid);
|
||||
let set = LandmarkSet::random_set(args.amount, &graph);
|
||||
|
||||
let encoded = bincode::serialize(&set).unwrap();
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
use fapra_osm_2::gridgraph::GridGraph;
|
||||
use std::fs::File;
|
||||
use fapra_osm_2::utils::load_graph;
|
||||
use clap::Parser;
|
||||
use std::process::exit;
|
||||
use rand::seq::SliceRandom;
|
||||
|
|
@ -21,21 +20,7 @@ struct Args {
|
|||
fn main() {
|
||||
let args = Args::parse();
|
||||
|
||||
let file = match File::open(args.graph.clone()) {
|
||||
Ok(f) => f,
|
||||
Err(e) => {
|
||||
println!("Error while opening the file {}: {}", args.graph, e);
|
||||
exit(1)
|
||||
}
|
||||
};
|
||||
|
||||
let graph = match GridGraph::from_fmi_file(file) {
|
||||
Ok(g) => g,
|
||||
Err(e) => {
|
||||
println!("Error while reading the graph: {:?}", e);
|
||||
exit(1);
|
||||
}
|
||||
};
|
||||
let graph = load_graph(&args.graph);
|
||||
|
||||
let mut rng = rand::thread_rng();
|
||||
|
||||
|
|
|
|||
|
|
@ -1,28 +1,18 @@
|
|||
use fapra_osm_2::gridgraph::GridGraph;
|
||||
use fapra_osm_2::utils::load_graph;
|
||||
use clap::Parser;
|
||||
use std::fs::File;
|
||||
use std::process::exit;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[clap(author, version, about, long_about=None)]
|
||||
struct Args {
|
||||
/// the FMI file to load
|
||||
#[clap(short, long)]
|
||||
input: String,
|
||||
graph: String,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args = Args::parse();
|
||||
|
||||
let file = match File::open(args.input.clone()) {
|
||||
Ok(f) => f,
|
||||
Err(e) => {
|
||||
println!("Error while opening file: {}", e);
|
||||
exit(1);
|
||||
}
|
||||
};
|
||||
let graph = load_graph(&args.graph);
|
||||
|
||||
let grid = GridGraph::from_fmi_file(file).unwrap();
|
||||
|
||||
println!("{}", grid.to_geojson().to_string());
|
||||
println!("{}", graph.to_geojson().to_string());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
use clap::Parser;
|
||||
use fapra_osm_2::alt::{LandmarkBestSet, LandmarkSet};
|
||||
use fapra_osm_2::alt::LandmarkBestSet;
|
||||
use fapra_osm_2::astar::{estimate_haversine, AStar};
|
||||
use fapra_osm_2::gridgraph::{GridGraph, NodeId};
|
||||
use fapra_osm_2::utils::RoutingQuery;
|
||||
use fapra_osm_2::gridgraph::NodeId;
|
||||
use fapra_osm_2::utils::{load_graph, load_landmarks, RoutingQuery};
|
||||
use serde_json;
|
||||
use std::fs::File;
|
||||
use std::io::BufReader;
|
||||
|
|
@ -40,21 +40,7 @@ struct Args {
|
|||
fn main() {
|
||||
let args = Args::parse();
|
||||
|
||||
let file = match File::open(args.graph.clone()) {
|
||||
Ok(f) => f,
|
||||
Err(e) => {
|
||||
println!("Error while opening the file {}: {}", args.graph, e);
|
||||
exit(1)
|
||||
}
|
||||
};
|
||||
|
||||
let graph = match GridGraph::from_fmi_file(file) {
|
||||
Ok(g) => g,
|
||||
Err(e) => {
|
||||
println!("Error while reading the graph: {:?}", e);
|
||||
exit(1);
|
||||
}
|
||||
};
|
||||
let graph = load_graph(&args.graph);
|
||||
|
||||
let astar = AStar { graph: &(*graph) };
|
||||
|
||||
|
|
@ -69,18 +55,7 @@ fn main() {
|
|||
let targets: Vec<RoutingQuery> = serde_json::from_reader(BufReader::new(targets)).unwrap();
|
||||
|
||||
if let Some(landmark_path) = args.landmarks {
|
||||
let landmarks = match File::open(landmark_path.clone()) {
|
||||
Ok(f) => f,
|
||||
Err(e) => {
|
||||
println!(
|
||||
"Error while opening landmark file {}: {:?}",
|
||||
landmark_path, e
|
||||
);
|
||||
exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
let landmarks: LandmarkSet = bincode::deserialize_from(BufReader::new(landmarks)).unwrap();
|
||||
let landmarks = load_landmarks(&landmark_path);
|
||||
|
||||
println!("running ALT");
|
||||
|
||||
|
|
|
|||
|
|
@ -6,11 +6,9 @@ use fapra_osm_2::alt::{LandmarkBestSet, LandmarkSet};
|
|||
use fapra_osm_2::astar::{estimate_haversine, AStar};
|
||||
use fapra_osm_2::coordinates::{DegreeCoordinate, RadianCoordinate};
|
||||
use fapra_osm_2::gridgraph::{GridGraph, NodeId, Route};
|
||||
use fapra_osm_2::utils::{load_graph, load_landmarks};
|
||||
use rand::seq::SliceRandom;
|
||||
use serde::Serialize;
|
||||
use std::fs::File;
|
||||
use std::io::BufReader;
|
||||
use std::process::exit;
|
||||
use std::time::Instant;
|
||||
|
||||
use rocket::form::Form;
|
||||
|
|
@ -25,7 +23,7 @@ use rocket_dyn_templates::{context, Engines, Template};
|
|||
struct Args {
|
||||
/// name of the FMI file to read
|
||||
#[clap(short, long)]
|
||||
filename: String,
|
||||
graph: String,
|
||||
|
||||
/// the landmarks to load
|
||||
#[clap(short, long)]
|
||||
|
|
@ -141,37 +139,9 @@ struct GraphWrapper {
|
|||
fn rocket() -> _ {
|
||||
let args = Args::parse();
|
||||
|
||||
println!("Loading file from {}", args.filename);
|
||||
let file = match File::open(args.filename.clone()) {
|
||||
Ok(f) => f,
|
||||
Err(e) => {
|
||||
println!("Error while opening the file {}: {}", args.filename, e);
|
||||
exit(1)
|
||||
}
|
||||
};
|
||||
let graph = load_graph(&args.graph);
|
||||
|
||||
let graph = match GridGraph::from_fmi_file(file) {
|
||||
Ok(g) => g,
|
||||
Err(e) => {
|
||||
println!("Error while reading the graph: {:?}", e);
|
||||
exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
println!("Loaded graph file");
|
||||
|
||||
let landmarks = match File::open(args.landmarks.clone()) {
|
||||
Ok(f) => f,
|
||||
Err(e) => {
|
||||
println!(
|
||||
"Error while opening landmark file {}: {:?}",
|
||||
args.landmarks, e
|
||||
);
|
||||
exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
let landmarks: LandmarkSet = bincode::deserialize_from(BufReader::new(landmarks)).unwrap();
|
||||
let landmarks = load_landmarks(&args.landmarks);
|
||||
|
||||
rocket::build()
|
||||
.manage(GraphWrapper {
|
||||
|
|
|
|||
40
src/utils.rs
40
src/utils.rs
|
|
@ -1,4 +1,9 @@
|
|||
use crate::alt::LandmarkSet;
|
||||
use crate::gridgraph::GridGraph;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fs::File;
|
||||
use std::io::BufReader;
|
||||
use std::process::exit;
|
||||
|
||||
pub const EARTH_RADIUS: f64 = 6_371_000.0; // meters
|
||||
|
||||
|
|
@ -7,3 +12,38 @@ pub struct RoutingQuery{
|
|||
pub source: usize,
|
||||
pub destination: usize,
|
||||
}
|
||||
|
||||
pub fn load_graph(path: &str) -> Box<GridGraph> {
|
||||
println!("Loading file from {}", path);
|
||||
let file = match File::open(path) {
|
||||
Ok(f) => f,
|
||||
Err(e) => {
|
||||
println!("Error while opening the file {}: {}", path, e);
|
||||
exit(1)
|
||||
}
|
||||
};
|
||||
|
||||
let graph = match GridGraph::from_fmi_file(file) {
|
||||
Ok(g) => g,
|
||||
Err(e) => {
|
||||
println!("Error while reading the graph: {:?}", e);
|
||||
exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
println!("Loaded graph file");
|
||||
|
||||
graph
|
||||
}
|
||||
|
||||
pub fn load_landmarks(path: &str) -> LandmarkSet {
|
||||
let landmarks = match File::open(path) {
|
||||
Ok(f) => f,
|
||||
Err(e) => {
|
||||
println!("Error while opening landmark file {}: {:?}", path, e);
|
||||
exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
bincode::deserialize_from(BufReader::new(landmarks)).unwrap()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,5 +13,5 @@ output = argv[2]
|
|||
for i in range(2, 7):
|
||||
num = 2**i
|
||||
|
||||
system(f"cargo run --release --bin=gen_landmarks_greedy -- --input={graph} --output={output}_greedy_{ num }.bin --amount { num }")
|
||||
system(f"cargo run --release --bin=gen_landmarks_random -- --input={graph} --output={output}_random_{ num }.bin --amount { num }")
|
||||
system(f"cargo run --release --bin=gen_landmarks_greedy -- --graph={graph} --output={output}_greedy_{ num }.bin --amount { num }")
|
||||
system(f"cargo run --release --bin=gen_landmarks_random -- --graph={graph} --output={output}_random_{ num }.bin --amount { num }")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue