Skip to main content

sum of array constraints

Answered

Comments

1 comment

  • Simon Bowly
    • Gurobi Staff

    Hi Muhummad,

    What you have here is a standard form matrix constraint, and the ideal way to model this is to use MVars and the matrix-friendly API:

    A = np.array([a1, a2, a3, a4]).transpose() # 5 rows by 4 columns
    b = np.array([1, 1, 0, 0, 0]) # length 5 vector

    model = gp.Model()
    x = model.addMVar(4, name="x") # length 4 MVar
    model.addConstr(A @ x == b) # 5 constraints

    Note though that if your data has a lot of zeros, then it is best to use a scipy.sparse matrix to represent A, instead of the dense numpy format.

    0

Please sign in to leave a comment.