“不能将标量组件 '' 视为索引组件”

问题描述 投票:0回答:1

这是我用于运营计划优化的几行代码。我有两个用于产品每月需求的字典。我用这两个字典创建了两个参数。正如 m 我已经定义了月份。之后,我想将一些变量与需求的参数相乘(以下约束),但出现错误KeyError:“无法将标量组件'tsi'视为索引组件” 我不知道这有什么问题。我尝试了很多方法来解决它,但没有任何效果。

import pyomo.environ as pyo                                                   
from pyomo.opt import SolverFactory

def run():
    model = pyo.ConcreteModel()

    Dict1 = {1:28800,2:26200,3:20900,4:12660,5:25770,6:12350,7:28200,8:26200,9:30000,10:7400,11:24000,12:22200}
    
    Dict2 = {1:5000,2:5700,3:4000,4:1540,5:9000,6:1800,7:7250,8:4000,9:8000,10:3000,11:5000,12:8100}

    model.m = pyo.Param(initialize = 12)                                                                                    
    model.k = pyo.Param(initialize=2)                                         
    model.i = pyo.Param(initialize=3)                                        
    model.j = pyo.Param(initialize=3)                                      
    model.l = pyo.Param(initialize=2)
    model.tsi = pyo.Param(initialize = Dict1)
    model.trc = pyo.Param(initialize = Dict2)

    model.setm = pyo.RangeSet(1, model.m)
    model.setk = pyo.RangeSet(1, model.k)
    model.seti = pyo.RangeSet(1, model.i)
    model.setj = pyo.RangeSet(1, model.j)
    model.setl = pyo.RangeSet(1, model.l)

def sixteenthRule(model, m):
    return ([model.g[1,m] for m in model.setm]) == ([model.trc[m] for m in range(1,13)])*([model.G[1,m] for m in model.setm]) + ([model.tsi[m] for m in range [1,13]])

def seventeenthRule(model, m):
    return ([model.g[2,m] for m in model.setm]) == ([model.trc[m] for m in range(1,13)])*([1-model.G[1,m] for m in model.setm])

def twentyeightthRule(model, m):
    return ([model.z[1,1,m] for m in model.setm]) == ([model.z[1,1,m-1] for m in model.setm]) + ([model.p[1,1,m] for m in model.setm]) - ([model.tsi[m] for m in range [1,13]])

def thirtythRule(model, m):
    return ([model.z[1,2,m] for m in model.setm]) == ([model.z[1,2,m-1] for m in model.setm]) + ([model.p[2,2,m] for m in model.setm]) + ([model.dr[2,m] for m in model.setm]) - ([model.G[1,m] for m in model.setm])*([model.trc[m] for m in range [1,13]]) + ([model.p[1,2,m] for m in model.setm])
python dictionary parameters constraints pyomo
1个回答
0
投票

参数

model.tsi
是您定义的标量参数,即它只能采用单个值,例如5 或 10。

Dict1
字典有多个值。如果您需要通过所有这些,根据问题的具体细节,您必须首先为
Set
的所有键定义一个 Pyomo
Dict1
,例如:

model.set_tsi = pyo.RangeSet(1, 12)

然后定义参数如下:

model.tsi = model.tsi = pyo.Param(model.set_tsi, initialize = Dict1)

© www.soinside.com 2019 - 2024. All rights reserved.