Skip to content

Commit

Permalink
fix: detection of underflows and overflows for linear inequalities
Browse files Browse the repository at this point in the history
  • Loading branch information
ImkoMarijnissen committed Nov 25, 2024
1 parent c2f7b19 commit 909dba9
Showing 1 changed file with 29 additions and 27 deletions.
56 changes: 29 additions & 27 deletions pumpkin-solver/src/propagators/arithmetic/linear_less_or_equal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,9 @@ where
return Err(conjunction.into());
}

for (i, x_i) in self.x.iter().enumerate() {
let bound = match TryInto::<i32>::try_into(self.lower_bound_left_hand_side) {
Ok(lower_bound_left_hand_side) => {
self.c - (lower_bound_left_hand_side - context.lower_bound(x_i))
}
let lower_bound_left_hand_side =
match TryInto::<i32>::try_into(self.lower_bound_left_hand_side) {
Ok(bound) => bound,
Err(_) if 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
Expand All @@ -165,6 +163,9 @@ where
}
};

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 {
let reason: PropositionalConjunction = self
.x
Expand Down Expand Up @@ -196,29 +197,30 @@ where
.map(|var| context.lower_bound(var) as i64)
.sum::<i64>();

let lower_bound_left_hand_side = match TryInto::<i32>::try_into(lower_bound_left_hand_side)
{
Ok(bound) => bound,
Err(_) if 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_reason(context.as_readonly()).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(());
}
};

for (i, x_i) in self.x.iter().enumerate() {
let bound = match TryInto::<i32>::try_into(lower_bound_left_hand_side) {
Ok(lower_bound_left_hand_side) => {
self.c - (lower_bound_left_hand_side - context.lower_bound(x_i))
}
Err(_) if 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_reason(context.as_readonly()).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 bound = self.c - (lower_bound_left_hand_side - context.lower_bound(x_i));

if context.upper_bound(x_i) > bound {
let reason: PropositionalConjunction = self
Expand Down

0 comments on commit 909dba9

Please sign in to comment.