简体   繁体   English

PYOMO 错误:KeyError:“索引'0'对索引组件'y'无效”

[英]PYOMO ERROR: KeyError: “Index '0' is not valid for indexed component 'y' ”

This is the hole Error:这是孔错误:

ERROR: Rule failed when generating expression for objective FObj: KeyError:
    "Index '4' is not valid for indexed component 'y'"
ERROR: Constructing component 'FObj' from data=None failed:
        KeyError: "Index '4' is not valid for indexed component 'y'"

I've proved everything, checking every RangeSet and it's ok so I don't why it doesn't work well.我已经证明了一切,检查了每个 RangeSet 并且没关系,所以我不知道为什么它不能很好地工作。 Thanks for reading this, if anyone could help...感谢您阅读本文,如果有人可以帮助...

from pyomo.environ import *
from pyomo.opt import SolverFactory
from pyomo.core.base.PyomoModel import AbstractModel
from pyomo.core.base.constraint import Constraint
from pyomo.core.base.set import RangeSet
#import pyomo.dae
import numpy as np
import logging 
logging.getLogger('pyomo.core').setLevel(logging.ERROR)

   model = AbstractModel()

   model.personas = RangeSet(0, 29)
   model.sabados = RangeSet(0,3)

   model.y = Var(model.personas,model.sabados, within = Binary)


   def ObjFunction(model):
      return sum(model.y[i][s] for i in model.personas for s in model.sabados)
   model.FObj= Objective(rule=ObjFunction, sense = maximize)

Problem discovered.发现问题。 I think you must have just changed the model type to Abstract as when I change it back to Concrete the problem with y shows up.我认为您必须刚刚将 model 类型更改为Abstract ,因为当我将其更改回Concrete时,出现了y的问题。

You are indexing model.y with double indexing (Python standard).您正在使用双索引(Python 标准)索引model.y Pyomo... for whatever reason... uses comma separated indices for multiple indexing. Pyomo ...无论出于何种原因...使用逗号分隔的索引进行多个索引。 Note the change in my code below.请注意下面我的代码中的更改。 If this is a head-hurter, I've built models and put the indices in a tuple just to keep myself sane.如果这是一个令人头疼的问题,我已经建立了模型并将索引放在一个元组中,只是为了让自己保持清醒。 Such as: model.y[(i, s)] which is unnecessary, but works and makes it look more distinct for pyomo.例如: model.y[(i, s)]这是不必要的,但它可以工作并使 pyomo 看起来更明显。

Couple other notes...结合其他注意事项...

  • I removed some of the unnecceary imports.我删除了一些不必要的进口。 One was causing some kind of warning.一个是引起某种警告。
  • I chopped down your indices just to see a smaller printout我砍掉了你的索引只是为了看到一个更小的打印输出

from pyomo.environ import *
from pyomo.opt import SolverFactory
#from pyomo.core.base.PyomoModel import AbstractModel
#from pyomo.core.base.constraint import Constraint
#from pyomo.core.base.set import RangeSet
#import pyomo.dae
import numpy as np
import logging 
#logging.getLogger('pyomo.core').setLevel(logging.ERROR)

model = ConcreteModel()

model.personas = RangeSet(0, 3)
model.sabados = RangeSet(0,2)

model.y = Var(model.personas,model.sabados, within = Binary)


def ObjFunction(model):
  return sum(model.y[i,s] for i in model.personas for s in model.sabados)
model.FObj= Objective(rule=ObjFunction, sense = maximize)

model.pprint()

Yields:产量:

1 Set Declarations
    y_index : Dim=0, Dimen=2, Size=12, Domain=None, Ordered=True, Bounds=None
        Virtual

2 RangeSet Declarations
    personas : Dim=0, Dimen=1, Size=4, Domain=Integers, Ordered=True, Bounds=(0, 3)
        Virtual
    sabados : Dim=0, Dimen=1, Size=3, Domain=Integers, Ordered=True, Bounds=(0, 2)
        Virtual

1 Var Declarations
    y : Size=12, Index=y_index
        Key    : Lower : Value : Upper : Fixed : Stale : Domain
        (0, 0) :     0 :  None :     1 : False :  True : Binary
        (0, 1) :     0 :  None :     1 : False :  True : Binary
        (0, 2) :     0 :  None :     1 : False :  True : Binary
        (1, 0) :     0 :  None :     1 : False :  True : Binary
        (1, 1) :     0 :  None :     1 : False :  True : Binary
        (1, 2) :     0 :  None :     1 : False :  True : Binary
        (2, 0) :     0 :  None :     1 : False :  True : Binary
        (2, 1) :     0 :  None :     1 : False :  True : Binary
        (2, 2) :     0 :  None :     1 : False :  True : Binary
        (3, 0) :     0 :  None :     1 : False :  True : Binary
        (3, 1) :     0 :  None :     1 : False :  True : Binary
        (3, 2) :     0 :  None :     1 : False :  True : Binary

1 Objective Declarations
    FObj : Size=1, Index=None, Active=True
        Key  : Active : Sense    : Expression
        None :   True : maximize : y[0,0] + y[0,1] + y[0,2] + y[1,0] + y[1,1] + y[1,2] + y[2,0] + y[2,1] + y[2,2] + y[3,0] + y[3,1] + y[3,2]

5 Declarations: personas sabados y_index y FObj
[Finished in 2.6s]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pyomo KeyError:“访问索引组件时出错:索引'('student_5','company_3','meetingtime_1')'对数组组件'var_X'无效” - Pyomo KeyError: “Error accessing indexed component: Index '('student_5', 'company_3', 'meetingtime_1')' is not valid for array component 'var_X'” 索引对索引组件无效。 pyomo 中带有双索引的参数 - Index is not valid for indexed component. Parameter with double indexing in pyomo Pyomo无法使用索引集索引组件 - Pyomo Cannot index a component with an indexed set Pyomo:从 json 存档中加载三个维度集数据错误:无法索引具有索引集的组件 - Pyomo: Loading three dimension set data from json archive error: Cannot index a component with an indexed set 源错误消息=“索引'('d1','i1')'对于索引组件'NFix'无效” - source error message=“Index '('d1', 'i1')' is not valid for indexed component 'NFix'” Pyomo ValueError:检索组件时出错 - Pyomo ValueError: Error retrieving component Pyomo:解决 DAE 时出错。 (关键错误:2447832121968) - Pyomo: Getting error for solving DAE. (KeyError: 2447832121968) Pyomo:获取键盘错误:4903576400 - Pyomo: Getting keyerror: 4903576400 由带有 Pyomo 的索引集索引的变量 - Variable indexed by an indexed Set with Pyomo Keyerror:“[[x,y,z]]都不在[index]中” - Keyerror: “none of [[x,y,z]] are in the [index]”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM