German comments; optimizations

This commit is contained in:
Arne Keller 2019-01-31 16:10:03 +01:00
parent c93c0d3043
commit 6de816f6a4
4 changed files with 38 additions and 99 deletions

29
Cargo.lock generated
View File

@ -1,9 +1,8 @@
[[package]] [[package]]
name = "Aufgabe1" name = "aufgabe1"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"decorum 0.1.2",
"geo 0.11.0", "geo 0.11.0",
"svg 0.5.12 (registry+https://github.com/rust-lang/crates.io-index)", "svg 0.5.12 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
@ -55,15 +54,6 @@ dependencies = [
"time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "decorum"
version = "0.1.2"
dependencies = [
"num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.84 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.84 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "failure" name = "failure"
version = "0.1.5" version = "0.1.5"
@ -160,21 +150,6 @@ name = "rustc-demangle"
version = "0.1.13" version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "serde"
version = "1.0.84"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "serde_derive"
version = "1.0.84"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.25 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "svg" name = "svg"
version = "0.5.12" version = "0.5.12"
@ -253,8 +228,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum redox_syscall 0.1.51 (registry+https://github.com/rust-lang/crates.io-index)" = "423e376fffca3dfa06c9e9790a9ccd282fafb3cc6e6397d01dbf64f9bacc6b85" "checksum redox_syscall 0.1.51 (registry+https://github.com/rust-lang/crates.io-index)" = "423e376fffca3dfa06c9e9790a9ccd282fafb3cc6e6397d01dbf64f9bacc6b85"
"checksum rstar 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "64ffe043929ee67d46694af1a4851f6bbe571b52a55677ba1686222dc35fd449" "checksum rstar 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "64ffe043929ee67d46694af1a4851f6bbe571b52a55677ba1686222dc35fd449"
"checksum rustc-demangle 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "adacaae16d02b6ec37fdc7acfcddf365978de76d1983d3ee22afc260e1ca9619" "checksum rustc-demangle 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "adacaae16d02b6ec37fdc7acfcddf365978de76d1983d3ee22afc260e1ca9619"
"checksum serde 1.0.84 (registry+https://github.com/rust-lang/crates.io-index)" = "0e732ed5a5592c17d961555e3b552985baf98d50ce418b7b655f31f6ba7eb1b7"
"checksum serde_derive 1.0.84 (registry+https://github.com/rust-lang/crates.io-index)" = "b4d6115a3ca25c224e409185325afc16a0d5aaaabc15c42b09587d6f1ba39a5b"
"checksum svg 0.5.12 (registry+https://github.com/rust-lang/crates.io-index)" = "a863ec1f8e7cfd4ea449f77445cca06aac240b9a677ccf12b0f65ef020db52c7" "checksum svg 0.5.12 (registry+https://github.com/rust-lang/crates.io-index)" = "a863ec1f8e7cfd4ea449f77445cca06aac240b9a677ccf12b0f65ef020db52c7"
"checksum syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)" = "f92e629aa1d9c827b2bb8297046c1ccffc57c99b947a680d3ccff1f136a3bee9" "checksum syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)" = "f92e629aa1d9c827b2bb8297046c1ccffc57c99b947a680d3ccff1f136a3bee9"
"checksum synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "73687139bf99285483c96ac0add482c3776528beac1d97d444f6e91f203a2015" "checksum synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "73687139bf99285483c96ac0add482c3776528beac1d97d444f6e91f203a2015"

View File

@ -1,7 +1,7 @@
[package] [package]
name = "Aufgabe1" name = "aufgabe1"
version = "0.1.0" version = "0.1.0"
authors = ["arne"] authors = ["Arne Keller <arne.keller01@yahoo.de>"]
edition = "2018" edition = "2018"
[profile.release] [profile.release]
@ -11,5 +11,4 @@ opt-level = 3
[dependencies] [dependencies]
geo = { path = "/home/arne/Documents/Code/Github/geo/geo" } geo = { path = "/home/arne/Documents/Code/Github/geo/geo" }
svg = "0.5.11" svg = "0.5.11"
decorum = { path = "/home/arne/Documents/Code/Github/decorum" }
chrono = "0.4.6" chrono = "0.4.6"

View File

@ -63,8 +63,8 @@ pub(crate) fn generate_svg(
route2_start: Point, route2_start: Point,
) -> String { ) -> String {
let mut document = Document::new() let mut document = Document::new()
// view box at (0, -200), w*h (200, 200) // view box at (x,y), (w,h)
.set("viewBox", (0, -1100, 2100, 2100)) .set("viewBox", (0.0, route2_start.y(), 2100.0, -route2_start.y()+1000.0))
.set("xmlns:xlink", "http://www.w3.org/1999/xlink"); .set("xmlns:xlink", "http://www.w3.org/1999/xlink");
for circle in points.iter().map(|(p, color)| { for circle in points.iter().map(|(p, color)| {

View File

@ -13,16 +13,16 @@ type Line = geo::Line<f64>;
type LineString = geo::LineString<f64>; type LineString = geo::LineString<f64>;
type Polygon = geo::Polygon<f64>; type Polygon = geo::Polygon<f64>;
// 30 km/h = 8+1/3 meters/second // 30 km/h = 8+1/3 m/s
// 15 km/h = 4+1/6 meters/second // 15 km/h = 4+1/6 m/s
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
struct RunState { struct RunState {
/// Theoretically possible maximum delay /// Theoretisch mögliche Wartezeit
delay: f64, delay: f64,
/// Our current location /// Lisas Position
pos: Point, pos: Point,
/// Current location of the bus, not including any delays /// Position des Busses
bus: Point, bus: Point,
} }
impl cmp::Eq for RunState {} impl cmp::Eq for RunState {}
@ -38,33 +38,36 @@ impl cmp::PartialOrd for RunState {
} }
fn main() { fn main() {
// starting at 07:00, bus is 15 km away // 07:00: Bus ist 15 km entfernt
let start_time = NaiveTime::from_hms(7, 0, 0); let start_time = NaiveTime::from_hms(7, 0, 0);
let bus = Point::new(0.0, -15000.0); let bus = Point::new(0.0, -15000.0);
// 30 km/h -> 8.3 m/s
let bus_speed = 30.0 / 3.6; let bus_speed = 30.0 / 3.6;
// Zeitpunkt basierend auf Wartezeit in Sekunden berechnen
let delay_to_time = |delay| start_time + Duration::seconds((delay / bus_speed) as i64); let delay_to_time = |delay| start_time + Duration::seconds((delay / bus_speed) as i64);
// Input einlesen (Lisas Haus und Hindernisse)
let data = input::read_input(); let data = input::read_input();
let house = data.start; let house = data.start;
let polys = data.polys; let polys = data.polys;
eprintln!("{:?}", house);
eprintln!("{:?}", polys);
for p in &polys { for p in &polys {
assert!(p.interiors.is_empty()); assert!(p.interiors.is_empty());
} }
// alle Ecken der Hindernisse bestimmen
let points = polys let points = polys
.iter() .iter()
.map(|x| x.exterior.0.clone()) .map(|x| x.exterior.0.clone())
.flatten() .flatten()
.collect::<Vec<_>>(); .collect::<Vec<_>>();
// Startzustand
let start = RunState { let start = RunState {
pos: house, pos: house,
bus: bus, bus: bus,
delay: max_possible_delay(bus, house), delay: max_possible_delay(bus, house),
}; };
eprintln!("# Max. delay possible: {:?}", delay_to_time(start.delay)); eprintln!("Maximum: {:?}", delay_to_time(start.delay));
let mut states = BinaryHeap::new(); let mut states = BinaryHeap::new();
states.push(vec![start]); states.push(vec![start]);
@ -74,36 +77,39 @@ fn main() {
let save_prefix = "tmp_"; let save_prefix = "tmp_";
let mut save_counter = 0; let mut save_counter = 0;
// weitersuchen, bis kein besserer Zustand mehr vorhanden ist
while states.peek().map(|x| x[0].delay > best_delay) == Some(true) { while states.peek().map(|x| x[0].delay > best_delay) == Some(true) {
let s = states.pop().unwrap(); let s = states.pop().unwrap();
let last = &s[0]; let last = &s[0];
// new states // neue Zustände
let mut all = vec![]; let mut all = vec![];
// attempt to go to any other vertex/point // versuche zu jeder anderen Ecke zu gehen
for next in points for next in points
.iter() .iter()
.filter(|next| !s.iter().any(|x| x.pos == (**next).into())) .filter(|next| !s.iter().any(|x| x.pos == (**next).into()))
{ {
let next = Point::from(*next); let next = Point::from(*next);
if next != last.pos && none_intersect(&polys, &Line::new(last.pos, next)) { if next != last.pos && none_intersect(&polys, &Line::new(last.pos, next)) {
// could run to that point // Lisa könnte zu dieser Ecke rennen
let bus_next = last.bus.translate(0.0, distance(last.pos, next) * 2.0); let bus_next = last.bus.translate(0.0, distance(last.pos, next) * 2.0);
let mut route = s.clone(); let delay = max_possible_delay(bus_next, next);
//eprintln!("{},{} would go {},{}", last.pos.x(), last.pos.y(), next.x(), next.y()); if delay > best_delay {
route.insert( let mut route = s.clone();
0, route.insert(
RunState { 0,
pos: next, RunState {
bus: bus_next, pos: next,
delay: max_possible_delay(bus_next, next), bus: bus_next,
}, delay,
); },
all.push(route); );
all.push(route);
}
} }
} }
// attempt to go to the bus // versuche, direkt zum Bus zu gehen
let bus = last.bus; let bus = last.bus;
let range = to_bus(bus, last.pos); let range = to_bus(bus, last.pos);
if range.len() == 2 { if range.len() == 2 {
@ -118,7 +124,7 @@ fn main() {
line.end.y line.end.y
- bus.y() - line.end_point().euclidean_distance(&line.start_point()) * 2.0; - bus.y() - line.end_point().euclidean_distance(&line.start_point()) * 2.0;
if delay > best_delay { if delay > best_delay {
// new high score! // neue beste Wartezeit
let mut route = s.clone(); let mut route = s.clone();
route.insert( route.insert(
0, 0,
@ -129,31 +135,27 @@ fn main() {
}, },
); );
eprintln!( eprintln!(
"# New best delay {:?} ({:?} states left)", "Verbesserung: {:?} ({:?} Zustände verbleiben)",
delay_to_time(delay), delay_to_time(delay),
states.len() states.len()
); );
best = route; best = route;
best.reverse(); best.reverse();
best_delay = delay; best_delay = delay;
/*
display::save_svg( display::save_svg(
&format!("{}{}.svg", save_prefix, save_counter), &format!("{}{}.svg", save_prefix, save_counter),
house, house,
&polys, &polys,
&best, &best,
); );
*/
save_counter += 1; save_counter += 1;
} else {
// not worth it
} }
} }
} }
states.extend(all); states.extend(all);
} }
} }
eprintln!("d = {:?}", delay_to_time(best_delay)); eprintln!("Zeit = {:?}", delay_to_time(best_delay));
let route = best; let route = best;
eprintln!("Route:"); eprintln!("Route:");
for s in &route { for s in &route {
@ -246,38 +248,3 @@ fn to_bus(bus: Point, start: Point) -> Vec<Point> {
vec![] vec![]
} }
} }
#[cfg(test)]
#[test]
fn test_to_bus() {
assert!(
to_bus(
Coordinate {
x: 0.0.into(),
y: 0.0.into()
}
.into(),
Coordinate {
x: 1.0.into(),
y: 23.0.into()
}
.into()
)
.len() == 2
);
assert_eq!(
to_bus(
Coordinate {
x: 0.0.into(),
y: 168.7601848778319.into()
}
.into(),
Coordinate {
x: 1.0.into(),
y: 23.0.into()
}
.into()
),
vec![]
);
}