Skip to main content

sum of array constraints

Answered

Comments

2 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    This post is more than two 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 try Gurobot, our chatbot interface offering instant, expert-level support.
  • 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

Post is closed for comments.