Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
bf9e1f0
Move propagator files into their own folder
kstrauch94 Nov 7, 2026
c91c5eb
First implementation of reasoning modes propagator
kstrauch94 Nov 8, 2026
cb12272
Implement nogoods for Intersection and Union modes
kstrauch94 Nov 9, 2026
247eb21
Delete code for prescense literals
kstrauch94 Nov 10, 2026
fb431fe
Add nogood implementation for infumum mode
kstrauch94 Nov 10, 2026
290d8d0
Use on model to add nogods
kstrauch94 Nov 11, 2026
2082098
Output for reaosning mode follow model_<mode>/2 format
kstrauch94 Nov 11, 2026
86a6609
Add testing for reasoning modes propagator
kstrauch94 Nov 11, 2026
5f49e1d
Fix issue when there were no tracked variables
kstrauch94 Nov 11, 2026
b8d6c83
Fix bug when no variables could get values
kstrauch94 Nov 14, 2026
360c24a
Add testing framework for new reasoning modes
kstrauch94 Nov 15, 2026
51e4266
Fix bug and typo
kstrauch94 Nov 15, 2026
02a7654
delete test since file should contain nothing
kstrauch94 Nov 15, 2026
648c59f
Add xfails for propagator and compile2
kstrauch94 Nov 15, 2026
eb369c8
delete rogue import
kstrauch94 Nov 15, 2026
61235b9
top and bot are in an enum
kstrauch94 Nov 15, 2026
44a71db
use logical OR and AND for supremum and infimum when both values are …
kstrauch94 Nov 15, 2026
923e273
add more tests for new modes
kstrauch94 Nov 15, 2026
df3ac15
extend bool int example
kstrauch94 Nov 16, 2026
513bc2a
only track user variables in new reasoning modes
kstrauch94 Nov 16, 2026
6a904bc
initial fix to sets within sets for new reasoning modes
kstrauch94 Nov 16, 2026
9da0eb5
flat ch xfails reasoning mode tests due to new modes
kstrauch94 Nov 16, 2026
5799eab
improve docstrings
kstrauch94 Nov 17, 2026
660bbc7
Can now use select_rm/2 to select regular atoms for the reasoning mod…
kstrauch94 Nov 19, 2026
47a6eae
small fix
kstrauch94 Nov 19, 2026
e9c3443
ignore regular atom if name matches a ch variable's name
kstrauch94 Nov 19, 2026
005c3c0
add deeply nested sets test
kstrauch94 Nov 19, 2026
6bb5e70
add new reasoning mode tests to unsupported
kstrauch94 Nov 20, 2026
838709c
Add some failing tests for reasoning modes
AbdallahS Nov 22, 2026
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
7 changes: 6 additions & 1 deletion .github/copilot-instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,12 @@ src/
│ │ ├── set.lp, multimap.lp # Data structures
│ │ ├── execution.lp, conditionals.lp # Control flow
│ │ └── propagator.lp, pythonHelper.lp # Solver integration
│ ├── propagator.py # Constraint handler propagator
│ ├── propagator/ # Constraint handler propagator subsystem
│ │ ├── handler.py # Main propagator implementation
│ │ ├── model_builder.py
│ │ ├── optimization_modes.py
│ │ ├── reasoning_modes.py
│ │ ├── variable_loader.py
│ ├── evaluator.py # Expression evaluator
│ ├── schemas/ # Data schemas (Clorm-based)
│ └── utils/ # Utility modules
Expand Down
6 changes: 4 additions & 2 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,10 @@ The supported suffixes are:
last model respectively.
- `none`: atom must occur in no model; it does not assert that the program is
unsatisfiable.
- `brave` and `cautious`: atom must occur in the final model with the matching
enumeration mode.
- `brave` `cautious`, `union`, `intersection`, `supremum`, and `infimum`: atom
must occur in the final model with the matching enumeration mode.
- `union.none`, `intersection.none`, `supremum.none`, and `infimum.none`: atom
must be absent from the final model with the matching reasoning mode.
- `optall`, `optany`, and `optnone`: atom must occur in every, any, or no
optimal model respectively.
- `stats`: one `key=value` entry per line. `models=<count>` asserts the exact
Expand Down
2 changes: 1 addition & 1 deletion src/constraint_handler/engine.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from dataclasses import dataclass, field

from constraint_handler.PropagatorConstants import PROPAGATOR_CHECK_MODE_STR
from constraint_handler.propagator.PropagatorConstants import PROPAGATOR_CHECK_MODE_STR


@dataclass(frozen=True)
Expand Down
12 changes: 10 additions & 2 deletions src/constraint_handler/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import constraint_handler.solver_environment as solver_environment
import flat_ch.main as flat_main
from constraint_handler.engine import Engine, compile
from constraint_handler.reasoning_modes.reasoning_modes_propagator import ReasoningMode, ReasoningModesPropagator
from constraint_handler.schemas.type_ import BaseType

