Search code examples
pythonpyomo

How to use an array for the Constraints in Pyomo


I am trying to use a constraint definition in Pyomo inside a for loop.
The following example is a simplification of my problem

for i in range(5):
    model.C[i] = Constraint(expr = model.x[i]<=10)

This haven't worked so far, since I couldn't find a way to declare model.C as an array and keep it as part of the model.
I could see some examples using the rule attribute; however, I wonder if I can make this work without it.

Can someone help me? Thank you


Solution

  • I just found the solution. You must create a ConstraintList and use the add() method:

    model.C = pyo.ConstraintList()
    for g in dados_ger.index:
       model.C.add(expr= (Pg[g]<=dados_ger.maximo[g]))