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)
错误:为约束objconst生成表达式时规则失败index (1,1): AttributeError: 'list' object has no attribute"is_expression_type">
错误:从数据中构造组件objconst =None失败AttributeError: 'list'对象没有属性'is_expression_type'
如何正确使用这个索引集模型?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)