Samira Fallah
Samira Fallah

Reputation: 3

How to add to a set in pyomo

I am trying to run this code (some important part of the code is here):

Master = AbstractModel()
Master.intIndices = Set(initialize=INTVARS)
Master.constraintSet = Set(initialize=CONS)
Master.conIndices = Set(initialize=CONVARS)
Master.intPartList = Set()

Master.dualVarSet = Master.constraintSet * Master.intPartList

Master.theta = Var(domain=Reals, bounds = (None, None))
Master.intVars = Var(Master.intIndices, domain=NonNegativeIntegers, bounds=(0, 10))
Master.dualVars = Var(Master.dualVarSet, domain=Reals, bounds = (None, None))
max_iters = 1000
opt = SolverFactory("couenne")
for i in range(max_iters):
    Master.intPartList.add(i)

but it shows me this error on the last line:

RuntimeError: Cannot access add on AbstractOrderedSimpleSet 'intPartList' before it has been constructed (initialized).

Can somebody help me?

Upvotes: 0

Views: 521

Answers (1)

AirSquid
AirSquid

Reputation: 11938

You are not initializing Master.intPartList with any data, so you can't update it like that. However, if you make your model a concrete model, and supply an initialization for your set you can...

In [11]: from pyomo.environ import *                                            

In [12]: m2 = ConcreteModel()                                                   

In [13]: m2.X = Set(initialize=[1,])                                            

In [14]: m2.X.add(2)                                                            
Out[14]: 1

In [15]: m2.X.pprint()                                                          
X : Size=1, Index=None, Ordered=Insertion
    Key  : Dimen : Domain : Size : Members
    None :     1 :    Any :    2 : {1, 2}

In [16]:     

Upvotes: 1

Related Questions