Skip to content

Commit 7d81d4b

Browse files
committed
Remove optional-constraint toggling and APIs
Remove support for optional/redundant constraints and the related toggling API. Constraint no longer has is_redundant or enabled attributes; add_constraint and add_constraints signatures no longer accept is_redundant/enabled. Deleted add_optional_constraint, add_assumption, set_constraint_enabled, enable_constraint, disable_constraint, and the PymzmConstraintNotInModel exception. Adjusted add_function_call/add_predicate_call to drop redundancy flags and changed Model.generate to include all constraints (removed enabled filtering). Updated tests accordingly and removed the optional-constraints test file.
1 parent a6f7fc2 commit 7d81d4b

5 files changed

Lines changed: 8 additions & 152 deletions

File tree

‎src/pymzm/constraint.py‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,6 @@ def __init__(
9494
cstr: ExpressionBool,
9595
ctype: str = CTYPE_NORMAL,
9696
annotation: str = None,
97-
is_redundant=False,
98-
enabled=True,
9997
):
10098
self.cstr = cstr
10199
if not isinstance(self.cstr, (ExpressionBool, bool, str)):
@@ -110,9 +108,6 @@ def __init__(
110108
if self.annotation not in AnnotationConstraint.ANNOTATIONS:
111109
raise PymzmInvalidConstraintAnnotation("annotation", self.annotation)
112110

113-
self.is_redundant = is_redundant
114-
self.enabled = enabled
115-
116111
def __str__(self):
117112
return self.cstr
118113

@@ -121,10 +116,7 @@ def _to_mz(self):
121116
if self.annotation is not None:
122117
annotation_suffix = f" :: {self.annotation}"
123118

124-
if self.is_redundant:
125-
return f"constraint redundant_constraint({self.cstr}){annotation_suffix};\n"
126-
else:
127-
return f"constraint {self.cstr}{annotation_suffix};\n"
119+
return f"constraint {self.cstr}{annotation_suffix};\n"
128120

129121
@staticmethod
130122
def _from_global_constraint(func: str, ctype: str, *args) -> "Constraint":

‎src/pymzm/exceptions.py‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -149,17 +149,6 @@ def __str__(self):
149149
)
150150

151151

152-
class PymzmConstraintNotInModel(PymzmModelStateError):
153-
def __init__(self, constraint):
154-
self.constraint = constraint
155-
156-
def __str__(self):
157-
return (
158-
f"Constraint {self.constraint!r} is not registered in this model. "
159-
"Only constraints added via model.add_constraint(...) can be toggled."
160-
)
161-
162-
163152
class PymzmVariableTypeError(PymzmArgumentError):
164153
def __init__(self, argname, expected_vtype, actual_vtype):
165154
self.expected_vtype = expected_vtype

‎src/pymzm/model.py‎

