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

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)

错误:为约束对象生成表达式时规则失败 index (1, 1): AttributeError: 'list' 对象没有属性 'is_expression_type'

错误:从数据=None 构建组件'objconst' 失败: AttributeError: 'list' 对象没有属性 'is_expression_type'

使用此索引集 model.x 构建约束的正确方法是什么?

这应该让你继续。做了一些调整。您应该使用 model.x 创建最后一个约束,因为您不能在规则语句中引用值 i。另外,我重新处理了您的 k 值。这编译。

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)