1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//! # 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 `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
//!
//! ```mermaid
//! 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>
extern crate alloc;
pub use ;
pub use ;
pub use ExactCover;
pub use ;
pub use ;
pub use Model;
pub use ParallelSolver;
pub use Propagator;
pub use ;
pub use ;
pub use ;
pub use Table;
/// Panics with `message` if `items` holds an element twice. Constructors use it to reject
/// scopes that list a variable, cell or column more than once.
pub
/// Doctests for the code examples in README.md (not part of the API).