Add beltfinding timeout and debugging.

This commit is contained in:
hal8174 2024-09-01 01:05:34 +02:00
parent f20a1841c9
commit 79c8b0c710
13 changed files with 278 additions and 53 deletions

View file

@ -4,7 +4,10 @@ use super::{
};
use crate::prelude::*;
use crate::{belt_finding::brute_force::BruteforceBuilder, misc::Map};
use std::ops::RangeInclusive;
use std::{
ops::RangeInclusive,
time::{Duration, Instant},
};
use termcolor::ColorSpec;
#[derive(Default)]
@ -68,7 +71,7 @@ impl Candidate {
}
impl ConflictAvoidance {
pub fn new(problem: Problem) -> Self {
pub fn new(problem: &Problem) -> Self {
let mut map: Map<Field> = Map::new(problem.map.width, problem.map.height);
for x in 0..problem.map.width {
for y in 0..problem.map.height {
@ -130,7 +133,11 @@ impl ConflictAvoidance {
&self.belts
}
fn try_bruteforce(&self, candidate: &Candidate) -> Option<Vec<BruteForceEntry>> {
fn try_bruteforce(
&self,
candidate: &Candidate,
timeout: Option<Instant>,
) -> Option<Vec<BruteForceEntry>> {
let xrange = candidate.min.x as usize..=candidate.max.x as usize;
let yrange = candidate.min.y as usize..=candidate.max.y as usize;
@ -218,9 +225,9 @@ impl ConflictAvoidance {
{
let p = start_pos - offset;
// println!("Blocked {:?}", p);
if b.get_blocked(p.x as usize, p.y as usize) {
return None;
}
// if b.get_blocked(p.x as usize, p.y as usize) {
// return None;
// }
b.set_blocked(p.x as usize, p.y as usize, true);
}
@ -258,7 +265,7 @@ impl ConflictAvoidance {
let mut min_cost = f64::INFINITY;
let mut solutions = Vec::new();
while b.next_finish_state() {
while b.next_finish_state(timeout) {
// println!("{}", b);
// b.print();
let c = b.cost();
@ -286,7 +293,7 @@ impl ConflictAvoidance {
)
}
pub fn remove_conflict(&mut self) -> bool {
pub fn remove_conflict(&mut self, timeout: Option<Instant>) -> bool {
let mut conflicts: Map<usize> = Map::new(self.map.width, self.map.height);
for x in 0..self.map.width {
@ -347,7 +354,7 @@ impl ConflictAvoidance {
}
// dbg!(&candidates);
loop {
while timeout.is_some_and(|t| t < Instant::now()) {
candidates.sort_by_key(|c| -c.area());
// dbg!(&candidates);
let c = match candidates.pop() {
@ -359,7 +366,7 @@ impl ConflictAvoidance {
self.range = Some((c.min.x..=c.max.x, c.min.y..=c.max.y));
let result = self.try_bruteforce(&c);
let result = self.try_bruteforce(&c, timeout);
// dbg!(&solutions);
@ -442,10 +449,13 @@ impl ConflictAvoidance {
candidates.push(candidate);
}
}
false
}
pub fn remove_all_conflicts(&mut self) -> bool {
while self.remove_conflict() {}
pub fn remove_all_conflicts(&mut self, timeout: Option<Duration>) -> bool {
let end = timeout.map(|t| std::time::Instant::now() + t);
while self.remove_conflict(end) {}
let mut conflicts: Map<bool> = Map::new(self.map.width, self.map.height);