Skip to content

Panic when handling large domain values #1

@Specy

Description

@Specy

Rust panics with error:

attempt to subtract with overflow
thread 'solvers::linear_integer_binary::a' panicked at C:\Users\specy\.cargo\registry\src\index.crates.io-6f17d22bba15001f\copper-0.1.0\src\props\sum.rs:33:35:
attempt to subtract with overflow

here:

impl<V: View> Prune for Sum<V> {
    fn prune(&mut self, ctx: &mut Context) -> Option<()> {
        // Derive minimum and maximum values the sum of terms can reach
        let min_of_terms: i32 = self.xs.iter().map(|x| x.min(ctx)).sum();
        let max_of_terms: i32 = self.xs.iter().map(|x| x.max(ctx)).sum();

        let _ = self.s.try_set_min(min_of_terms, ctx)?;
        let _ = self.s.try_set_max(max_of_terms, ctx)?;

        // Current bounds of the sum of all terms
        let min = self.s.min(ctx);
        let max = self.s.max(ctx);

        for x in &self.xs {
            let _ = x.try_set_min(min - (max_of_terms - x.max(ctx)), ctx)?; //<---- here
            let _ = x.try_set_max(max - (min_of_terms - x.min(ctx)), ctx)?; 
        }

        Some(())
    }
}

with code:

#[test]
fn a(){
    let mut m = Model::default();
    let vars: Vec<_> = m.new_vars(3, i32::MIN / 4, i32::MAX / 4).unwrap().collect();
    let c1 = m.sum(&vars);
    m.less_than_or_equals(c1, 256);
    m.maximize(c1).unwrap();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions