Reputation: 265
I am adding a series of constraints like this, one for each node (Node_A, Node_B, ... Node_L):
def NodeA(model):
k = sum(
model.ArcVar[i] * node_arc_matrix[i,0]
for i in model.ArcVar
)
return k == 2*model.NodeVar[1]
model.NodeAConstraint=Constraint(rule=NodeA)
Where the 0-index in node_arc_matrix refers to the column corresponding to Node_A, and model.NodeVar[1] refers to Node-A as well. Instead of creating a constraint for each node manually can I create constraints across both i (arcs) and j (nodes)?
Upvotes: 0
Views: 1006
Reputation: 1068
No need to add a loop.
When you have elements in your constraint that are a "for all element in ...", Pyomo simply requires you to add the Set
s that you want to iterate over in the Constraint
construction line.
model.NodeAConstraint=Constraint(model.set_of_arcs_A, model.set_of_node_N, rule=NodeA)
And your rule must include a
and n
as parameters (i
seems to already be used in your function).
def NodeA(model, a, n):
k = sum(
model.ArcVar[i] * node_arc_matrix[i,a]
for i in model.ArcVar
)
return k == 2 * model.NodeVar[n]
Notice where I put a
and n
in the previous code. It also needs to be parameters for your function. I tried replacing every 0
by a
and every 1
by n
, if I understood your needs correctly.
Link to documentation here: https://pyomo.readthedocs.io/en/stable/pyomo_modeling_components/Constraints.html
Upvotes: 2