refactored data loading code

This commit is contained in:
Johannes Erwerle 2022-09-15 15:42:06 +02:00
parent 6f6df97af8
commit 1067c6a514
8 changed files with 74 additions and 128 deletions

View file

@ -1,7 +1,8 @@
use bincode; use bincode;
use clap::Parser; use clap::Parser;
use fapra_osm_2::alt::{LandmarkSet, Landmark}; use fapra_osm_2::alt::{Landmark, LandmarkSet};
use fapra_osm_2::gridgraph::{GridGraph, EdgeCost}; use fapra_osm_2::gridgraph::EdgeCost;
use fapra_osm_2::utils::load_graph;
use std::fs::File; use std::fs::File;
use std::io::prelude::*; use std::io::prelude::*;
use std::process::exit; use std::process::exit;
@ -11,7 +12,7 @@ use std::process::exit;
struct Args { struct Args {
/// the FMI file to load /// the FMI file to load
#[clap(short, long)] #[clap(short, long)]
input: String, graph: String,
/// the file to which to write the landmarks /// the file to which to write the landmarks
#[clap(short, long)] #[clap(short, long)]
@ -25,16 +26,7 @@ struct Args {
fn main() { fn main() {
let args = Args::parse(); let args = Args::parse();
let file = match File::open(args.input.clone()) { let graph = load_graph(&args.graph);
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 mut output = match File::create(args.output.clone()) { let mut output = match File::create(args.output.clone()) {
Ok(f) => f, Ok(f) => f,
@ -45,7 +37,6 @@ fn main() {
}; };
fn compute_next_index(costs: &mut Vec<EdgeCost>, landmark: &Landmark) -> usize { fn compute_next_index(costs: &mut Vec<EdgeCost>, landmark: &Landmark) -> usize {
let mut max_index = 0; let mut max_index = 0;
let mut max_cost = 0; let mut max_cost = 0;
@ -67,7 +58,6 @@ fn main() {
println!("selected initial node: {:?}", initial_node); println!("selected initial node: {:?}", initial_node);
let mut landmark = Landmark::generate(*initial_node, &graph); let mut landmark = Landmark::generate(*initial_node, &graph);
let mut costs: Vec<EdgeCost> = landmark.distances.clone(); let mut costs: Vec<EdgeCost> = landmark.distances.clone();
@ -80,7 +70,10 @@ fn main() {
while set.landmarks.len() < args.amount { while set.landmarks.len() < args.amount {
let node = graph.nodes[next]; 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); landmark = Landmark::generate(node, &graph);
next = compute_next_index(&mut costs, &landmark); next = compute_next_index(&mut costs, &landmark);
@ -89,5 +82,7 @@ fn main() {
let encoded = bincode::serialize(&set).unwrap(); 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");
} }

View file

@ -1,7 +1,7 @@
use bincode; use bincode;
use clap::Parser; use clap::Parser;
use fapra_osm_2::alt::LandmarkSet; 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::fs::File;
use std::io::prelude::*; use std::io::prelude::*;
use std::process::exit; use std::process::exit;
@ -11,7 +11,7 @@ use std::process::exit;
struct Args { struct Args {
/// the FMI file to load /// the FMI file to load
#[clap(short, long)] #[clap(short, long)]
input: String, graph: String,
/// the file to which to write the landmarks /// the file to which to write the landmarks
#[clap(short, long)] #[clap(short, long)]
@ -25,16 +25,7 @@ struct Args {
fn main() { fn main() {
let args = Args::parse(); let args = Args::parse();
let file = match File::open(args.input.clone()) { let graph = load_graph(&args.graph);
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 mut output = match File::create(args.output.clone()) { let mut output = match File::create(args.output.clone()) {
Ok(f) => f, 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(); let encoded = bincode::serialize(&set).unwrap();

View file

@ -1,5 +1,4 @@
use fapra_osm_2::gridgraph::GridGraph; use fapra_osm_2::utils::load_graph;
use std::fs::File;
use clap::Parser; use clap::Parser;
use std::process::exit; use std::process::exit;
use rand::seq::SliceRandom; use rand::seq::SliceRandom;
@ -21,21 +20,7 @@ struct Args {
fn main() { fn main() {
let args = Args::parse(); let args = Args::parse();
let file = match File::open(args.graph.clone()) { let graph = load_graph(&args.graph);
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 mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();

View file

@ -1,28 +1,18 @@
use fapra_osm_2::gridgraph::GridGraph; use fapra_osm_2::utils::load_graph;
use clap::Parser; use clap::Parser;
use std::fs::File;
use std::process::exit;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[clap(author, version, about, long_about=None)] #[clap(author, version, about, long_about=None)]
struct Args { struct Args {
/// the FMI file to load /// the FMI file to load
#[clap(short, long)] #[clap(short, long)]
input: String, graph: String,
} }
fn main() { fn main() {
let args = Args::parse(); let args = Args::parse();
let file = match File::open(args.input.clone()) { let graph = load_graph(&args.graph);
Ok(f) => f,
Err(e) => {
println!("Error while opening file: {}", e);
exit(1);
}
};
let grid = GridGraph::from_fmi_file(file).unwrap(); println!("{}", graph.to_geojson().to_string());
println!("{}", grid.to_geojson().to_string());
} }

View file

@ -1,8 +1,8 @@
use clap::Parser; 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::astar::{estimate_haversine, AStar};
use fapra_osm_2::gridgraph::{GridGraph, NodeId}; use fapra_osm_2::gridgraph::NodeId;
use fapra_osm_2::utils::RoutingQuery; use fapra_osm_2::utils::{load_graph, load_landmarks, RoutingQuery};
use serde_json; use serde_json;
use std::fs::File; use std::fs::File;
use std::io::BufReader; use std::io::BufReader;
@ -40,21 +40,7 @@ struct Args {
fn main() { fn main() {
let args = Args::parse(); let args = Args::parse();
let file = match File::open(args.graph.clone()) { let graph = load_graph(&args.graph);
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 astar = AStar { graph: &(*graph) }; let astar = AStar { graph: &(*graph) };
@ -69,18 +55,7 @@ fn main() {
let targets: Vec<RoutingQuery> = serde_json::from_reader(BufReader::new(targets)).unwrap(); let targets: Vec<RoutingQuery> = serde_json::from_reader(BufReader::new(targets)).unwrap();
if let Some(landmark_path) = args.landmarks { if let Some(landmark_path) = args.landmarks {
let landmarks = match File::open(landmark_path.clone()) { let landmarks = load_landmarks(&landmark_path);
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();
println!("running ALT"); println!("running ALT");

View file

@ -6,11 +6,9 @@ use fapra_osm_2::alt::{LandmarkBestSet, LandmarkSet};
use fapra_osm_2::astar::{estimate_haversine, AStar}; use fapra_osm_2::astar::{estimate_haversine, AStar};
use fapra_osm_2::coordinates::{DegreeCoordinate, RadianCoordinate}; use fapra_osm_2::coordinates::{DegreeCoordinate, RadianCoordinate};
use fapra_osm_2::gridgraph::{GridGraph, NodeId, Route}; use fapra_osm_2::gridgraph::{GridGraph, NodeId, Route};
use fapra_osm_2::utils::{load_graph, load_landmarks};
use rand::seq::SliceRandom; use rand::seq::SliceRandom;
use serde::Serialize; use serde::Serialize;
use std::fs::File;
use std::io::BufReader;
use std::process::exit;
use std::time::Instant; use std::time::Instant;
use rocket::form::Form; use rocket::form::Form;
@ -25,7 +23,7 @@ use rocket_dyn_templates::{context, Engines, Template};
struct Args { struct Args {
/// name of the FMI file to read /// name of the FMI file to read
#[clap(short, long)] #[clap(short, long)]
filename: String, graph: String,
/// the landmarks to load /// the landmarks to load
#[clap(short, long)] #[clap(short, long)]
@ -141,37 +139,9 @@ struct GraphWrapper {
fn rocket() -> _ { fn rocket() -> _ {
let args = Args::parse(); let args = Args::parse();
println!("Loading file from {}", args.filename); let graph = load_graph(&args.graph);
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 = match GridGraph::from_fmi_file(file) { let landmarks = load_landmarks(&args.landmarks);
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();
rocket::build() rocket::build()
.manage(GraphWrapper { .manage(GraphWrapper {

View file

@ -1,9 +1,49 @@
use crate::alt::LandmarkSet;
use crate::gridgraph::GridGraph;
use serde::{Deserialize, Serialize}; 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 pub const EARTH_RADIUS: f64 = 6_371_000.0; // meters
#[derive(Serialize, Deserialize, Debug, Copy, Clone)] #[derive(Serialize, Deserialize, Debug, Copy, Clone)]
pub struct RoutingQuery{ pub struct RoutingQuery {
pub source: usize, pub source: usize,
pub destination: 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()
}

View file

@ -13,5 +13,5 @@ output = argv[2]
for i in range(2, 7): for i in range(2, 7):
num = 2**i 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_greedy -- --graph={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_random -- --graph={graph} --output={output}_random_{ num }.bin --amount { num }")