Lines changed: 7 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -513,60 +513,20 @@ def add_variables(
513513
def add_constraint(
514514
self,
515515
constraint: ConstraintInput,
516-
is_redundant: bool = False,
517-
enabled: bool = True,
518516
) -> Constraint:
519517
if isinstance(constraint, Constraint):
520-
constraint.is_redundant = is_redundant
521-
constraint.enabled = enabled
522518
if constraint.ctype != Constraint.CTYPE_NORMAL:
523519
self.global_constraints.add(constraint.ctype)
524520

525521
elif isinstance(constraint, ExpressionBool):
526-
constraint = Constraint(
527-
constraint.name, is_redundant=is_redundant, enabled=enabled
528-
)
522+
constraint = Constraint(constraint.name)
529523

530524
else:
531525
raise PymzmInvalidConstraintType("constraint", type(constraint).__name__)
532526

533527
self.constraints.append(constraint)
534528
return constraint
535529

536-
def add_optional_constraint(
537-
self,
538-
constraint: ConstraintInput,
539-
enabled: bool = False,
540-
is_redundant: bool = False,
541-
) -> Constraint:
542-
return self.add_constraint(
543-
constraint, is_redundant=is_redundant, enabled=enabled
544-
)
545-
546-
def add_assumption(
547-
self,
548-
constraint: ConstraintInput,
549-
enabled: bool = True,
550-
is_redundant: bool = False,
551-
) -> Constraint:
552-
return self.add_optional_constraint(
553-
constraint, enabled=enabled, is_redundant=is_redundant
554-
)
555-
556-
def set_constraint_enabled(
557-
self, constraint: Constraint, enabled: bool = True
558-
) -> Constraint:
559-
if constraint not in self.constraints:
560-
raise PymzmConstraintNotInModel(constraint)
561-
constraint.enabled = enabled
562-
return constraint
563-
564-
def enable_constraint(self, constraint: Constraint) -> Constraint:
565-
return self.set_constraint_enabled(constraint, True)
566-
567-
def disable_constraint(self, constraint: Constraint) -> Constraint:
568-
return self.set_constraint_enabled(constraint, False)
569-
570530
def add_include(self, include_file: str) -> None:
571531
include_file = include_file.strip()
572532
if not include_file:
@@ -581,12 +541,9 @@ def add_includes(self, include_files: Iterable[str]) -> None:
581541
def add_constraints(
582542
self,
583543
constraints: Iterable[ConstraintInput],
584-
is_redundant: bool = False,
585-
enabled: bool = True,
586544
) -> None:
587-
constraints = list(constraints)
588545
for constraint in constraints:
589-
self.add_constraint(constraint, is_redundant=is_redundant, enabled=enabled)
546+
self.add_constraint(constraint)
590547

591548
def add_function_declaration(self, declaration: str) -> None:
592549
declaration = declaration.strip()
@@ -656,21 +613,20 @@ def call_function(
656613
return Expression.function(name, *args, returns_bool=returns_bool)
657614

658615
def add_function_call(
659-
self, name: str, *args: Any, is_redundant: bool = False
616+
self, name: str, *args: Any
660617
) -> Constraint:
661618
return self.add_constraint(
662-
self.call_function(name, *args, returns_bool=True),
663-
is_redundant=is_redundant,
619+
self.call_function(name, *args, returns_bool=True)
664620
)
665621

666622
def call_predicate(self, name: str, *args: Any) -> ExpressionBool:
667623
return Expression.predicate(name, *args)
668624

669625
def add_predicate_call(
670-
self, name: str, *args: Any, is_redundant: bool = False
626+
self, name: str, *args: Any
671627
) -> Constraint:
672628
return self.add_constraint(
673-
self.call_predicate(name, *args), is_redundant=is_redundant
629+
self.call_predicate(name, *args)
674630
)
675631

676632
def generate(self, debug: bool = False) -> None:
@@ -900,9 +856,7 @@ def to_ir(self) -> ModelIR:
900856
if self.solve_criteria is None:
901857
raise PymzmSolveNotConfigured()
902858

903-
enabled_constraints = tuple(
904-
constraint for constraint in self.constraints if constraint.enabled
905-
)
859+
enabled_constraints = tuple(self.constraints)
906860
auto_includes = {
907861
f"{constraint.ctype}.mzn"
908862
for constraint in enabled_constraints

‎tests/test_invariants.py‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
PymzmArgumentError,
1515
PymzmModelStateError,
1616
PymzmSolveNotConfigured,
17-
PymzmConstraintNotInModel,
1817
PymzmVariableTypeError,
1918
)
2019
from pymzm.model import SOLVE_MAXIMIZE, SOLVE_SATISFY, SearchAnnotation, SeqSearch, RestartStrategy
@@ -68,23 +67,6 @@ def test_set_solve_method_wrong_restart_strategy_raises():
6867
m.set_solve_method(ann, restart_strategy="bad")
6968

7069

71-
# ---------------------------------------------------------------------------
72-
# Model.set_constraint_enabled()
73-
# ---------------------------------------------------------------------------
74-
75-
76-
def test_set_constraint_enabled_not_in_model_raises():
77-
m = Model()
78-
c = Constraint("x > 0")
79-
with pytest.raises(PymzmConstraintNotInModel):
80-
m.set_constraint_enabled(c, True)
81-
82-
83-
# ---------------------------------------------------------------------------
84-
# Model.add_include()
85-
# ---------------------------------------------------------------------------
86-
87-
8870
def test_add_include_empty_string_raises():
8971
m = Model()
9072
with pytest.raises(PymzmArgumentError):

‎tests/test_optional_constraints.py‎

Lines changed: 0 additions & 61 deletions
This file was deleted.

0 commit comments

Comments
 (0)