Optimize last route segment

This commit is contained in:
Arne Keller 2019-02-02 14:23:43 +01:00
parent 6de816f6a4
commit 37544b09aa
3 changed files with 41 additions and 46 deletions

View File

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

View File

@ -57,7 +57,7 @@ fn read_stdin() -> InputData {
}
}
fn custom_input() -> InputData {
fn _custom_input() -> InputData {
let mut polys = vec![];
polys.push(Polygon::new(
vec![

View File

@ -4,6 +4,7 @@ use chrono::{Duration, NaiveTime};
use std::cmp;
use std::collections::BinaryHeap;
use std::f64;
mod display;
mod input;
@ -113,43 +114,41 @@ fn main() {
let bus = last.bus;
let range = to_bus(bus, last.pos);
if range.len() == 2 {
let range = Line::new(range[0], range[1]);
for next_y in float_range(range.start.y, range.end.y) {
let mut next = range.start;
next.y = next_y;
let next = Point::from(next);
let line = Line::new(last.pos, next);
if none_intersect(&polys, &line) {
let delay =
line.end.y
- bus.y() - line.end_point().euclidean_distance(&line.start_point()) * 2.0;
if delay > best_delay {
// neue beste Wartezeit
let mut route = s.clone();
route.insert(
0,
RunState {
pos: next,
bus: next,
delay,
},
);
eprintln!(
"Verbesserung: {:?} ({:?} Zustände verbleiben)",
delay_to_time(delay),
states.len()
);
best = route;
best.reverse();
best_delay = delay;
display::save_svg(
&format!("{}{}.svg", save_prefix, save_counter),
house,
&polys,
&best,
);
save_counter += 1;
}
let next = Point::new(
0.0,
last.pos.y() + 30.0f64.to_radians().tan() * last.pos.x(),
);
let line = Line::new(last.pos, next);
if none_intersect(&polys, &line) {
let delay = line.end.y
- bus.y() - line.end_point().euclidean_distance(&line.start_point())
* 2.0;
if delay > best_delay {
// neue beste Wartezeit
let mut route = s.clone();
route.insert(
0,
RunState {
pos: next,
bus: next,
delay,
},
);
eprintln!(
"Verbesserung: {:?} ({:?} Zustände verbleiben)",
delay_to_time(delay),
states.len()
);
best = route;
best.reverse();
best_delay = delay;
display::save_svg(
&format!("{}{}.svg", save_prefix, save_counter),
house,
&polys,
&best,
);
save_counter += 1;
}
}
states.extend(all);
@ -164,13 +163,6 @@ fn main() {
display::dump_route(house, &polys, &route);
}
/// [a; b]
fn float_range(a: f64, b: f64) -> impl Iterator<Item = f64> {
const STEPS: usize = 1337;
let d = b - a;
(0..=STEPS).map(move |s| a + ((s as f64) * d) / STEPS as f64)
}
fn none_intersect(polys: &[Polygon], line: &Line) -> bool {
let mut middle = line.start;
middle.x += line.dx() / 2.0;