From cb8f34c3b395d8ef3db02b4527a3632d1953f988 Mon Sep 17 00:00:00 2001 From: Imko Marijnissen Date: Thu, 3 Dec 2026 12:13:54 +0100 Subject: [PATCH 1/2] fix: do not store trail + do not store two solutions at the same time --- .../src/branching/autonomous_search.rs | 7 ++- .../core/src/basic_types/solution.rs | 8 +-- .../core/src/branching/selection_context.rs | 5 +- .../core/src/engine/cp/assignments.rs | 40 +++++++++++++++ .../core/src/optimisation/linear_sat_unsat.rs | 51 ++++++++----------- 5 files changed, 77 insertions(+), 34 deletions(-) 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..ab932e3e5 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; @@ -83,10 +86,9 @@ impl From for Solution { } impl From> for Solution { + /// Copies the domains of the solution, without the trail. fn from(value: SolutionReference) -> Self { - Self { - assignments: value.assignments.clone(), - } + value.assignments.clone_without_trail().into() } } diff --git a/pumpkin-crates/core/src/branching/selection_context.rs b/pumpkin-crates/core/src/branching/selection_context.rs index 460f30ba9..0930e5533 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; @@ -82,6 +85,6 @@ impl<'a> SelectionContext<'a> { /// Returns a [`Solution`] containing the values which are currently assigned. pub fn solution(&self) -> Solution { - Solution::from(self.state.assignments.clone()) + Solution::from(self.state.assignments.clone_without_trail()) } } diff --git a/pumpkin-crates/core/src/engine/cp/assignments.rs b/pumpkin-crates/core/src/engine/cp/assignments.rs index 30db10012..2da7c3436 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; @@ -216,6 +219,21 @@ impl Assignments { new_assignment } + /// Returns a copy of `self` without the trail. + /// + /// The copy answers every query that the domains answer by themselves, such as + /// [`Self::get_assigned_value`], [`Self::evaluate_predicate`] and the queries at a trail + /// position. + /// A query that reads the trail, such as [`Self::get_trail_entry`], panics on the copy. + pub(crate) fn clone_without_trail(&self) -> Assignments { + Assignments { + trail: Default::default(), + bounds: self.bounds.clone(), + domains: self.domains.clone(), + pruned_values: self.pruned_values, + } + } + pub(crate) fn is_initial_bound(&self, predicate: Predicate) -> bool { let domain_id = predicate.get_domain(); @@ -1471,6 +1489,28 @@ mod tests { use super::*; use crate::engine::notifications::DomainEvent; + #[test] + fn clone_without_trail_keeps_the_domains() { + let mut notification_engine = NotificationEngine::test_default(); + let mut assignments = Assignments::default(); + let x = assignments.grow(1, 10); + notification_engine.grow(); + + assignments.new_checkpoint(); + for predicate in [predicate!(x != 1), predicate!(x != 5)] { + let _ = assignments + .post_predicate(predicate, None, &mut notification_engine) + .expect("non-empty domain"); + } + + let copy = assignments.clone_without_trail(); + + assert_eq!(copy.get_lower_bound(x), 2); + assert_eq!(copy.get_upper_bound(x), 10); + assert!(!copy.is_value_in_domain(x, 5)); + assert_eq!(copy.num_trail_entries(), 0); + } + #[test] fn jump_in_bound_change_lower_and_upper_bound_event_backtrack() { let mut notification_engine = NotificationEngine::test_default(); 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 => {} } } } From ef54d68b56d058693b4c846ea3dbf65433d56f49 Mon Sep 17 00:00:00 2001 From: Imko Marijnissen Date: Thu, 3 Dec 2026 12:27:24 +0100 Subject: [PATCH 2/2] refactor: do not remove trail when cloning to minimise changes --- .../core/src/basic_types/solution.rs | 3 +- .../core/src/branching/selection_context.rs | 2 +- .../core/src/engine/cp/assignments.rs | 37 ------------------- 3 files changed, 2 insertions(+), 40 deletions(-) diff --git a/pumpkin-crates/core/src/basic_types/solution.rs b/pumpkin-crates/core/src/basic_types/solution.rs index ab932e3e5..d65891a53 100644 --- a/pumpkin-crates/core/src/basic_types/solution.rs +++ b/pumpkin-crates/core/src/basic_types/solution.rs @@ -86,9 +86,8 @@ impl From for Solution { } impl From> for Solution { - /// Copies the domains of the solution, without the trail. fn from(value: SolutionReference) -> Self { - value.assignments.clone_without_trail().into() + 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 0930e5533..c34d67f20 100644 --- a/pumpkin-crates/core/src/branching/selection_context.rs +++ b/pumpkin-crates/core/src/branching/selection_context.rs @@ -85,6 +85,6 @@ impl<'a> SelectionContext<'a> { /// Returns a [`Solution`] containing the values which are currently assigned. pub fn solution(&self) -> Solution { - Solution::from(self.state.assignments.clone_without_trail()) + Solution::from(self.state.assignments.clone()) } } diff --git a/pumpkin-crates/core/src/engine/cp/assignments.rs b/pumpkin-crates/core/src/engine/cp/assignments.rs index 2da7c3436..80c617c1e 100644 --- a/pumpkin-crates/core/src/engine/cp/assignments.rs +++ b/pumpkin-crates/core/src/engine/cp/assignments.rs @@ -219,21 +219,6 @@ impl Assignments { new_assignment } - /// Returns a copy of `self` without the trail. - /// - /// The copy answers every query that the domains answer by themselves, such as - /// [`Self::get_assigned_value`], [`Self::evaluate_predicate`] and the queries at a trail - /// position. - /// A query that reads the trail, such as [`Self::get_trail_entry`], panics on the copy. - pub(crate) fn clone_without_trail(&self) -> Assignments { - Assignments { - trail: Default::default(), - bounds: self.bounds.clone(), - domains: self.domains.clone(), - pruned_values: self.pruned_values, - } - } - pub(crate) fn is_initial_bound(&self, predicate: Predicate) -> bool { let domain_id = predicate.get_domain(); @@ -1489,28 +1474,6 @@ mod tests { use super::*; use crate::engine::notifications::DomainEvent; - #[test] - fn clone_without_trail_keeps_the_domains() { - let mut notification_engine = NotificationEngine::test_default(); - let mut assignments = Assignments::default(); - let x = assignments.grow(1, 10); - notification_engine.grow(); - - assignments.new_checkpoint(); - for predicate in [predicate!(x != 1), predicate!(x != 5)] { - let _ = assignments - .post_predicate(predicate, None, &mut notification_engine) - .expect("non-empty domain"); - } - - let copy = assignments.clone_without_trail(); - - assert_eq!(copy.get_lower_bound(x), 2); - assert_eq!(copy.get_upper_bound(x), 10); - assert!(!copy.is_value_in_domain(x, 5)); - assert_eq!(copy.num_trail_entries(), 0); - } - #[test] fn jump_in_bound_change_lower_and_upper_bound_event_backtrack() { let mut notification_engine = NotificationEngine::test_default();