skip to content

Two constraint models of the same support rota differ only in encoding, yet one solves in seconds and the other thrashes for hours - what makes the difference?

level: seniorimportance: nice to knowfreq 28%

answer

  1. the model, not the engine
  2. group constraints infer more
  3. interchangeable values multiply copies
  4. backtracking undoes the wrong guess
  5. branch on the most constrained variable

basics

~20 s

Solve time is a property of the encoding, not only of the problem. A rule stated as one group constraint prunes far more than the equivalent pairwise pile, interchangeable people multiply identical rosters, and a poor variable order makes the search rediscover one conflict everywhere.

solid answer

~50 s

Three levers, all of them in the model rather than the solver. **Propagation strength**: "these seven days take seven different people" as a single group constraint lets the solver reason about the whole group and cut early, while twenty-one pairwise inequalities only bite once a day is actually fixed. **Symmetry**: if four people are fully interchangeable, every roster exists in many permuted copies, and a search that has refuted one copy will dutifully refute the rest unless you impose an order that admits just one. **Ordering and thrashing**: chronological backtracking undoes the most recent guess, so when the real culprit was fixed high in the tree the search re-derives the same conflict in subtree after subtree - that is thrashing, and picking the most constrained variable first, or learning the conflict, stops it. Same solution set, wildly different search.

go deeper

for a junior

Take away one idea: how you write the rules, not just which rules you write, decides how long the solver takes.

for a middle

Be able to contrast a group constraint with the equivalent pairwise statements and say why the group form rules more out before anything is fixed.

for a senior

Diagnose a slow run: look for repeated identical failures, interchangeable values with no order imposed, and a branching order that hides the conflict from the root.

for a principal

Weigh a faster encoding against a model the on-call team can still read and change, and decide when the honest answer is that the problem is hard rather than badly modelled.

## Solve time is a property of the model Two models with identical solution sets can differ by orders of magnitude in cost, because the solver does not search the problem - it searches the space the model gave it, using the reasoning the constraint forms permit. This is why experienced modellers treat re-modelling as the first performance lever, ahead of tuning the engine. ## Propagation strength: group versus pairwise A rule can almost always be written more than one way. - As **one group constraint** over many variables: the solver can reason about the group as a whole. If five days have shrunk to the same four candidates, no other day in the group can take any of those four, and it can say so immediately. - As a **pile of pairwise statements**: logically identical, but each one can only remove a value when one of its two variables is actually fixed. The counting argument above is invisible to it. Same solutions, weaker inference, exponentially more nodes. ## Symmetry multiplies identical rosters If four people are genuinely interchangeable for a stretch of the quarter, any roster over them has many equivalent permutations. The consequences are both ways round: - Finding a solution is not much affected - any copy will do. - **Proving** something - infeasibility, or optimality - is badly affected, because the search must refute every copy separately, and refuting one teaches it nothing about the others. The fix is a symmetry-breaking constraint: impose an arbitrary order among the interchangeable elements so exactly one representative of each equivalence class survives. It rules out solutions, which feels wrong until you see that each one it rules out is a relabelling of one it keeps. ## Thrashing, and where it comes from Thrashing is the search failing repeatedly for the same reason in unrelated parts of the tree. The mechanism: 1. Some early guess - a person fixed to a Monday in week 1 - is the real cause of a conflict that only shows up in week 9. 2. The search hits the conflict, and chronological backtracking undoes the **most recent** guess, which is in week 9 and irrelevant. 3. It tries the next week-9 value, hits the same conflict, and repeats across the whole subtree before it ever revisits week 1. The standard answers: | Lever | What it changes | |---|---| | Fail-first variable order | Branch on the most constrained variable, so conflicts surface near the root | | Value order | Try the value most likely to succeed first, shortening the path to a solution | | Conflict-directed backjumping | Undo the guess that actually caused the failure, not the latest one | | Learning the conflict | Record the combination that failed so the same dead end is refuted once | | Restarts | Abandon a bad early branching decision instead of living with it | Fail-first is the counter-intuitive one: branching on the variable with the fewest remaining values makes failures happen sooner and nearer the root, where a refutation removes the most. ## What a re-model actually changes - The **number of variables** and therefore the tree's depth. - The **form of each rule**, and therefore how much each one can infer before anything is fixed. - Whether the objective can bound a **partial** assignment or only a complete one. - How many **equivalent copies** of each solution exist. - What the branching heuristic has to work with - domains that shrink informatively, or flat ones that tell it nothing. ## Where this shows up outside scheduling Solvers hide inside ordinary tooling, and the same encoding effects apply. - **Dependency version resolution**: choosing one version per package such that every declared range is satisfied is exactly a constraint model - variables are packages, domains are published versions, constraints are the ranges. Resolvers that report "no version set satisfies the requirements" are reporting infeasibility, and the good ones report a conflict set with it. - **Constraint-based type inference**: a checker walks the program, collects constraints between unknown types, and solves them, which is why a type error can be reported far from the line that caused it - the conflict is a property of the collected set, not of one expression. ## What interviewers listen for That you locate the fix in the model rather than the engine, that you can name at least one of propagation strength, symmetry and search order with its mechanism, and that you say "can change by orders of magnitude" rather than promising it - re-modelling is a lever, not a guarantee, and some models are slow because the problem is genuinely hard.

  • Symmetry breaking removes legal solutions. How is that safe?
    Because each one it removes is a relabelling of one it keeps: imposing an order on interchangeable people leaves exactly one representative per equivalence class. Satisfiability and the optimal cost are unchanged; only the count of distinct answers falls. It is unsafe only if the elements are not genuinely interchangeable - different qualifications, different contracts.
  • Why does branching on the most constrained variable help rather than hurt?
    Because the expensive outcome is discovering a failure deep in the tree after building most of an assignment. Branching where the fewest values remain makes failures happen early and near the root, where one refutation removes the largest subtree. The heuristic is failing sooner deliberately, which is why it is called fail-first.
  • Is re-modelling always worth trying before tuning the solver?
    Usually, because encoding decides both the size of the space and how much each rule can infer, while engine settings only change how that fixed space is walked. But it is not free: a re-model is a rewrite the team must still be able to read, and some problems are slow because they are genuinely hard rather than badly stated.

Two people are given the same jigsaw: one sorts edges and colour groups first, the other starts at a random piece and pushes on. Same puzzle, same picture, entirely different afternoon.

saying these in an interview costs you the question

  • Assuming logically equivalent rule forms always prune equally
  • Treating solve time as a property of the problem alone
  • Calling symmetry breaking unsound because it removes solutions
  • Branching on the variable with the most options left
  • Expecting chronological backtracking to undo the guilty guess
  • Promising that a re-model will always deliver orders of magnitude