Repository navigation
Conversation
A soft constraint is one the solve may leave violated, at the cost of its weight: Theorem<T>.SoftWhere(constraint, weight, group) adds it, and a theorem carrying at least one is checked through an Optimize, where each soft constraint is asserted with AssertSoft so Z3 minimises the total weight left unsatisfied. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Soft constraints:
Theorem<T>.SoftWhere(weighted MaxSAT)Until now every
Whereconstraint was hard: a constraint that conflicted with another made the theorem unsatisfiable, and "satisfy this if you can, but prefer that one" could not be written at all.SoftWhereadds the soft form:How it works
SoftWhere(constraint, weight, group = "default")appends aSoftConstraint(expression, weight, group) — a new record travelling separately from the hard constraints through the builder chain, so aWhereadded after aSoftWherekeeps it (pinned by a test).Optimize, becauseAssertSoftdefines an implicit objective — minimise the total weight left unsatisfied — that a plain solver has nowhere to put. The solve path's one dispatch issoftConstraints.Any() ? ctx.MkOptimize() : ctx.MkSolver(), and the model is read off whichever form it got.ExpressionVisitor.Translatea hard constraint goes through and asserted withoptimizer.AssertSoft(expression, weight, group).ArgumentOutOfRangeException, not at solve time.The plumbing this rides on (
Check,AssertConstraints,Assert,AssertBoundsover aZ3Objectapproach) already exists forOptimize, so the solve-path change is the dispatch plus theAssertSoftblock.Tests
8 new cases (
AssertSoftMaxSatTests): the heavier weight wins the conflict; flipping the weights flips the outcome; a compatible soft constraint is satisfied; an impossible one is paid for without making the problem unsat; non-positive weight throws (0 and -3); numeric soft constraints over the same symbol; and a hardWhereadded after aSoftWherekeeps the soft constraint.One deliberate exclusion: the witness-over-soft-constraint test from the original series depends on the model witness of #127, which this branch does not include — it follows once both are in.
Validation
Full suite on 964f177 + this change: 376 passed / 0 failed (368 baseline + 8 new).
dotnet buildof the solution: 0 warnings (the XML doc gate included), 0 errors.Second of the one-concern feature PRs promised alongside #111, after #127.
🤖 Generated with Claude Code