use alloc::vec::Vec;
use crate::{Domain, Domains, Inconsistent, Propagator, Var};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Relation {
AtMost,
Equal,
AtLeast,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Linear {
vars: Vec<Var>,
coefficients: Vec<i64>,
relation: Relation,
rhs: i64,
}
impl Linear {
#[must_use]
pub fn new(terms: impl IntoIterator<Item = (i64, Var)>, relation: Relation, rhs: i64) -> Self {
let (coefficients, vars) = terms.into_iter().unzip();
Self {
vars,
coefficients,
relation,
rhs,
}
}
#[must_use]
pub fn sum(vars: impl IntoIterator<Item = Var>, relation: Relation, rhs: i64) -> Self {
Self::new(vars.into_iter().map(|var| (1, var)), relation, rhs)
}
pub fn terms(&self) -> impl Iterator<Item = (i64, Var)> + '_ {
self.coefficients
.iter()
.copied()
.zip(self.vars.iter().copied())
}
#[must_use]
pub fn relation(&self) -> Relation {
self.relation
}
#[must_use]
pub fn rhs(&self) -> i64 {
self.rhs
}
fn caps(&self) -> bool {
matches!(self.relation, Relation::AtMost | Relation::Equal)
}
fn floors(&self) -> bool {
matches!(self.relation, Relation::AtLeast | Relation::Equal)
}
}
impl<D: Domain> Propagator<D> for Linear {
type Scratch = Vec<(i128, i128)>;
fn scope(&self) -> &[Var] {
&self.vars
}
fn propagate(
&self,
domains: &mut Domains<D>,
bounds: &mut Vec<(i128, i128)>,
) -> Result<(), Inconsistent> {
let rhs = i128::from(self.rhs);
let (low, high) = self.term_bounds(domains, bounds);
if (self.caps() && low > rhs) || (self.floors() && high < rhs) {
return Err(Inconsistent);
}
for (term, (&var, &coefficient)) in self.vars.iter().zip(&self.coefficients).enumerate() {
let (term_low, term_high) = bounds[term];
let (rest_low, rest_high) = (low - term_low, high - term_high);
let mut allowed = domains[var];
for value in domains[var].values() {
let contribution = i128::from(coefficient) * i128::from(value);
let over = self.caps() && contribution + rest_low > rhs;
let under = self.floors() && contribution + rest_high < rhs;
if over || under {
allowed.remove(value);
}
}
domains.restrict(var, allowed)?;
}
Ok(())
}
}
impl Linear {
fn term_bounds<D: Domain>(
&self,
domains: &Domains<D>,
bounds: &mut Vec<(i128, i128)>,
) -> (i128, i128) {
bounds.clear();
let (mut low, mut high) = (0, 0);
for (&var, &coefficient) in self.vars.iter().zip(&self.coefficients) {
let contributions = domains[var]
.values()
.map(|value| i128::from(coefficient) * i128::from(value));
let (term_low, term_high) =
contributions.fold((i128::MAX, i128::MIN), |(lo, hi), c| (lo.min(c), hi.max(c)));
bounds.push((term_low, term_high));
low += term_low;
high += term_high;
}
(low, high)
}
}
#[cfg(test)]
mod tests {
use alloc::vec::Vec;
use super::{Linear, Relation};
use crate::testing::{Rng, check_sound_against_brute_force};
use crate::{Domains, Inconsistent, Propagator, Var};
#[test]
fn sound_against_brute_force() {
let mut rng = Rng::new(23);
for _ in 0..3000 {
let vars = 1 + rng.below(4);
let coefficients: Vec<i64> = (0..vars)
.map(|_| i64::try_from(rng.below(7)).unwrap() - 3)
.collect();
let relation = [Relation::AtMost, Relation::Equal, Relation::AtLeast][rng.below(3)];
let rhs = i64::try_from(rng.below(25)).unwrap() - 8;
let holds = |assignment: &[u32]| {
let sum: i64 = coefficients
.iter()
.zip(assignment)
.map(|(&a, &x)| a * i64::from(x))
.sum();
match relation {
Relation::AtMost => sum <= rhs,
Relation::Equal => sum == rhs,
Relation::AtLeast => sum >= rhs,
}
};
check_sound_against_brute_force(&mut rng, vars, 6, holds, |scope: Vec<Var>| {
Linear::new(coefficients.iter().copied().zip(scope), relation, rhs)
});
}
}
#[test]
fn infeasible_bounds_fail() {
let mut domains = Domains::<u16>::new();
let x = domains.add(0b11);
let y = domains.add(0b11);
let too_big = Linear::sum([x, y], Relation::AtLeast, 3);
assert_eq!(
too_big.propagate(&mut domains, &mut Vec::new()),
Err(Inconsistent)
);
let negative = Linear::new([(-1, x), (2, y)], Relation::Equal, 2);
negative.propagate(&mut domains, &mut Vec::new()).unwrap();
assert_eq!((domains[x], domains[y]), (0b01, 0b10));
}
}