module_main = [
Expand Down Expand Up @@ -176,6 +177,7 @@ def add_to_control(
environment=None,
_environment_ids=dict(),
engine: Engine = compile,
reasoning_mode: ReasoningMode = ReasoningMode.STANDARD,
):
"""Adds encoding logic to the provided Control instance. The environment argumennt specifies the locals used in the python statements and expressions."""
if engine.name == "fch":
Expand Down Expand Up @@ -205,14 +207,17 @@ def add_to_control(
_environment_ids[eid] = idx
ctrl.add(f"main_solverIdentifier({idx}).")
ctrl.add(engine.program())
setup_propagator(ctrl)
setup_propagator(ctrl, reasoning_mode)


def setup_propagator(ctrl: clingo.Control):
def setup_propagator(ctrl: clingo.Control, reasoning_mode: ReasoningMode):
prop = propagator.ConstraintHandlerPropagator()
post_prop = post_processor.OptimizePostProcessingPropagator()

reasoning_mode_prop = ReasoningModesPropagator(reasoning_mode)

ctrl.register_propagator(prop)
ctrl.register_propagator(reasoning_mode_prop)
ctrl.register_propagator(post_prop)
prop.get_configuration(ctrl)
original_solve = ctrl.solve
Expand All @@ -222,13 +227,16 @@ def om(model):
if prop.on_model(model) == False:
return False
post_processor.set_optimize_valuation(post_prop, model)
reasoning_mode_prop.on_model(model)
post_processor.extend_with_reasoning_mode_atoms(reasoning_mode_prop, model)
if on_model is not None:
return on_model(model)

return om

def new_solve(*args, **kwargs):
post_prop.reset_optimize_value_symbols()
reasoning_mode_prop.prepare(ctrl)
if len(args) > 1:
args = (args[0], combine_on_model(args[1])) + args[2:]
elif "on_model" in kwargs:
Expand Down
41 changes: 40 additions & 1 deletion src/constraint_handler/post_processor.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
from __future__ import annotations

from collections import defaultdict
from typing import Mapping
from collections.abc import Mapping
from typing import Any

import clingo

import constraint_handler.evaluator as evaluator
import constraint_handler.myClorm as myClorm
import constraint_handler.schemas.result as result
import constraint_handler.schemas.warning as warning
from constraint_handler import multimap
from constraint_handler.reasoning_modes.reasoning_modes_propagator import (
ReasoningMode,
ReasoningModesPropagator,
_LatticeBound,
)


class OptimizePostProcessingPropagator(clingo.Propagator):
Expand Down Expand Up @@ -137,3 +144,35 @@ def set_optimize_valuation(propagator: OptimizePostProcessingPropagator, model)

if model.optimality_proven:
propagator._cached_optimize_value_symbols = optimize_value_symbols.copy()


def extend_with_reasoning_mode_atoms(propagator: ReasoningModesPropagator, model) -> None:
if propagator.reasoning_mode is ReasoningMode.STANDARD:
return

symbols = set()
for variable, values in propagator.reasoning_strategy.rm_model.items():
if propagator.is_non_constraint_handler_variable(variable):
if any(value is True for value in values):
symbols.add(clingo.Function(f"model_{propagator.reasoning_mode.name.lower()}", [variable]))
continue
for value in values:
if isinstance(value, multimap.Multimap):
value_symbol = _multimap_symbol(value)
else:
if isinstance(value, _LatticeBound):
value = value.value
reduced_value, _ = evaluator.reducedExpr(value)
value_symbol = myClorm.pytocl(reduced_value)
symbols.add(clingo.Function(f"model_{propagator.reasoning_mode.name.lower()}", [variable, value_symbol]))
model.extend(symbols)


def _multimap_symbol(value: multimap.Multimap[Any, Any]) -> clingo.Symbol:
entries = []
for key, values in value.items():
reduced_key, _ = evaluator.reducedExpr(key)
reduced_values = sorted(myClorm.pytocl(evaluator.reducedExpr(item)[0]) for item in values)
entries.append(clingo.Function("", [myClorm.pytocl(reduced_key), myClorm.nest(reduced_values)]))

return clingo.Function("multimap", [myClorm.nest(sorted(entries)), clingo.Function("", [])])
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class EvaluationResult(enum.Enum):
INFER = "__infer__"


class ReasoningMode(enum.Enum):
class PropagatorReasoningMode(enum.Enum):
STANDARD = "standard"
BRAVE = "brave"
CAUTIOUS = "cautious"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import clingo

from constraint_handler import evaluator, multimap, myClorm
from constraint_handler.PropagatorConstants import (
from constraint_handler.propagator.PropagatorConstants import (
DEFAULT_DECISION_LEVEL,
EvaluationResult,
ValueStatus,
Expand Down
5 changes: 5 additions & 0 deletions src/constraint_handler/propagator/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Propagator implementation and supporting modules."""

from .handler import ConstraintHandlerPropagator

__all__ = ["ConstraintHandlerPropagator"]
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
import clingo

import constraint_handler.schemas.propagator_atom as prop_atom
from constraint_handler import evaluator, myClorm, variable_loader
from constraint_handler.model_builder import ModelBuilder
from constraint_handler.optimization_modes import OptimizationModeHandler
from constraint_handler.PropagatorConstants import (
from constraint_handler import evaluator, myClorm
from constraint_handler.propagator import variable_loader
from constraint_handler.propagator.model_builder import ModelBuilder
from constraint_handler.propagator.optimization_modes import OptimizationModeHandler
from constraint_handler.propagator.PropagatorConstants import (
OTHER_ENGINE_VAR_NAME,
PROPAGATOR_CHECK_MODE_ATOM,
EvaluationResult,
ValueStatus,
propagator_warning_t,
)
from constraint_handler.PropagatorVariables import (
from constraint_handler.propagator.PropagatorVariables import (
EvaluateVariable,
Evaluations,
OptimizationHandler,
Expand All @@ -23,7 +24,7 @@
VariableValue,
is_evaluation_possible,
)
from constraint_handler.reasoning_modes import ReasoningModeHandler
from constraint_handler.propagator.reasoning_modes import ReasoningModeHandler
from constraint_handler.schemas import expression, internal, result, warning


Expand All @@ -32,7 +33,7 @@ class ConstraintHandlerPropagator(clingo.Propagator):
Propagator that evaluates constraint_handler variables during solving.

The propagator maintains a mapping from clingo symbols and solver literals to
variable objects (see `constraint_handler.PropagatorVariables`). During
variable objects (see `constraint_handler.propagator.PropagatorVariables`). During
propagation/check it evaluates affected variables, checks ensure constraints,
and can add nogoods for conflicts or for pruning based on optimization or reasoning mode(brave/cautious).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import clingo

from constraint_handler import evaluator, multimap
from constraint_handler.PropagatorConstants import OTHER_ENGINE_VAR_NAME, ValueStatus
from constraint_handler.PropagatorVariables import BoolEvaluateVariable, EvaluateVariable, VariableType
from constraint_handler.propagator.PropagatorConstants import OTHER_ENGINE_VAR_NAME, ValueStatus
from constraint_handler.propagator.PropagatorVariables import BoolEvaluateVariable, EvaluateVariable, VariableType
from constraint_handler.schemas import expression, result, warning


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

import constraint_handler.schemas.propagator_atom as prop_atom
from constraint_handler import myClorm
from constraint_handler.PropagatorConstants import (
from constraint_handler.propagator.PropagatorConstants import (
OPTIMIZATION_HELPER_PROGRAM,
OPTIMIZATION_STAGE_ATOM,
OptimizationStrength,
propagator_warning_t,
)
from constraint_handler.PropagatorVariables import OptimizationHandler, VariableValue
from constraint_handler.propagator.PropagatorVariables import OptimizationHandler, VariableValue
from constraint_handler.schemas import expression, warning


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
import clingo
from clingo import Symbol

from constraint_handler.PropagatorConstants import REASONING_MODE_PROGRAM, REASONING_STAGE_ATOM, ReasoningMode
from constraint_handler.PropagatorVariables import BoolEvaluateVariable, VariableType
from constraint_handler.propagator.PropagatorConstants import (
REASONING_MODE_PROGRAM,
REASONING_STAGE_ATOM,
PropagatorReasoningMode,
)
from constraint_handler.propagator.PropagatorVariables import BoolEvaluateVariable, VariableType
from constraint_handler.schemas import result, warning


Expand All @@ -27,7 +31,7 @@ class _ReasoningStrategy(ABC):
accumulated_model: The accumulated model across model processing.
"""

mode = ReasoningMode.STANDARD
mode = PropagatorReasoningMode.STANDARD

def __init__(self) -> None:
self.stage_literals: dict[int, int] = {1: -1, 2: -1}
Expand Down Expand Up @@ -201,7 +205,7 @@ class StandardReasoningStrategy(_ReasoningStrategy):


class BraveReasoningStrategy(_StagedReasoningStrategy):
mode = ReasoningMode.BRAVE
mode = PropagatorReasoningMode.BRAVE

def accumulate_model(self, previous_model, current_model):
if previous_model is None:
Expand Down Expand Up @@ -244,7 +248,7 @@ def _variable_result_atoms(variables):


class CautiousReasoningStrategy(_StagedReasoningStrategy):
mode = ReasoningMode.CAUTIOUS
mode = PropagatorReasoningMode.CAUTIOUS

def accumulate_model(self, previous_model, current_model):
if previous_model is None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

import constraint_handler.schemas.propagator_atom as prop_atom
from constraint_handler import myClorm
from constraint_handler.PropagatorConstants import OTHER_ENGINE_VAR_NAME, propagator_warning_t
from constraint_handler.PropagatorVariables import (
from constraint_handler.propagator.PropagatorConstants import OTHER_ENGINE_VAR_NAME, propagator_warning_t
from constraint_handler.propagator.PropagatorVariables import (
BoolEvaluateVariable,
DictVariable,
EvaluateVariable,
Expand Down
Loading
Loading