constraint-search 0.1.0

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
Documentation
//! # 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>

#![no_std]
#![forbid(unsafe_code)]

extern crate alloc;

mod all_different;
mod domain;
mod exact_cover;
mod line;
mod linear;
mod model;
#[cfg(feature = "parallel")]
mod parallel;
mod propagator;
mod rule;
mod search;
mod store;
mod table;
#[cfg(test)]
mod testing;

pub use all_different::{AllDifferent, MatchingScratch};
pub use domain::{BitSet, Domain, Values};
pub use exact_cover::ExactCover;
pub use line::{EMPTY, FILLED, Line, LineScratch};
pub use linear::{Linear, Relation};
pub use model::Model;
#[cfg(feature = "parallel")]
pub use parallel::ParallelSolver;
pub use propagator::Propagator;
pub use rule::{
    GroupIntersection, PropagatorRule, Rule, Rules, Step, SumDifference, Trace, ValueSubset,
    VariableSubset,
};
pub use search::{
    Count, Marginals, Outcome, Progress, Report, Solver, Stats, Status, ValueOrder, VarOrder,
};
pub use store::{Domains, Inconsistent, Var};
pub use table::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.
#[track_caller]
pub(crate) fn assert_distinct<T: Ord + Clone>(items: &[T], message: &str) {
    let mut sorted = items.to_vec();
    sorted.sort_unstable();
    assert!(
        sorted.windows(2).all(|pair| pair[0] != pair[1]),
        "{message}"
    );
}

/// Doctests for the code examples in README.md (not part of the API).
#[cfg(doctest)]
#[doc = include_str!("../README.md")]
mod readme_doctests {}