Planning & Search in Rust
Esc

Loading…

↑↓ move ↵ open Esc close
Colour theme
All algorithms

Backtracking search with propagation

Also known as MAC, Maintaining arc consistency, Fail-first (MRV)

constraint constraint-search

Backtracking search finds, counts or enumerates the solutions of a model. At each node it picks an unfixed variable (by default the one with the fewest values left), orders its values (ascending, descending, least constraining first, or random) and tries them in turn: it assigns the value, propagates to the fixpoint, and undoes the choice through the trail on a conflict. Propagating every constraint after each choice prunes more than the forward checking of Haralick and Elliott, and it keeps their fail-first principle as the default variable order. The walk keeps its choice points on an explicit stack, so it can pause at a solution or when the budget runs out and continue later; count with a limit of 2 decides whether a puzzle has a unique solution. Use it for logic puzzles, colouring and small scheduling problems; a hint value per variable makes a known solution come first.

Pseudocode

algorithm Backtracking-Search(model, D)input: model with propagators, current domains D, variable order, value orderoutput: each solution in turn (the walk pauses there and resumes after it)if propagate-all(D) = failure thenreturnsearch(D)procedure search(D)x ← pick-variable(D)                         ▷ MRV: fewest values left, ties by index; or lowest indexif x = nil thenyield D                                    ▷ every variable is fixedreturnVALUES ← order(D(x))                         ▷ ascending, descending, least constraining or randommove the hint value of x, if any, to the front of VALUESfor each v in VALUES dopush-level(D)if assign(D, x, v) ≠ failure and propagate-changes(D) ≠ failure thensearch(D)pop-level(D)                               ▷ the trail restores every changed domainfunction count(model, D, limit)n ← 0for each solution s of Backtracking-Search(model, D) don ← n + 1if n = limit thenreturn at least n                        ▷ a limit of 2 decides uniquenessreturn exactly n

In the crates

  • constraint-search

    Depth-first search with propagation after each choice, MRV variable order and LCV or random value order, resumable under a budget; counts solutions with early stop.

    SolverVarOrderValueOrder

Lineage

Comes from

builds on DFS

builds on Constraint propagation to a fixpoint

Papers

  1. Increasing tree search efficiency for constraint satisfaction problems Robert M. Haralick, Gordon L. Elliott · 1980· Artificial Intelligence introduces doi 10.1016/0004-3702(80)90051-x
  2. Contradicting conventional wisdom in constraint satisfaction Daniel Sabin, Eugene C. Freuder · 1994· Principles and Practice of Constraint Programming (PPCP 1994), Lecture Notes in Computer Science extends doi 10.1007/3-540-58601-6_86