How to add to a set in pyomo?

85 views
Skip to first unread message

Samira Fallah

unread,
Mar 23, 2021, 11:59:29 AM3/23/21
to Pyomo Forum
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).

Samira Fallah

unread,
Mar 23, 2021, 12:00:00 PM3/23/21
to Pyomo Forum
Can somebody help me?

Samira Fallah

unread,
Mar 24, 2021, 3:00:24 PM3/24/21
to Pyomo Forum
I fixed the issue. The issue was that we were trying to change an object of the master before creating an instance which is not possible in the abstractModel. The quick way to fix it was to create a temporary python list and initialize the master object with that list and change the python list in each iteration. In summary, changing from:

Master.intPartList = Set()

for i in range(max_iters):
  Master.intPartList.add(i)
  instance = Master.create_instance()

to:

listTemp = []
Master.intPartList = Set(initialize=listTemp)
for i in range(max_iters):
  listTemp.append(i)
  instance = Master.create_instance()
Reply all
Reply to author
Forward
0 new messages