diff --git a/src/solver/mod.rs b/src/solver/mod.rs index 6a236a0..c536b26 100644 --- a/src/solver/mod.rs +++ b/src/solver/mod.rs @@ -70,33 +70,40 @@ impl Solver { } pub fn solve(&mut self, solver_limit: Option) -> Result<(), WaveError> { - let now = Instant::now(); + let start_time = Instant::now(); self.display(ui::DisplayMode::Full); - let mut propagation_counter: usize = 0; - let mut collapse_counter: usize = 0; + let mut propagation_counter: (usize, std::time::Duration) = (0, std::time::Duration::ZERO); + let mut collapse_counter: (usize, std::time::Duration) = (0, std::time::Duration::ZERO); + let mut backtrack_counter: (usize, std::time::Duration) = (0, std::time::Duration::ZERO); println!("# started"); self.debug("--------"); while self.history.len() < self.size * self.size { self.debug(&format!("\n## while, h={}/{}", self.last_move_index, self.history.len())); while self.last_move_index < self.history.len() && self.history.len() < self.size * self.size { - let mut backtrack = 0; + let mut need_backtrack = false; + + let propagation_start = Instant::now(); match self.propagate_collapse() { Ok(_) => {}, Err(reason) => { if let WaveError::Contradiction = reason { - backtrack = 1; + need_backtrack = true; } else { return Err(reason) } } }; - while backtrack > 0 { - backtrack -=1; + propagation_counter.0 += 1; + propagation_counter.1 += propagation_start.elapsed(); + + if need_backtrack { + let backtrack_start = Instant::now(); self.backtrack()?; + backtrack_counter.0 += 1; + backtrack_counter.1 += backtrack_start.elapsed(); } - propagation_counter += 1; } if self.grid_display { @@ -107,21 +114,34 @@ impl Solver { } } - self.collapse()?; - collapse_counter += 1; + let collapse_start = Instant::now(); + self.collapse()?; + collapse_counter.0 += 1; + collapse_counter.1 += collapse_start.elapsed(); if !self.debug_display && !self.grid_display { self.progress_bar(); } if let Some(limit) = solver_limit { - if collapse_counter >= limit { + if collapse_counter.0 >= limit { break; } } } self.debug("--------"); - let elapsed = now.elapsed(); - println!("\n# finished in {} propagations ({} forced collapse), {:.2?} ({:.2?}/propagation)", propagation_counter, collapse_counter, elapsed, elapsed/(propagation_counter as u32)); + let total_elapsed = start_time.elapsed(); + let propagation_percentage = (propagation_counter.1.as_secs_f64() / total_elapsed.as_secs_f64()) * 100.0; + let collapse_percentage = (collapse_counter.1.as_secs_f64() / total_elapsed.as_secs_f64()) * 100.0; + let backtrack_percentage = (backtrack_counter.1.as_secs_f64() / total_elapsed.as_secs_f64()) * 100.0; + + println!("\n# finished in {:.2?} ({:.2?}% propagation, {:.2?}% forced collapse, {:.2?}% backtrack)", + total_elapsed, propagation_percentage, collapse_percentage, backtrack_percentage + ); + println!("- {} propagations ({:.2?}), {} forced collapse ({:.2?}), {} backtrack ({:.2?})", + propagation_counter.0, (propagation_counter.1.checked_div(propagation_counter.0 as u32)).unwrap_or(std::time::Duration::ZERO), + collapse_counter.0, (collapse_counter.1.checked_div(collapse_counter.0 as u32)).unwrap_or(std::time::Duration::ZERO), + backtrack_counter.0, (backtrack_counter.1.checked_div(backtrack_counter.0 as u32)).unwrap_or(std::time::Duration::ZERO) + ); if !self.grid_display || self.debug_display { self.display(ui::DisplayMode::Full); } diff --git a/todo b/todo deleted file mode 100644 index c9a2301..0000000 --- a/todo +++ /dev/null @@ -1,9 +0,0 @@ -more check for impossible - - -cargo run 4 --grid --debug --ask --norand --limit 220 ---d-fc-g - -error : -when backtracking, always adding the state back is wrong -(maybe it was not there)