发现问题。我认为您必须将模型类型更改为Abstract
,因为当我将其更改回Concrete
时,会出现y
的问题。
这是孔错误:
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'"
我已经证明了所有内容,检查了每个RangeSet,而且还可以,所以我不知道为什么它不能很好地工作。感谢您阅读本文,如果有人可以帮助...
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)
这是孔错误:错误:为目标FObj生成表达式时规则失败:KeyError:“索引'4'对索引组件'y'无效”错误:从...构造组件'FObj']]] >
发现问题。我认为您必须将模型类型更改为Abstract
,因为当我将其更改回Concrete
时,会出现y
的问题。
您正在使用双索引(Python标准)为model.y
编制索引。 Pyomo ...由于任何原因...使用逗号分隔的索引进行多个索引。请注意下面我的代码中的更改。如果这是一个障碍,我会建立模型并将索引放在一个元组中,以保持理智。例如:model.y[(i, s)]
是不必要的,但是可以使pyomo变得更独特。
加上其他注释...
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()
收益率:
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]
发现问题。我认为您必须将模型类型更改为Abstract
,因为当我将其更改回Concrete
时,会出现y
的问题。