diff --git a/src/pyscipopt/scip.pxi b/src/pyscipopt/scip.pxi index 21679d001..30cded111 100644 --- a/src/pyscipopt/scip.pxi +++ b/src/pyscipopt/scip.pxi @@ -2509,8 +2509,8 @@ cdef class _VarArray: else: raise TypeError(f"Expected Variable or list of Variable, got {type(vars)}.") - if vars: - self.size = len(vars) + self.size = len(vars) + if self.size: self.ptr = malloc(self.size * sizeof(SCIP_VAR*)) for i, var in enumerate(vars): if not isinstance(var, Variable): diff --git a/tests/test_cons.py b/tests/test_cons.py index 957081241..14c60ff66 100644 --- a/tests/test_cons.py +++ b/tests/test_cons.py @@ -210,6 +210,17 @@ def test_cons_indicator_with_matrix_binvar(): assert m.isEQ(m.getVal(x), 1) +def test_cons_knapsack_with_matrix_vars(): + # test matrix variable vars #1043 + m = Model() + vars = m.addMatrixVar(3, vtype="B") + m.addConsKnapsack(vars, [1, 2, 3], 5) + + m.setObjective(vars.sum(), "maximize") + m.optimize() + + assert (m.getVal(vars) == [0, 1, 1]).all() + @pytest.mark.xfail( reason="addConsIndicator doesn't behave as expected when binary variable is False. See Issue #717." )