diff --git a/pumpkin-crates/propagators/src/propagators/arithmetic/linear_less_or_equal.rs b/pumpkin-crates/propagators/src/propagators/arithmetic/linear_less_or_equal.rs index 4611cb1c0..d311654f9 100644 --- a/pumpkin-crates/propagators/src/propagators/arithmetic/linear_less_or_equal.rs +++ b/pumpkin-crates/propagators/src/propagators/arithmetic/linear_less_or_equal.rs @@ -201,37 +201,19 @@ where return Err(conflict.into()); } - let lower_bound_left_hand_side = match TryInto::::try_into( - context.read_trailed_integer(self.lower_bound_left_hand_side), - ) { - Ok(bound) => bound, - Err(_) - if context - .read_trailed_integer(self.lower_bound_left_hand_side) - .is_positive() => - { - // We cannot fit the `lower_bound_left_hand_side` into an i32 due to an - // overflow (hence the check that the lower-bound on the left-hand side is - // positive) - // - // This means that the lower-bounds of the current variables will always be - // higher than the right-hand side (with a maximum value of i32). We thus - // return a conflict - return Err(self.create_conflict(context.domains()).into()); - } - Err(_) => { - // We cannot fit the `lower_bound_left_hand_side` into an i32 due to an - // underflow - // - // This means that the constraint is always satisfied - return Ok(()); - } - }; + let lower_bound_left_hand_side = + context.read_trailed_integer(self.lower_bound_left_hand_side); for (i, x_i) in self.x.iter().enumerate() { - let bound = self.c - (lower_bound_left_hand_side - context.lower_bound(x_i)); - - if context.upper_bound(x_i) > bound { + // Computed in i64: the difference between the left-hand side and a single term need + // not fit in an i32, even when the sum does. + let bound = + self.c as i64 - (lower_bound_left_hand_side - context.lower_bound(x_i) as i64); + + if (context.upper_bound(x_i) as i64) > bound { + // The constraint is not violated, so `bound >= lb(x_i)`; it is also below + // `ub(x_i)`, hence it fits in an i32. + let bound = bound as i32; context.post(predicate![x_i <= bound], i)?; } } @@ -246,36 +228,16 @@ where .map(|var| context.lower_bound(var) as i64) .sum::(); - let lower_bound_left_hand_side = match TryInto::::try_into(lower_bound_left_hand_side) - { - Ok(bound) => bound, - Err(_) - if context - .read_trailed_integer(self.lower_bound_left_hand_side) - .is_positive() => - { - // We cannot fit the `lower_bound_left_hand_side` into an i32 due to an - // overflow (hence the check that the lower-bound on the left-hand side is - // positive) - // - // This means that the lower-bounds of the current variables will always be - // higher than the right-hand side (with a maximum value of i32). We thus - // return a conflict - return Err(self.create_conflict(context.domains()).into()); - } - Err(_) => { - // We cannot fit the `lower_bound_left_hand_side` into an i32 due to an - // underflow - // - // This means that the constraint is always satisfied - return Ok(()); - } - }; + if (self.c as i64) < lower_bound_left_hand_side { + return Err(self.create_conflict(context.domains()).into()); + } for (i, x_i) in self.x.iter().enumerate() { - let bound = self.c - (lower_bound_left_hand_side - context.lower_bound(x_i)); + let bound = + self.c as i64 - (lower_bound_left_hand_side - context.lower_bound(x_i) as i64); - if context.upper_bound(x_i) > bound { + if (context.upper_bound(x_i) as i64) > bound { + let bound = bound as i32; let reason: PropositionalConjunction = self .x .iter() @@ -408,6 +370,46 @@ mod tests { .expect_err("Expected overflow to be detected"); } + #[test] + fn bound_beyond_i32_range_is_not_propagated() { + let mut state = State::default(); + + // The sum of lower bounds fits in an i32, but `lb(lhs) - lb(x)` does not. + let x = state.new_interval_variable(1_805_622_787, 1_842_671_577, None); + let y = state.new_interval_variable(-1_500_000_000, 0, None); + let z = state.new_interval_variable(-1_445_766_565, 0, None); + let constraint_tag = state.new_constraint_tag(); + + let _ = state.add_propagator(LinearLessOrEqualPropagatorArgs { + x: [x, y, z].into(), + c: 0, + constraint_tag, + }); + state.propagate_to_fixed_point().expect("no empty domains"); + + state.assert_bounds(x, 1_805_622_787, 1_842_671_577); + } + + #[test] + fn lower_bound_sum_below_i32_range_still_propagates() { + let mut state = State::default(); + + // The sum of lower bounds is `i32::MIN - 1`, yet `x <= c - lb(y) = i32::MIN + 1`. + let x = state.new_interval_variable(i32::MIN, 0, None); + let y = state.new_interval_variable(-1, 0, None); + let constraint_tag = state.new_constraint_tag(); + + let _ = state.add_propagator(LinearLessOrEqualPropagatorArgs { + x: [x, y].into(), + c: i32::MIN, + constraint_tag, + }); + state.propagate_to_fixed_point().expect("no empty domains"); + + state.assert_bounds(x, i32::MIN, i32::MIN + 1); + state.assert_bounds(y, -1, 0); + } + #[test] fn underflow_leads_to_no_propagation() { let mut state = State::default();