Skip to content

Get original variables: Reading out of bounds #253

@HunkBenny

Description

@HunkBenny

TL;DR

Encountered an out of bounds error when calling model.orig_vars().

When calling this method, this underlying piece of code is called:

russcip/src/scip.rs

Lines 233 to 254 in 11a4df9

pub(crate) fn vars(&self, original: bool, capture: bool) -> BTreeMap<usize, *mut SCIP_Var> {
// NOTE: this method should only be called once per SCIP instance
let n_vars = self.n_vars();
let mut vars = BTreeMap::new();
let scip_vars = if original {
unsafe { ffi::SCIPgetOrigVars(self.raw) }
} else {
unsafe { ffi::SCIPgetVars(self.raw) }
};
for i in 0..n_vars {
let scip_var = unsafe { *scip_vars.add(i) };
if capture {
unsafe {
ffi::SCIPcaptureVar(self.raw, scip_var);
}
}
let var = scip_var;
let var_id = unsafe { ffi::SCIPvarGetIndex(var) } as usize;
vars.insert(var_id, var);
}
vars
}

This loops over all variables (n_vars) and retrieves them from SCIP. The problem here is that the number of original variables can be different from the number of variables currently in the problem.

When querying the original variables, n_vars should (I believe) be retrieved via ffi::SCIPgetNOrigVars(self.raw). Also see; https://www.scipopt.org/doc/html/group__GlobalProblemMethods.php#ga6072e2a6e7e1d54410e19ddcaae32cd6

--- Context:
I was working on the Branch-and-Price example found here. One of the bonus exercises asks us to initialize the problem with a smart selection of variables. When SCIP starts solving the problem, all variables are transformed. As a result, new variables (with new indexes) are created by SCIP. This is an issue when you are using the Ryan-Foster branching rule, as you need to know what the pattern looks like when branching. If you have a variable that is transformed from an original variable, then you need to figure out during solving which original variable it relates to because then you can retrieve the object representation. For this I wanted to use the orig_vars() method, to check which transformed variables represent them.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions