Skip to main content

Abs value constraint for MLinExpr




  • Yuriy Zinchenko
    Gurobi Staff Gurobi Staff

    You can simply change your variable declaration from using MVar to regular Gurobi variables; the abs-value should work there without any issues.  Have you tried that by any chance?



  • Maliheh Aramon
    Gurobi Staff Gurobi Staff
    Hi Christian,
    Gurobi 10.0 was recently released. Included in this release is the extension of Gurobi Matrix API which enables natural model building using matrix based expressions relying on NumPy concepts such as vectorization and broadcasting.
    The new capabilities of modelling classes MVar, MLinExpr, and MQuadExpr are not still fully integrated with general constraints. The general constraints should be added by indexing the Matrix API classes. See the snippet below.
    m = gp.Model()

    A = np.random.rand(3, 3)
    b = np.random.rand(3, 2)
    x = m.addMVar(shape=(3, 2), name="x")
    y = m.addMVar(shape=(3, 2), name="y")

    m.addConstr(A @ x == b)

    # We still do not support vectorization of the general constraints. The
    # constraints should be added via indexing
    for i in range(3):
    for j in range(2):
    m.addConstr(y[i, j] == gp.abs_(x[i, j]))
    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,

Please sign in to leave a comment.