Skip to main content

Matrixmultiplication with decisionmatrix

Answered

Comments

3 comments

  • Official comment
    Simranjit Kaur
    • 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?.
  • Eli Towle
    • Gurobi Staff

    Currently, you can only construct matrix linear expressions with 1-D MVar objects. Our development team is considering adding more support for 2-D MVar objects in a future release, since functionality like this has been requested a few times.

    Instead, you can add these constraints using matrix slicing:

    for i in range(3):
        for j in range(3):
            m.addConstr(sum(b[k, j] * a[i, :] @ x[:, k] for k in range(3)) >= lb[i, j])

    You could add all of the constraints in one line using Model.addConstrs(), but that might be difficult to read.

     

    1
  • Maliheh Aramon
    • Gurobi Staff
    Hi Yannik,
     
    Gurobi 10.0 was recently released. Included in this release is an improved Python Matrix API which allows matrix operations on 2D MVar objects.
     
    The code snippet below runs fine using Gurobi 10.0.
    m = gp.Model()

    x = m.addMVar((3, 4), vtype=gp.GRB.INTEGER, name="x")
    A, B, b = np.random.rand(3, 3), np.random.rand(4, 3), np.random.rand(3, 3)

    m.addConstr(A @ x @ B <= b)
    Checkout the Matrix-friendly Modeling with Gurobipy webinar if you would like to learn more about this new functionality. 
     
    Please continue submitting new community posts for any bugs you might encounter in the future or for any comments/questions you might have. Users like you help to make Gurobi better!
     
    Best regards,
    Maliheh
    0

Post is closed for comments.