Skip to main content

How to define constraints over multiple dimensions?

Answered

Comments

2 comments

  • Eli Towle
    Gurobi Staff Gurobi Staff

    Hi Irene,

    It looks like to me like the first and second constraints are mixed up in the capacity constraint you coded. In particular, \( \texttt{supply_avail[i, j]} \) corresponds to \( a_{ij} \) in the formulation, which shows up in the second constraint set. However, you sum over \( I \) on the left-hand side, which is done only in the first constraint set. I'll assume we are trying to write the second constraint set.

    Model.addConstr() is used to add a single constraint. Instead, we can use Model.addConstrs() to add multiple constraints with a single method call. Additionally, for the second constraint, we want to sum over \( L \) on the left-hand side, not \( I \). Altogether:

    m.addConstrs((x.sum(i, j, '*') <= supply_avail[i, j] for i in supply_node for j in resource), name='capacity')

    This code adds the following constraints:

    capacity[Node1,A]: allocate[Node1,A,Sink1] + allocate[Node1,A,Sink2] <= 2                                                 
    capacity[Node1,B]: allocate[Node1,B,Sink1] + allocate[Node1,B,Sink2] <= 1                                                 
    capacity[Node1,C]: allocate[Node1,C,Sink1] + allocate[Node1,C,Sink2] <= 1                                                 
    capacity[Node2,A]: allocate[Node2,A,Sink1] + allocate[Node2,A,Sink2] <= 1                                                 
    capacity[Node2,B]: allocate[Node2,B,Sink1] + allocate[Node2,B,Sink2] <= 1                                                 
    capacity[Node2,C]: allocate[Node2,C,Sink1] + allocate[Node2,C,Sink2] <= 1                                                 
    capacity[Node3,A]: allocate[Node3,A,Sink1] + allocate[Node3,A,Sink2] <= 1                                                 
    capacity[Node3,B]: allocate[Node3,B,Sink1] + allocate[Node3,B,Sink2] <= 0                                                 
    capacity[Node3,C]: allocate[Node3,C,Sink1] + allocate[Node3,C,Sink2] <= 1 

    As far as a better way to define the data: explicitly defining the data in the code should be very fast. The model-building time should be negligible for a model of this size.

    Thanks,

    Eli

    0
  • Irene Alisjahbana
    Gurobi-versary
    First Comment
    First Question

    Hi Eli, 

    Thank you for your quick reply.

    Apologies for the error. You are correct, I was trying to demonstrate the second set of constraints. 

    This makes a lot of sense and is exactly what I was looking for! Thank you.

    Best,

    Irene

    0

Please sign in to leave a comment.