[英]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...结合其他注意事项...
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.