Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions src/constraint_handler/propagator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
37 changes: 37 additions & 0 deletions tests/test_reasoning_mode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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,
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}"
Loading