Take as an example a vector
v which is of type Boolean and has n elements. Based on this vector we build a variable array
x of the same length of
v , but from which only the indices corresponding with the non-zero elements of
v are effectively variables.
With JuMP we could do something like this:
v = Bool[1, 1, 1, 0, 0, 1] n = size(v,1) @variable(model, x[i=1:n; v[i] != 0])
The problem for me with the interaction of this variable
x in the model building. If we wanted to create the following objective function:
c = [1, 2, 3, 4, 5, 6] @objective(model, min, c'*x)
Throws the following: ERROR: DimensionMismatch(“first array has length 6 which does not match the length of the second, 4.”)
I would have expected for the variable to behave like a sparse array, with the “non-variable” entries of
x as zeros. But
x doesn’t exist instead of being 0.
Is there any workaround to fill the empty entries with zeros? I would like to avoid for-loops, since the model I’m trying to build is already clearly written in Matrix-vector form (and is much more complex than the example).