Skip to main content

Crate constraint_search

Crate constraint_search 

Source
Expand description

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 x not be v?”) 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 colour

The 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 * W values, for variables with more values than a u128 holds.
Domains
The current domain of every variable, with a trail of earlier domains.
ExactCover
An exact cover problem: columns to cover and rows that each cover some of them.
GroupIntersection
A value that group A must use, and can only take inside its overlap with group B, is used by that overlap, so the rest of B cannot take it.
Inconsistent
A constraint cannot be satisfied any more: some domain would become empty.
Line
A line of cells, each EMPTY (0) or FILLED (1), holding the given runs of filled cells in order, separated by at least one empty cell.
LineScratch
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::marginals call enumerated, optionally weighted.
MatchingScratch
Working memory of AllDifferent, reused across propagation calls.
Model
A constraint problem: variables, their initial domains and the propagators over them.
ParallelSolver
Backtracking search over a shared model on several threads.
PropagatorRule
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 A also appears in equation B, the remaining terms of B sum to rhs(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.
ValueSubset
k values that only k variables 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 Domains and Model.
VariableSubset
k variables of an all-different group whose domains together hold only k values 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 Linear constraint compares with its right-hand side.
Status
Why Solver::for_each_solution returned.
ValueOrder
How search orders the values of the branching variable.
VarOrder
How search picks the variable to branch on.

Constants§

EMPTY
The cell value of an empty cell in a Line.
FILLED
The cell value of a filled cell in a Line.

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.