Search code examples
pyomo

Cannot iterate over AbstractOrderedScalarSet before it has been constructed (initialized)


I have just started with pyomo and Python, and trying to create a simple model but have a problem with adding a constraint.

I followed the following example from GitHub https://github.com/brentertainer/pyomo-tutorials/blob/master/introduction/02-lp-pyomo.ipynb

import pandas as pd
import pyomo.environ as pe
import pyomo.opt as po

#DATA
T=3;
CH=2;


time = ['t{0}'.format(t+1) for t in range(T)]
CHP=['CHP{0}'.format(s+1) for s in range(CH)]

#Technical characteristic

heat_maxprod = {'CHP1': 250,'CHP2': 250} #Only for CHPS

#MODEL
seq=pe.ConcreteModel
### SETS
seq.CHP = pe.Set(initialize = CHP) 
seq.T = pe.Set(initialize = time)

### PARAMETERS
seq.heat_maxprod = pe.Param(seq.CHP, initialize = heat_maxprod) #Max heat production

### VARIABLES

seq.q_DA=pe.Var(seq.CHP, seq.T, domain=pe.Reals)

### CONSTRAINTS

##Maximum and Minimum Heat Production

seq.Heat_DA1 = pe.ConstraintList()
for t in seq.T:
    for s in seq.CHP:
        seq.Heat_DA1.add( 0 <= seq.q_DA[s,t])
        
seq.Heat_DA2 = pe.ConstraintList()
for t in seq.T:
    for s in seq.CHP:
        seq.Heat_DA2.add( seq.q_DA[s,t] <= seq.heat_maxprod[s])


### OBJECTIVE 

       
seq.obj=Objective(expr=sum( seq.C_fuel[s]*(seq.rho_heat[s]*seq.q_DA[s,t]) for t in seq.T for s in seq.CHP))
 

When I run the program I am getting the following error:

RuntimeError: Cannot iterate over AbstractOrderedScalarSet 'AbstractOrderedScalarSet' before it has been constructed (initialized): 'iter' is an attribute on an Abstract component and cannot be accessed until the component has been fully constructed (converted to a Concrete component) using AbstractModel.create_instance() or AbstractOrderedScalarSet.construct().

Can someone, please, help with an issue? Thanks!

P.S. I know that the resulting answer for the problem is zero, I just want to make it work in terms of correct syntaxis.


Solution

  • In this line of code:

    seq=pe.ConcreteModel
    

    You are missing parenthesis. So, I think you are just creating an alias for the function instead of calling it.

    Try:

    seq=pe.ConcreteModel()