From 2885b8f8afc9a44a3897c86beac83a6e1cefba31 Mon Sep 17 00:00:00 2001 From: Abdallah Saffidine Date: Thu, 5 Nov 2026 15:20:31 +1100 Subject: [PATCH 1/3] Add repeated solve test --- tests/test_reasoning_mode.py | 40 ++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 tests/test_reasoning_mode.py diff --git a/tests/test_reasoning_mode.py b/tests/test_reasoning_mode.py new file mode 100644 index 00000000..e9481d59 --- /dev/null +++ b/tests/test_reasoning_mode.py @@ -0,0 +1,40 @@ +import pytest +from clingo import Control, parse_term + +import constraint_handler +import constraint_handler.engine as engine + + +@pytest.mark.parametrize("reasoning_mode", ["brave", "cautious"]) +@pytest.mark.parametrize( + "selected_engine", + [ + pytest.param( + engine.propagator, + marks=pytest.mark.xfail( + strict=True, raises=AssertionError, reason="Repeated reasoning-mode solves lose propagator set values" + ), + id="propagator", + ), + pytest.param(engine.propagator_check, id="propagator-check"), + ], +) +def test_repeated_solve_set_values(selected_engine, reasoning_mode): + control = Control(["0", "--heuristic=Domain", f"--enum-mode={reasoning_mode}"]) + constraint_handler.add_to_control(control, engine=selected_engine) + control.add(""" + variable_declare(s,set). + set_assign(s,val(int,1)). + """) + control.ground() + expected = { + parse_term("value(s,ref(set,variable(s)))"), + parse_term("set_value(s,val(int,1))"), + } + + for solve_number in range(1, 3): + final_values = set() + with control.solve(yield_=True) as handle: + for model in handle: + final_values = set(constraint_handler.find_values(model)) + assert expected <= final_values, f"Solve {solve_number} lost set values: {final_values}" \ No newline at end of file From 37c19b7938438fabf8ff4b06aaf1041b999fe63c Mon Sep 17 00:00:00 2001 From: Abdallah Saffidine Date: Thu, 5 Nov 2026 15:29:06 +1100 Subject: [PATCH 2/3] Fix formatting --- tests/test_reasoning_mode.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_reasoning_mode.py b/tests/test_reasoning_mode.py index e9481d59..b73f5ca9 100644 --- a/tests/test_reasoning_mode.py +++ b/tests/test_reasoning_mode.py @@ -37,4 +37,4 @@ def test_repeated_solve_set_values(selected_engine, reasoning_mode): with control.solve(yield_=True) as handle: for model in handle: final_values = set(constraint_handler.find_values(model)) - assert expected <= final_values, f"Solve {solve_number} lost set values: {final_values}" \ No newline at end of file + assert expected <= final_values, f"Solve {solve_number} lost set values: {final_values}" From e05e3d8991c9381297bc108f188ee16dfc60d048 Mon Sep 17 00:00:00 2001 From: Klaus Strauch Date: Thu, 19 Nov 2026 01:34:03 +0100 Subject: [PATCH 3/3] Fix reasoning modes failing after first solve --- src/constraint_handler/propagator.py | 32 ++++++++++++++++++++++------ tests/test_reasoning_mode.py | 3 --- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/constraint_handler/propagator.py b/src/constraint_handler/propagator.py index 4a2ad5cc..0690cbe1 100644 --- a/src/constraint_handler/propagator.py +++ b/src/constraint_handler/propagator.py @@ -156,6 +156,10 @@ def init(self, init: clingo.PropagateInit) -> None: self.add_watches_to_ctl(init) + # run propagate with literal 1 + # We do this explicitly here since it does not happen on solve calls after the first one + self.propagate(init, [1]) + def add_watch(self, lit: int) -> None: """ Add a watch literal to the set of watched literals if check_only is disabled @@ -290,19 +294,19 @@ def evaluate_model(self, ctl: clingo.PropagateControl) -> bool: return self.add_nogoods_from_queue(ctl) return False - def add_nogoods_from_queue(self, ctl: clingo.PropagateControl) -> bool: + def add_nogoods_from_queue(self, ctl: clingo.PropagateControl | clingo.PropagateInit) -> bool: """ Add queued nogoods until the queue is empty or clingo refuses one. Args: - ctl: Clingo PropagateControl object. + ctl: Clingo propagation control or initialization object. Returns: bool: True if adding a nogood failed (propagation must stop). """ while len(self.nogood_queue) > 0: ng = self.nogood_queue.pop(0) - if not ctl.add_nogood(ng, lock=True): + if not self.add_nogood(ctl, ng, lock=True): return True return False @@ -327,14 +331,18 @@ def check_shared_values(self, ctl: clingo.PropagateControl) -> None: for var in self.shared_values: var.evaluate(self.evaluations, ctl, self.environment) - def propagate(self, control: clingo.PropagateControl, changes: Sequence[int]) -> None: + def propagate( + self, + control: clingo.PropagateControl | clingo.PropagateInit, + changes: Sequence[int], + ) -> None: """ Propagate after a solver assignment change. Checks which variables are affected by the change, evaluates them, and applies optimization pruning if enabled. Args: - control: Clingo propagation control. + control: Clingo propagation control or initialization object. changes: Sequence of (signed) solver literals that changed. """ # print("Propagating...", changes) @@ -382,9 +390,19 @@ def evaluate_optimization_sum(self, ctl: clingo.PropagateControl) -> bool: self.add_nogood_for_variable(ctl, self.optimization.sums, extra_literals=extra_literals) return True + @staticmethod + def add_nogood( + ctl: clingo.PropagateControl | clingo.PropagateInit, + literals: Iterable[int], + lock: bool = False, + ) -> bool: + if isinstance(ctl, clingo.PropagateInit): + return ctl.add_clause([-literal for literal in literals]) + return ctl.add_nogood(literals, lock=lock) + def add_nogood_for_variable( self, - ctl: clingo.PropagateControl, + ctl: clingo.PropagateControl | clingo.PropagateInit, var: VariableType | OptimizationHandler, extra_literals: Iterable[int] | None = None, conflict: bool = True, @@ -410,7 +428,7 @@ def add_nogood_for_variable( ng: set[int] = self.get_reasons(var) if extra_literals: ng = ng.union(extra_literals) - prop_stop = ctl.add_nogood(ng, lock=True) + prop_stop = self.add_nogood(ctl, ng, lock=True) if conflict and prop_stop: assert ( False diff --git a/tests/test_reasoning_mode.py b/tests/test_reasoning_mode.py index b73f5ca9..0b6afccc 100644 --- a/tests/test_reasoning_mode.py +++ b/tests/test_reasoning_mode.py @@ -11,9 +11,6 @@ [ pytest.param( engine.propagator, - marks=pytest.mark.xfail( - strict=True, raises=AssertionError, reason="Repeated reasoning-mode solves lose propagator set values" - ), id="propagator", ), pytest.param(engine.propagator_check, id="propagator-check"),