pub struct Linear { /* private fields */ }Expand description
a₁·x₁ + a₂·x₂ + … (≤ | = | ≥) b, where each value is read as the integer it is.
This covers Kakuro and Killer-Sudoku cages (sums of digits), Minesweeper (how many of the neighbouring cells, each 0 or 1, are mines), Battleships row and column counts, card counts per hand, and cryptarithms such as SEND + MORE = MONEY (one equation with place values as coefficients).
Propagation reasons on bounds: a value stays only if it can be completed with the smallest
(for ≤) or largest (for ≥) contributions the other terms can still make. It fails once
those bounds cannot meet the right-hand side, so a full assignment is always checked. It
is sound but, for =, not complete: a value whose completions all fall between the bounds
may stay. Coefficients may be negative; sums are computed in i128, so they never
overflow.
§Examples
A Killer-Sudoku cage of two cells summing to 15, digits 1 to 9:
use constraint_search::{Domain, Domains, Linear, Propagator, Relation};
let mut domains = Domains::<u16>::new();
let digits = u16::range(10).difference(u16::singleton(0));
let a = domains.add(digits);
let b = domains.add(digits);
let cage = Linear::sum([a, b], Relation::Equal, 15);
cage.propagate(&mut domains, &mut Vec::new())?;
assert!(domains[a].values().eq(6..=9));Implementations§
Source§impl Linear
impl Linear
Sourcepub fn new(
terms: impl IntoIterator<Item = (i64, Var)>,
relation: Relation,
rhs: i64,
) -> Self
pub fn new( terms: impl IntoIterator<Item = (i64, Var)>, relation: Relation, rhs: i64, ) -> Self
The constraint Σ coefficient · var (relation) rhs over the given terms.
A variable may appear in several terms; propagation then treats the terms independently, which stays sound.
Sourcepub fn sum(
vars: impl IntoIterator<Item = Var>,
relation: Relation,
rhs: i64,
) -> Self
pub fn sum( vars: impl IntoIterator<Item = Var>, relation: Relation, rhs: i64, ) -> Self
The constraint Σ var (relation) rhs: every coefficient is 1.