Skip to main content

Linear

Struct Linear 

Source
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

Source

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.

Source

pub fn sum( vars: impl IntoIterator<Item = Var>, relation: Relation, rhs: i64, ) -> Self

The constraint Σ var (relation) rhs: every coefficient is 1.

Source

pub fn terms(&self) -> impl Iterator<Item = (i64, Var)> + '_

The (coefficient, variable) terms, in order.

Source

pub fn relation(&self) -> Relation

How the sum compares with the right-hand side.

Source

pub fn rhs(&self) -> i64

The right-hand side.

Trait Implementations§

Source§

impl Clone for Linear

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Linear

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Linear

Source§

impl PartialEq for Linear

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<D: Domain> Propagator<D> for Linear

Source§

type Scratch = Vec<(i128, i128)>

The smallest and largest contribution of each term.

Source§

fn scope(&self) -> &[Var]

The variables the constraint is over. A change to any of them wakes the propagator.
Source§

fn propagate( &self, domains: &mut Domains<D>, bounds: &mut Vec<(i128, i128)>, ) -> Result<(), Inconsistent>

Removes the values the constraint rules out, using scratch as working memory. Read more
Source§

impl StructuralPartialEq for Linear

Auto Trait Implementations§

§

impl Forget for Linear

§

impl Freeze for Linear

§

impl Move for Linear

§

impl RefUnwindSafe for Linear

§

impl Send for Linear

§

impl Sync for Linear

§

impl Unpin for Linear

§

impl UnsafeUnpin for Linear

§

impl UnwindSafe for Linear

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.