pythonpyomoindexed

AttributeError: 'list' object has no attribute 'is_expression_type', What does this mean in pyomo?


model = pyo.ConcreteModel()


# Sets
model.k = pyo.RangeSet(136)
model.i = pyo.Set(initialize = [1,2,3,4])
model.n = pyo.Set(model.i,initialize = {1:[1,2,3],2:[1,2,3,4,5],3:[1,2,3],4:[1,2]})

def set_init(model):
    return [(i,n) for i in model.i for n in model.n[i]]
model.x = pyo.Set(dimen = 2,initialize = set_init)

#Param
model.t = pyo.Param(model.k,initialize = np.arange(0, 68, 0.5).tolist())
model.tauB = pyo.Param(model.x)

#Var
model.Sb = pyo.Var(model.k,model.x,within=pyo.Binary)
model.Db = pyo.Var(model.x,domain = pyo.NonNegativeReals)

def rule3(model,i,n):
    return model.Db[i,n]>=sum(model.Sb[k,i,n]*model.t[k] for k in model.k) - model.tauB[i,n]
model.objconst = pyo.Constraint(model.i,model.n[i],rule=rule3)

ERROR: Rule failed when generating expression for Constraint objconst with index (1, 1): AttributeError: 'list' object has no attribute 'is_expression_type'

ERROR: Constructing component 'objconst' from data=None failed: AttributeError: 'list' object has no attribute 'is_expression_type'

What is the correct way to use this indexed set model.x to build the constraint?


Solution

  • This should get you going. Made several tweaks. You should use model.x for creating your last constraint as you cannot reference the value i in the rule statement. Also, I re-worked your k values. This compiles.

    import pyomo.environ as pyo
    import numpy as np
    model = pyo.ConcreteModel()
    
    k_data = { idx:item for idx, item in enumerate(np.arange(0, 68, 0.5))}
    
    
    # Sets
    model.k = pyo.Set(initialize=k_data.keys())
    model.i = pyo.Set(initialize = [1,2,3,4])
    model.n = pyo.Set(model.i,initialize = {1:[1,2,3],2:[1,2,3,4,5],3:[1,2,3],4:[1,2]})
    
    def set_init(model):
        return [(i,n) for i in model.i for n in model.n[i]]
    model.x = pyo.Set(dimen = 2,initialize = set_init)
    
    #Param
    model.t = pyo.Param(model.k,initialize = k_data)
    model.tauB = pyo.Param(model.x, initialize = 0)  # <-- need some values for this
    
    #Var
    model.Sb = pyo.Var(model.k,model.x,within=pyo.Binary)
    model.Db = pyo.Var(model.x,domain = pyo.NonNegativeReals)
    
    def rule3(model,i,n):
        return model.Db[i,n]>=sum(model.Sb[k,i,n]*model.t[k] for k in model.k) - model.tauB[i,n]
    model.objconst = pyo.Constraint(model.x,rule=rule3)