diff --git a/pumpkin-crates/branching/src/branching/autonomous_search.rs b/pumpkin-crates/branching/src/branching/autonomous_search.rs index 86c3662b7..18d47cff7 100644 --- a/pumpkin-crates/branching/src/branching/autonomous_search.rs +++ b/pumpkin-crates/branching/src/branching/autonomous_search.rs @@ -1,3 +1,6 @@ +//! Defines [`AutonomousSearch`], a [`Brancher`] that selects predicates with VSIDS and their truth +//! values with solution-based phase saving. + use pumpkin_core::Solver; use pumpkin_core::branching::Brancher; use pumpkin_core::branching::BrancherEvent; @@ -323,7 +326,9 @@ impl Brancher for AutonomousSearch { } fn on_solution(&mut self, solution: SolutionReference) { - // We store the best known solution + // The previous best known solution is dropped before the new one is copied, so that only + // one copy is alive at a time. + self.best_known_solution = None; self.best_known_solution = Some(solution.into()); self.backup_brancher.on_solution(solution); } diff --git a/pumpkin-crates/core/src/basic_types/solution.rs b/pumpkin-crates/core/src/basic_types/solution.rs index 4dbdb583f..d65891a53 100644 --- a/pumpkin-crates/core/src/basic_types/solution.rs +++ b/pumpkin-crates/core/src/basic_types/solution.rs @@ -1,3 +1,6 @@ +//! Defines [`Solution`] and [`SolutionReference`], an owned and a borrowed assignment of values to +//! the variables, which both implement [`ProblemSolution`]. + use crate::engine::Assignments; use crate::engine::variables::DomainGeneratorIterator; use crate::engine::variables::DomainId; @@ -84,9 +87,7 @@ impl From for Solution { impl From> for Solution { fn from(value: SolutionReference) -> Self { - Self { - assignments: value.assignments.clone(), - } + value.assignments.clone().into() } } diff --git a/pumpkin-crates/core/src/branching/selection_context.rs b/pumpkin-crates/core/src/branching/selection_context.rs index 460f30ba9..c34d67f20 100644 --- a/pumpkin-crates/core/src/branching/selection_context.rs +++ b/pumpkin-crates/core/src/branching/selection_context.rs @@ -1,3 +1,6 @@ +//! Defines [`SelectionContext`], the view of the solver state that a [`Brancher`] uses to select +//! its next decision. + use std::fmt::Debug; use crate::basic_types::Random; diff --git a/pumpkin-crates/core/src/engine/cp/assignments.rs b/pumpkin-crates/core/src/engine/cp/assignments.rs index 30db10012..80c617c1e 100644 --- a/pumpkin-crates/core/src/engine/cp/assignments.rs +++ b/pumpkin-crates/core/src/engine/cp/assignments.rs @@ -1,3 +1,6 @@ +//! Defines [`Assignments`], the current domains of all integer variables together with the trail +//! of every change made to them, so that earlier domains can be queried and restored. + use crate::basic_types::Trail; use crate::containers::HashMap; use crate::containers::KeyedVec; diff --git a/pumpkin-crates/core/src/optimisation/linear_sat_unsat.rs b/pumpkin-crates/core/src/optimisation/linear_sat_unsat.rs index 743c6fff2..4e76f4b73 100644 --- a/pumpkin-crates/core/src/optimisation/linear_sat_unsat.rs +++ b/pumpkin-crates/core/src/optimisation/linear_sat_unsat.rs @@ -1,3 +1,6 @@ +//! Defines [`LinearSatUnsat`], the optimisation procedure that repeatedly searches for a solution +//! with a strictly better objective value than the best one found so far. + use std::ops::ControlFlow; use super::OptimisationProcedure; @@ -65,37 +68,27 @@ impl LinearSatUnsat { let best_objective_value = best_solution.get_integer_value(objective.clone()); - let conclusion = { - let solve_result = solver.satisfy_under_assumptions( - brancher, - termination, - resolver, - &[predicate![objective < best_objective_value]], - ); - - match solve_result { - SatisfactionResultUnderAssumptions::Satisfiable(satisfiable) => { - best_solution = satisfiable.solution().into(); - None - } - SatisfactionResultUnderAssumptions::UnsatisfiableUnderAssumptions(_) => { - Some(OptimisationResult::Optimal(best_solution.clone())) - } - SatisfactionResultUnderAssumptions::Unsatisfiable(_) => unreachable!( - "If the problem is unsatisfiable here, it would have been unsatisifable in the initial solve." - ), - SatisfactionResultUnderAssumptions::Unknown(_) => { - Some(OptimisationResult::Satisfiable(best_solution.clone())) - } + match solver.satisfy_under_assumptions( + brancher, + termination, + resolver, + &[predicate![objective < best_objective_value]], + ) { + SatisfactionResultUnderAssumptions::Satisfiable(satisfiable) => { + // The previous best solution is dropped before the new one is copied, so that + // only one copy is alive at a time. + drop(best_solution); + best_solution = satisfiable.solution().into(); } - }; - - match conclusion { - Some(OptimisationResult::Optimal(solution)) => { - return OptimisationResult::Optimal(solution); + SatisfactionResultUnderAssumptions::UnsatisfiableUnderAssumptions(_) => { + return OptimisationResult::Optimal(best_solution); + } + SatisfactionResultUnderAssumptions::Unsatisfiable(_) => unreachable!( + "If the problem is unsatisfiable here, it would have been unsatisfiable in the initial solve." + ), + SatisfactionResultUnderAssumptions::Unknown(_) => { + return OptimisationResult::Satisfiable(best_solution); } - Some(result) => return result, - None => {} } } }