Constructing a sparse variable for optimization

I want to optimize over a matrix X.

While I specify in my model constraints the indices at which X must be zero (according to the specifications I provide in a binary matrix A):

@variable(model, X[1:n_rows, 1:n_cols])
set_lower_bound.(X, zeros(n_rows, n_cols))
set_upper_bound.(X, ones(n_rows, n_cols))
@constraint(model, X .<= A')

I’m wondering if there’s a better alternative I could use – like constructing X as a sparse matrix variable or reformulating my constraints – to help the JuMP model recognize that it does not need to consider the zero-constrained entries of X as variables over which it should optimize, with the hope that it makes the model more efficient.

I’m new to julia, so any insight is super appreciated : )

1 Like

I would do;

@variable(model, 0 <= X[i in 1:n_rows, j in 1:n_cols] <= A[j, i])

If the matrix is very sparse, you might also consider creating a SparseAxisArray like:

@variable(model, 0 <= X[i in 1:n_rows, j in 1:n_cols; A[j, i] == 1] <= 1)

but this has some limitations because it doesn’t act like a true sparse array (you can’t do linear algebra with it).

1 Like