Sparse variable matrices in Python
AnsweredAs of version 9.0, sparse sparse constraints were added to the Gurobipy API. How would I go about defining a sparse matrix of variables, similar to the following (illustrated using Xpress Python interface):
In the example, I use a boolean mask to build a sparse matrix as Xpress does not support declarations using the scipy.sparse interface.
-
Official comment
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?. -
Does Gurobi support the sparse matrix of variables?
0 -
Hi Canqi,
Not currently. You can use a boolean mask to set ub and lb values to 0 when defining an MVar, and presolve will remove these unwanted variables when you optimize. Whether this is a good idea will depend on how big the MVar is and how sparse you would want the matrix - at some point it will be easier and faster to use the traditional gurobipy interface and not the Matrix API.
- Riley
0 -
Dear Riley,
Thanks for your reply! I am trying to solve a mixed integer second order programming model with ten millions of variables and millions of constraints. MVar is used to model the variables while the modeling time (the time used before solving this model) approaches 1 hour, any tips for reducing the modeling time? Besides, lots of memory is required for the parallel implementation of the similar size MISOCP model! To reduce the modeling time and memory, I want to decrease the size of such hard model , e.g., the sparse matrix variable !
-Canqi
0 -
Hi Canqi,
One mistake we often see is users creating MVars then using constraints in the traditional format (which is very slow with MVars), rather than a matrix style format, eg
m.addConstrs(gp.quicksum(w[i]*x[i][j] for i in I) >= b[j] for j in J)
instead of
m.addConstr(w@x >= b)
You want to avoid indexing individual elements of the MVar, so if you're doing that either a) don't use MVars or b) rework your constraints.
Any chance this is a cause of your slow model building?
- Riley
0 -
Hi Riley,
Is there a way to use matrix-style operations with boolean variables to define indicator constraints? Specifically, I'm looking to avoid indexing individual elements of MVars while defining constraints like:
b = m.addMVar(shape=len(numpy_h), vtype=gp.GRB.BINARY, name="b")
Copy codem.addGenConstrIndicator(b[i], True, y[i] == 0, name=f"indicator_y_zero_b_one_{i}") m.addGenConstrIndicator(b[i], False, y[i] >= epsilon, name=f"indicator_y_nonzero_b_zero_{i}")
Can we achieve this in a more efficient way using matrix operations with boolean variables?
Thank you for your help!
Roee
0 -
Hi Roee,
You can use elements from the gurobipy matrix API in indicator constraints in version 11+.
Please see the reference doc for addGenConstrIndicator.
i.e if b and y are MVars (of the same size) you can do
m.addGenConstrIndicator(b, True, y == 0, name=f"indicator_y_zero_b_one"
m.addGenConstrIndicator(b, False, y >= epsilon, name=f"indicator_y_nonzero_b_zero")- Riley
0
Post is closed for comments.
Comments
7 comments