Skip to main content

Sparse variable matrices in Python

Answered

Comments

7 comments

  • Official comment
    Simranjit Kaur
    Gurobi Staff Gurobi Staff
    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?.
  • Canqi Yao
    First Comment

    Does Gurobi support the sparse matrix of variables? 

    0
  • Riley Clement
    Gurobi Staff Gurobi Staff

    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
  • Canqi Yao
    First Comment

    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
  • Riley Clement
    Gurobi Staff Gurobi Staff

    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
  • Roee Idan
    First Comment
    First Question

    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 code
    m.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
  • Riley Clement
    Gurobi Staff Gurobi Staff

    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.