我得到以下基本设置:
model.X = ['A','B']
model.stage = range(1,3)
model.Y = ['C','D']
model.area = pyo.Var(model.X, model.stage, model.Y, initialize = 2, bounds=(0,100))
但是我希望变量区域可以用不同的值初始化,例如
model.area['A',1,'C'] initialize = 1
model.area['A',1,'D'] initialize = 1
model.area['B',1,'C'] initialize = 2
model.area['B',1,'D'] initialize = 2
model.area['A',2,'C'] initialize = 1
model.area['A',2,'D'] initialize = 1
model.area['B',2,'C'] initialize = 2
model.area['B',2,'D'] initialize = 2
我不知道如何使用不同的子索引迭代定义的变量区域,任何人都可以分享一些想法吗? 谢谢
答案 0 :(得分:0)
如果model
对象是ConcreteModel
,则只需编写
model.area['A',1,'C'].value = 1
model.area['B',1,'C'].value = 2
如果它是AbstractModel
,则可以在构造的实例上执行相同操作,也可以定义规则并将其分配给initialize关键字:
def area_init(m,i,j,k):
if (i,j,k) in [('A',1,'C'),...]:
return 1
elif ...:
return 2
model.area = pyo.Var(model.X,model.stage,model.Y,initialize=area_init,bounds=(0,100))