use lp_ip_craft::{Model, Sense};
#[inline]
fn down(v: f64) -> f64 {
if v == 0.0 {
-f64::from_bits(1)
} else if v > 0.0 {
f64::from_bits(v.to_bits() - 1)
} else {
f64::from_bits(v.to_bits() + 1)
}
}
#[inline]
fn enclose(v: f64, exact: bool) -> Option<(f64, f64)> {
let pair = if exact { (v, v) } else { (down(v), -down(-v)) };
(pair.0.is_finite() && pair.1.is_finite()).then_some(pair)
}
#[inline]
fn small_integer(v: f64) -> bool {
v.abs() <= 1_125_899_906_842_624.0 && (v as i64) as f64 == v
}
#[inline]
pub(crate) fn is_integer(v: f64) -> bool {
v.abs() >= 4_503_599_627_370_496.0 || (v as i64) as f64 == v
}
#[inline]
pub(crate) fn product(a: f64, b: f64) -> Option<(f64, f64)> {
let v = a * b;
enclose(
v,
a == 0.0
|| b == 0.0
|| a.abs() == 1.0
|| b.abs() == 1.0
|| (small_integer(a) && small_integer(b) && small_integer(v)),
)
}
#[inline]
pub(crate) fn add(a: f64, b: f64) -> Option<(f64, f64)> {
let v = a + b;
enclose(
v,
a == 0.0
|| b == 0.0
|| a == -b
|| (small_integer(a) && small_integer(b) && small_integer(v)),
)
}
#[inline]
pub(crate) fn dot(terms: impl Iterator<Item = (f64, f64)>, offset: f64) -> Option<(f64, f64)> {
let (mut lo, mut hi) = (offset, offset);
for (a, b) in terms {
let (l, u) = product(a, b)?;
lo = add(lo, l)?.0;
hi = add(hi, u)?.1;
}
Some((lo, hi))
}
pub(crate) fn upper(model: &Model, x: &[f64]) -> Option<f64> {
upper_with_bounds(model, model.variables(), x)
}
pub(crate) fn upper_with_bounds(
model: &Model,
bounds: &lp_ip_craft::Bounds,
x: &[f64],
) -> Option<f64> {
for (j, &v) in x.iter().enumerate() {
if !v.is_finite()
|| v < bounds.lower[j]
|| v > bounds.upper[j]
|| (model.kinds()[j].is_integral() && !is_integer(v))
{
return None;
}
}
let a = model.matrix();
for i in 0..model.m() {
let (lo, hi) = dot(
(a.pattern.indptr[i]..a.pattern.indptr[i + 1])
.map(|k| (a.values[k], x[a.pattern.indices[k]]))
.filter(|&(_, v)| v != 0.0),
0.0,
)?;
if lo < model.rows().lower[i] || hi > model.rows().upper[i] {
return None;
}
}
let s = if model.sense() == Sense::Minimize {
1.0
} else {
-1.0
};
Some(
dot(
model
.objective()
.iter()
.zip(x)
.filter(|&(_, &v)| v != 0.0)
.map(|(&c, &v)| (s * c, v)),
s * model.objective_offset(),
)?
.1,
)
}
pub(crate) fn lattice_ceil(bound: f64) -> f64 {
(bound - 1e-6).ceil()
}
pub(crate) fn integer_lattice(model: &Model) -> bool {
model.objective_offset() == 0.0
&& model.is_mip()
&& model
.objective()
.iter()
.zip(model.kinds())
.all(|(&c, k)| c == 0. || k.is_integral())
&& model.objective().iter().all(|&v| small_integer(v))
&& model
.objective()
.iter()
.enumerate()
.map(|(j, &c)| {
c.abs()
* model.variables().lower[j]
.abs()
.max(model.variables().upper[j].abs())
})
.sum::<f64>()
< 562_949_953_421_312.0
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn elementary_enclosures() {
assert_eq!(product(3., 7.), Some((21., 21.)));
let x = product(0.1, 0.2).unwrap();
assert!(x.0 <= 0.02 && x.1 >= 0.02 && x.0 < x.1);
assert!(product(f64::MAX, 2.).is_none());
assert_eq!(add(2., -2.), Some((0., 0.)));
}
}
#[cfg(test)]
mod lattice_tests {
use super::lattice_ceil;
#[test]
fn bounds_a_hair_above_an_integer_stay_at_it() {
for (bound, rounded) in [
(35.13479829452206, 36.),
(36., 36.),
(36.0000000000001, 36.),
(36.00001, 37.),
(-43.00000000000016, -43.),
(-42.9999, -42.),
(-0.4, 0.),
] {
assert_eq!(lattice_ceil(bound), rounded, "{bound}");
}
}
}
#[cfg(test)]
mod small_integer_tests {
use super::{is_integer, small_integer};
#[test]
fn matches_fract_definition() {
let limit = 1_125_899_906_842_624.0;
let mut values = vec![
0.0,
-0.0,
0.5,
-0.5,
1.0,
-1.0,
limit,
-limit,
limit + 1.0,
limit - 0.5,
limit * 2.0,
9.007_199_254_740_993e15,
f64::MIN_POSITIVE,
f64::from_bits(1),
f64::MAX,
f64::INFINITY,
f64::NEG_INFINITY,
f64::NAN,
];
let mut seed = 5u64;
for _ in 0..20000 {
seed = seed
.wrapping_mul(6364136223846793005)
.wrapping_add(1442695040888963407);
let v = f64::from_bits(seed);
values.push(v);
values.push(((seed >> 11) % 4096) as f64 / 8.0 - 256.0);
values.push((seed >> 13) as f64);
}
for v in values {
assert_eq!(
small_integer(v),
v.fract() == 0.0 && v.abs() <= limit,
"{v:e}"
);
if v.is_finite() {
assert_eq!(is_integer(v), v.fract() == 0.0, "{v:e}");
}
}
for v in [
4503599627370495.5,
4503599627370496.0,
-4503599627370495.5,
1e300,
] {
assert_eq!(is_integer(v), v.fract() == 0.0, "{v:e}");
}
}
}