constraint-search
Finite-domain constraint satisfaction for puzzles and deduction games: check that a puzzle has one solution, give human-style hints and grade difficulty, infer hidden facts, with propagation, all-different reasoning, backtracking and solution counting
cargo add constraint-search Who it is for
- Puzzle generators and solvers (Sudoku and its variants, nonograms, Minesweeper, Kakuro, logic grids): uniqueness checks stop at the second solution, and the human-style rules grade difficulty by the costliest technique a solve needs and give the next hint step.
- Tutoring, hint and explainable configuration tools, and small scheduling or allocation models: rules carry names, and
Trace::why_removedsays which deduction ruled a value out, so "why can't I choose this?" has an answer next to the solution. - Game AI with hidden information, such as card locations or unrevealed Minesweeper cells: the backbone gives the certain facts, marginals the probability of each value, and sampling draws consistent hidden states to plan with, without enumerating worlds by hand.
- Embedded and
no_stdusers: the core needs onlyalloc, and parallel search is an opt-instdfeature. - For large industrial models, optimisation objectives or huge domains, use OR-Tools CP-SAT, Gecode or a MIP solver. For search over the moves of an adversarial game, use game-tree-search; for shortest paths and state-space search with costs, heuristic-search; for planning when the outcomes of actions are uncertain, rather than facts hidden, stochastic-search.
Algorithms 10
- Algorithm X with dancing links DLX · Dancing links
Exact cover with primary and secondary columns, branching on the column with the fewest rows, on index-linked arrays.
- All-different by bipartite matching Régin's all-different · GAC all-different · Hall-set reasoning
Complete (generalised arc consistent) all-different from a maximum matching and the strongly connected components of the variable–value graph.
- Backbone and solution marginals Backbone · Solution sampling
The values possible and certain in every solution, each value's probability (optionally weighted) across the solutions, and random solutions by restarts for determinizing hidden information.
- Backtracking search with propagation MAC · Maintaining arc consistency · Fail-first (MRV)
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.
- Bounds propagation for linear constraints Linear sum propagation
Weighted sums compared with a constant (≤, = or ≥), pruned on the bounds the other terms can still reach.
- Constraint propagation to a fixpoint Propagation engine · AC-3-style propagation
Runs every propagator whose scope changed until none removes a value; the trail undoes the changes on backtrack.
- Cost-ordered inference rules Human-style solving · Naked and hidden subsets · Locked candidates
Named, costed rules applied cheapest first, for hints, difficulty grading and an answer to why a value was removed; generic names, with a domain's own terms through with_name.
RulesVariableSubsetValueSubsetGroupIntersectionSumDifferencePropagatorRuleTrace -
Splits the search into partial assignments of the first branching variables, which a thread pool solves with one Solver each; a portfolio mode races branching heuristics instead.
- Nonogram line solving Paint-by-numbers line solver
Complete propagation of one nonogram row or column: keeps exactly the cell values some placement of the runs uses.
- Table constraint Extensional constraint · GAC on allowed tuples
Complete propagation of a constraint given by its allowed tuples, in one scan of the tuples.