From 897f868eb24237e5765a460ff0269c1c830ef8c2 Mon Sep 17 00:00:00 2001 From: gaoflow Date: Thu, 25 Jun 2026 14:31:34 +0200 Subject: [PATCH] Fix VariableExactSumConstraint forwardcheck double-counting unassigned variable In the no-multipliers branch of __call__, `sum_value` includes `min(domain)` as a placeholder for each unassigned variable. The forward-check loop then computed `temp_sum = sum_value + value`, inadvertently double-counting the placeholder for the variable under scrutiny and pruning valid domain values too aggressively. Example: with a=[1,2], b=[1,2], c=[3] and a=1 already assigned, `sum_value` was 2 (1 from a plus min(b)=1 as placeholder). The forward-check then computed temp_sum = 2 + 2 = 4 > 3 and incorrectly hid b=2, making a+b=3 unreachable. Fix: subtract the placeholder for the variable being checked before adding the candidate value, so only the contribution from *other* unassigned variables is retained: var_placeholder = min(domain) temp_sum = sum_value - var_placeholder + value This mirrors the intent of the multipliers branch, which never added a placeholder and was therefore unaffected. --- constraint/constraints.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/constraint/constraints.py b/constraint/constraints.py index 08839f1..21713aa 100644 --- a/constraint/constraints.py +++ b/constraint/constraints.py @@ -495,8 +495,12 @@ def __call__(self, variables: Sequence, domains: dict, assignments: dict, forwar if temp_sum > target_value: domain.hideValue(value) else: + # sum_value already includes min(domain) as a placeholder for this + # unassigned variable; subtract it before adding the candidate value + # to avoid double-counting. + var_placeholder = min(domain) for value in domain[:]: - temp_sum = sum_value + value + temp_sum = sum_value - var_placeholder + value if temp_sum > target_value: domain.hideValue(value) if not domain: