Skip to content

Add SoftWhere soft constraints (weighted MaxSAT) - #128

Open
jsboige wants to merge 1 commit into
endjin:mainfrom
MyIntelligenceAgency:feature/soft-constraints-16609
Open

jsboige wants to merge 1 commit into
endjin:mainfrom
MyIntelligenceAgency:feature/soft-constraints-16609

Conversation

@jsboige

@jsboige jsboige commented Nov 14, 2026

Copy link
Copy Markdown

Soft constraints: Theorem<T>.SoftWhere (weighted MaxSAT)

Until now every Where constraint 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. SoftWhere adds the soft form:

var result = ctx.NewTheorem<Flags>()
    .Where(f => f.A ^ f.B)            // hard: exactly one of A, B
    .SoftWhere(f => f.A, weight: 10)  // soft: prefer A
    .SoftWhere(f => f.B, weight: 1)   // soft: over B
    .Solve();                          // A = true, B = false

How it works

  • SoftWhere(constraint, weight, group = "default") appends a SoftConstraint (expression, weight, group) — a new record travelling separately from the hard constraints through the builder chain, so a Where added after a SoftWhere keeps it (pinned by a test).
  • A theorem with no soft constraint takes the plain solver, exactly as before. One with at least one is checked through an Optimize, because AssertSoft defines an implicit objective — minimise the total weight left unsatisfied — that a plain solver has nowhere to put. The solve path's one dispatch is softConstraints.Any() ? ctx.MkOptimize() : ctx.MkSolver(), and the model is read off whichever form it got.
  • Each soft constraint is translated through the same ExpressionVisitor.Translate a hard constraint goes through and asserted with optimizer.AssertSoft(expression, weight, group).
  • A weight must be strictly positive: a sacrifice that costs nothing cannot be traded against one that does, so zero or negative fails at the call with ArgumentOutOfRangeException, not at solve time.

The plumbing this rides on (Check, AssertConstraints, Assert, AssertBounds over a Z3Object approach) already exists for Optimize, so the solve-path change is the dispatch plus the AssertSoft block.

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 hard Where added after a SoftWhere keeps 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 build of 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

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant