-1

I am using python in Google Colab to write a nonlinear optimization model and the goal is to run it in a meta-heuristic and get the results. The first time I ran the model it worked just fine. However, I changed some variables and added more constraints to the problem and now I keep getting the following error.

RuntimeError: Cannot add component 'cons_index' (type <class 'pyomo.core.base.set.AbstractOrderedScalarSet'>) to block 'unknown': a component by that name (type <class 'pyomo.core.base.set.OrderedScalarSet'>) is already defined.

The code:

import pyomo.environ as pyo
from pyomo.opt import SolverFactory

M = [10,20,30,40,50,60,70,80,90,100]

model = pyo.ConcreteModel()
model.t = pyo.RangeSet(1, len(M))

model.x = pyo.Var(T, domain = pyo.Reals)
model.y = pyo.Var(T, domain = pyo.Reals)

model.OBJ = pyo.Objective(expr = sum(model.x[t]**2 + model.y[t]**2 for t in T))

model.cons = pyo.ConstraintList()

for t in T:
    model.cons.add(sum(model.x[i] * model.y[t] for i in T) >= M[t])

solver = pyo.SolverFactory('ipopt')
solver.solve(model)
print('Obj_Function Value = ', model.OBJ())

I used pyomo the first time on a this problem and no error was given then I wanted to make some changes to it and now at the model.cons = pyo.ConstraintList() line the error is shown to me.

Anas
  • 29
  • 3

1 Answers1

0

I made a few assumptions here to get this running....

First, if you use a pyomo RangeSet (I would advise against it, unless you really want to be 1-indexed) you need to correct for that when indexing regular python things, like M. Note that I tweaked the index.

I also changed the domain to get this running to non-negative reals. Not sure what you are trying to do, so maybe that isn't a good assumption.

Take a look at the model printout to ensure the math is what you are looking for.

import pyomo.environ as pyo
from pyomo.opt import SolverFactory

M = [10,20,30,40,50,60,70,80,90,100]

model = pyo.ConcreteModel()
model.T = pyo.RangeSet(1, len(M))

model.x = pyo.Var(model.T, domain = pyo.NonNegativeReals)
model.y = pyo.Var(model.T, domain = pyo.NonNegativeReals)

model.OBJ = pyo.Objective(expr = sum(model.x[t]**2 + model.y[t]**2 for t in model.T))

model.cons = pyo.ConstraintList()

for t in model.T:
    model.cons.add(sum(model.x[i] * model.y[t] for i in model.T) >= M[t-1])

solver = pyo.SolverFactory('ipopt')
solver.solve(model)
print('Obj_Function Value = ', model.OBJ())

model.pprint()

Output:

Obj_Function Value =  124.09673524399766
1 Set Declarations
    cons_index : Size=1, Index=None, Ordered=Insertion
        Key  : Dimen : Domain : Size : Members
        None :     1 :    Any :   10 : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

1 RangeSet Declarations
    T : Dimen=1, Size=10, Bounds=(1, 10)
        Key  : Finite : Members
        None :   True :  [1:10]

2 Var Declarations
    x : Size=10, Index=T
        Key : Lower : Value              : Upper : Fixed : Stale : Domain
          1 :     0 : 2.4909509754709234 :  None : False : False : NonNegativeReals
          2 :     0 : 2.4909509754709234 :  None : False : False : NonNegativeReals
          3 :     0 : 2.4909509754709234 :  None : False : False : NonNegativeReals
          4 :     0 : 2.4909509754709234 :  None : False : False : NonNegativeReals
          5 :     0 : 2.4909509754709234 :  None : False : False : NonNegativeReals
          6 :     0 : 2.4909509754709234 :  None : False : False : NonNegativeReals
          7 :     0 : 2.4909509754709234 :  None : False : False : NonNegativeReals
          8 :     0 : 2.4909509754709234 :  None : False : False : NonNegativeReals
          9 :     0 : 2.4909509754709234 :  None : False : False : NonNegativeReals
         10 :     0 : 2.4909509754709234 :  None : False : False : NonNegativeReals
    y : Size=10, Index=T
        Key : Lower : Value              : Upper : Fixed : Stale : Domain
          1 :     0 : 0.4014531026983497 :  None : False : False : NonNegativeReals
          2 :     0 : 0.8029062007167008 :  None : False : False : NonNegativeReals
          3 :     0 : 1.2043592997747101 :  None : False : False : NonNegativeReals
          4 :     0 :  1.605812399092779 :  None : False : False : NonNegativeReals
          5 :     0 :  2.007265498514876 :  None : False : False : NonNegativeReals
          6 :     0 :  2.408718597988988 :  None : False : False : NonNegativeReals
          7 :     0 : 2.8101716974928213 :  None : False : False : NonNegativeReals
          8 :     0 :  3.211624797015233 :  None : False : False : NonNegativeReals
          9 :     0 : 3.6130778965500268 :  None : False : False : NonNegativeReals
         10 :     0 :  4.014530996093492 :  None : False : False : NonNegativeReals

1 Objective Declarations
    OBJ : Size=1, Index=None, Active=True
        Key  : Active : Sense    : Expression
        None :   True : minimize : x[1]**2 + y[1]**2 + x[2]**2 + y[2]**2 + x[3]**2 + y[3]**2 + x[4]**2 + y[4]**2 + x[5]**2 + y[5]**2 + x[6]**2 + y[6]**2 + x[7]**2 + y[7]**2 + x[8]**2 + y[8]**2 + x[9]**2 + y[9]**2 + x[10]**2 + y[10]**2

1 Constraint Declarations
    cons : Size=10, Index=cons_index, Active=True
        Key : Lower : Body                                                                                                                             : Upper : Active
          1 :  10.0 :           x[1]*y[1] + x[2]*y[1] + x[3]*y[1] + x[4]*y[1] + x[5]*y[1] + x[6]*y[1] + x[7]*y[1] + x[8]*y[1] + x[9]*y[1] + x[10]*y[1] :  +Inf :   True
          2 :  20.0 :           x[1]*y[2] + x[2]*y[2] + x[3]*y[2] + x[4]*y[2] + x[5]*y[2] + x[6]*y[2] + x[7]*y[2] + x[8]*y[2] + x[9]*y[2] + x[10]*y[2] :  +Inf :   True
          3 :  30.0 :           x[1]*y[3] + x[2]*y[3] + x[3]*y[3] + x[4]*y[3] + x[5]*y[3] + x[6]*y[3] + x[7]*y[3] + x[8]*y[3] + x[9]*y[3] + x[10]*y[3] :  +Inf :   True
          4 :  40.0 :           x[1]*y[4] + x[2]*y[4] + x[3]*y[4] + x[4]*y[4] + x[5]*y[4] + x[6]*y[4] + x[7]*y[4] + x[8]*y[4] + x[9]*y[4] + x[10]*y[4] :  +Inf :   True
          5 :  50.0 :           x[1]*y[5] + x[2]*y[5] + x[3]*y[5] + x[4]*y[5] + x[5]*y[5] + x[6]*y[5] + x[7]*y[5] + x[8]*y[5] + x[9]*y[5] + x[10]*y[5] :  +Inf :   True
          6 :  60.0 :           x[1]*y[6] + x[2]*y[6] + x[3]*y[6] + x[4]*y[6] + x[5]*y[6] + x[6]*y[6] + x[7]*y[6] + x[8]*y[6] + x[9]*y[6] + x[10]*y[6] :  +Inf :   True
          7 :  70.0 :           x[1]*y[7] + x[2]*y[7] + x[3]*y[7] + x[4]*y[7] + x[5]*y[7] + x[6]*y[7] + x[7]*y[7] + x[8]*y[7] + x[9]*y[7] + x[10]*y[7] :  +Inf :   True
          8 :  80.0 :           x[1]*y[8] + x[2]*y[8] + x[3]*y[8] + x[4]*y[8] + x[5]*y[8] + x[6]*y[8] + x[7]*y[8] + x[8]*y[8] + x[9]*y[8] + x[10]*y[8] :  +Inf :   True
          9 :  90.0 :           x[1]*y[9] + x[2]*y[9] + x[3]*y[9] + x[4]*y[9] + x[5]*y[9] + x[6]*y[9] + x[7]*y[9] + x[8]*y[9] + x[9]*y[9] + x[10]*y[9] :  +Inf :   True
         10 : 100.0 : x[1]*y[10] + x[2]*y[10] + x[3]*y[10] + x[4]*y[10] + x[5]*y[10] + x[6]*y[10] + x[7]*y[10] + x[8]*y[10] + x[9]*y[10] + x[10]*y[10] :  +Inf :   True

6 Declarations: T x y OBJ cons_index cons
[Finished in 320ms]
AirSquid
  • 10,214
  • 2
  • 7
  • 31
  • Regarding the RangeSet usage I need it to have that indexing thanks for doing M[t-1]. I've placed your code in colab and the error didn't appear anymore now there is a new problem regarding ipopt. I've tried this solution and it still doesn't work https://stackoverflow.com/questions/59870272/is-there-a-way-to-run-ipopt-on-colab give the following error ApplicationError: No executable found for solver 'ipopt' do you have any idea how to solve this? – Anas May 12 '23 at 10:33