Expand description
§Constraint search
Finite-domain constraint satisfaction: variables with small domains, constraints that prune them, and search for the assignments that remain.
§Scope
- Finite-domain variables with bitset domains (
Domain) and a trail for undoing changes on backtrack (Domains) - Propagators run to a fixpoint (
Propagator), including all-different with complete Hall-set reasoning (AllDifferent), constraints given by their allowed tuples (Table), weighted sums (Linear) and nonogram lines (Line) - Pluggable inference rules ordered by cost, for human-style solving, hints, explanations
(“why can
xnot bev?”) and difficulty grading (Rules), for all-different groups, sums and any propagator (PropagatorRule) - Backtracking search with variable and value ordering heuristics under a
search_core::Budget, resumable so the work can be spread over time (Solver,Solver::resume) - Solution counting with early stop, for checking that a puzzle has a unique solution
(
Solver::count) - Problems with many solutions, as with hidden information: the values certain in every
solution (
Solver::backbone), each value’s probability (Solver::marginals), and random solutions to determinize hidden state for game-tree search (Solver::sample) - Exact cover with dancing links (
ExactCover)
§Overview
flowchart LR
model[Model: variables, propagators] --> solver[Solver: domains, trail, queue]
solver -->|solve, count| search[backtracking + propagation]
domains[Domains] --> rules[Rules: costed deductions]
rules --> trace[Trace: steps, grade]A Model holds the variables and constraints and is shared read-only; it is
Send + Sync. A Solver borrows it, owns the working state and reuses it across
solves, which suits generators that check uniqueness thousands of times. Rules work on
any Domains and explain a solve step by step instead.
Domains are bitsets: u16, u32, u64 and u128 hold up to their bit width in values,
and BitSet holds more.
§Example
Colour a map of four regions with three colours, so that neighbours differ:
use constraint_search::{AllDifferent, Domain, Model, Outcome, Solver, Stats};
let mut model = Model::<u16>::new();
let [a, b, c, d] = [(); 4].map(|()| model.add_var(u16::range(3)));
for pair in [[a, b], [a, c], [b, c], [b, d], [c, d]] {
model.add(AllDifferent::new(pair));
}
let report = Solver::new(&model).solve(|_: &Stats| false);
let Outcome::Solved(colours) = report.outcome else { panic!("three colours suffice") };
assert_eq!(colours[0], colours[3]); // a and d must share a colourThe crate is no_std and needs alloc. The parallel feature adds ParallelSolver
(it needs std for threads): the search tree split across a pool of threads, or a
portfolio of branching heuristics racing each other, with a shared stop.
Repository: https://github.com/sunsided/constraint-search
Structs§
- AllDifferent
- The variables take pairwise distinct values.
- BitSet
- A domain of
64 * Wvalues, for variables with more values than au128holds. - Domains
- The current domain of every variable, with a trail of earlier domains.
- Exact
Cover - An exact cover problem: columns to cover and rows that each cover some of them.
- Group
Intersection - A value that group
Amust use, and can only take inside its overlap with groupB, is used by that overlap, so the rest ofBcannot take it. - Inconsistent
- A constraint cannot be satisfied any more: some domain would become empty.
- Line
- A line of cells, each
EMPTY(0) orFILLED(1), holding the given runs of filled cells in order, separated by at least one empty cell. - Line
Scratch - Working memory of
Line: reachability of placement states and the values seen. - Linear
a₁·x₁ + a₂·x₂ + … (≤ | = | ≥) b, where each value is read as the integer it is.- Marginals
- How often each value occurs across the solutions a
Solver::marginalscall enumerated, optionally weighted. - Matching
Scratch - Working memory of
AllDifferent, reused across propagation calls. - Model
- A constraint problem: variables, their initial domains and the propagators over them.
- Parallel
Solver - Backtracking search over a shared model on several threads.
- Propagator
Rule - Runs propagators of one kind as a single
Rule, so their deductions get a name and a cost in hints and difficulty grades. - Report
- What a search returns: its result and the counters that led to it.
- Rules
- A set of rules, kept in ascending order of cost.
- Solver
- Backtracking search over a shared
Model, with reusable working state. - Stats
- Counters collected during one search.
- Step
- One application of a rule: which rule, and what it removed.
- SumDifference
- When every term of equation
Aalso appears in equationB, the remaining terms ofBsum torhs(B) − rhs(A). - Table
- The variables take one of the listed tuples of values.
- Trace
- The steps of a solve by rules alone, and whether they solved the puzzle.
- Value
Subset kvalues that onlykvariables of an all-different group can still take must go to those variables, so those variables can take no other value.- Values
- The values of a domain in ascending order, returned by
Domain::values. - Var
- A variable: a dense index into
DomainsandModel. - Variable
Subset kvariables of an all-different group whose domains together hold onlykvalues use up those values, so no other variable of the group can take them.
Enums§
- Count
- How many solutions a count found.
- Outcome
- How a search ended.
- Progress
- Where a resumable search stands.
- Relation
- How the sum of a
Linearconstraint compares with its right-hand side. - Status
- Why
Solver::for_each_solutionreturned. - Value
Order - How search orders the values of the branching variable.
- VarOrder
- How search picks the variable to branch on.
Constants§
Traits§
- Domain
- The set of values a variable may still take, as a bitset over
0..CAPACITY. - Propagator
- A constraint that removes values from variable domains.
- Rule
- A named deduction technique with a cost.