From 4da3924e246b1381a6cbaeccad75188d5d362c0e Mon Sep 17 00:00:00 2001 From: ImkoMarijnissen Date: Wed, 15 Jan 2025 13:55:50 +0100 Subject: [PATCH] fix: alternating brancher did not call the methods correctly --- .../branchers/alternating_brancher.rs | 50 +++++++++++++++---- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/pumpkin-solver/src/branching/branchers/alternating_brancher.rs b/pumpkin-solver/src/branching/branchers/alternating_brancher.rs index dd8f4b8c..e42a45d8 100644 --- a/pumpkin-solver/src/branching/branchers/alternating_brancher.rs +++ b/pumpkin-solver/src/branching/branchers/alternating_brancher.rs @@ -78,6 +78,19 @@ impl AlternatingBrancher { fn toggle_brancher(&mut self) { self.is_using_default_brancher = !self.is_using_default_brancher } + + /// Returns true if only the default strategy is used from now on and false otherwise. + /// + /// This is important if [`AlternatingStrategy::SwitchToDefaultAfterFirstSolution`] is used as + /// the strategy. + fn will_always_use_default(&self) -> bool { + match self.strategy { + AlternatingStrategy::SwitchToDefaultAfterFirstSolution => { + self.is_using_default_brancher + } + _ => false, + } + } } impl Brancher for AlternatingBrancher { @@ -97,13 +110,19 @@ impl Brancher for AlternatingBrancher { } fn on_appearance_in_conflict_predicate(&mut self, predicate: Predicate) { - self.other_brancher - .on_appearance_in_conflict_predicate(predicate) + self.default_brancher + .on_appearance_in_conflict_predicate(predicate); + if !self.will_always_use_default() { + self.other_brancher + .on_appearance_in_conflict_predicate(predicate) + } } fn on_conflict(&mut self) { - self.other_brancher.on_conflict(); - self.default_brancher.on_conflict() + self.default_brancher.on_conflict(); + if !self.will_always_use_default() { + self.other_brancher.on_conflict(); + } } fn on_solution(&mut self, solution: SolutionReference) { @@ -120,8 +139,8 @@ impl Brancher for AlternatingBrancher { } } AlternatingStrategy::SwitchToDefaultAfterFirstSolution => { - // Switch only the first time, not that `even_number_of_solutions` is initialised to - // true + // Switch only the first time, note that `even_number_of_solutions` is initialised + // to true if self.even_number_of_solutions { self.even_number_of_solutions = false; self.is_using_default_brancher = true; @@ -130,12 +149,17 @@ impl Brancher for AlternatingBrancher { _ => {} } - self.other_brancher.on_solution(solution); - self.default_brancher.on_solution(solution) + self.default_brancher.on_solution(solution); + if !self.will_always_use_default() { + self.other_brancher.on_solution(solution); + } } fn on_unassign_integer(&mut self, variable: DomainId, value: i32) { - self.other_brancher.on_unassign_integer(variable, value) + self.default_brancher.on_unassign_integer(variable, value); + if !self.will_always_use_default() { + self.other_brancher.on_unassign_integer(variable, value) + } } fn on_restart(&mut self) { @@ -173,12 +197,16 @@ impl Brancher for AlternatingBrancher { fn on_backtrack(&mut self) { self.default_brancher.on_backtrack(); - self.other_brancher.on_backtrack(); + if !self.will_always_use_default() { + self.other_brancher.on_backtrack(); + } } fn synchronise(&mut self, assignments: &Assignments) { self.default_brancher.synchronise(assignments); - self.other_brancher.synchronise(assignments); + if !self.will_always_use_default() { + self.other_brancher.synchronise(assignments); + } } }