如何求解错误检索组件索引索引[1]:尚未构造组件
我正在遇到此错误(错误检索组件索引索引[1]:尚未构造组件。),我无法弄清楚下面的代码在哪里出错,我正在pyomo中编码一个线性程序,我没有精通Python,当我学习线性编程时,我基本上是在重复。你能帮我吗?错误似乎在定义目标函数的行中...请帮助
''
#!/usr/bin/env python3
- - 编码:UTF-8 - -
“”“” 在太阳上创建6月12日20:01:39 2022
@author:Israathiab ”“”
import pyomo.environ as pyo
def pyomoresults(QEMCAProfitModel): # define a function to print optimization results
""""Print results of the optimization of model 'QEMCAProfitModel' including
objective value at optimum, optimal decision variable values and
dual/slack information on constraints"""
print('')
print('=========================')
print('QEMCAProfitModel Maximization Solution')
print('=========================')
print('')
print('Objective Function Value at Optimum')
# loop over objective objects in the model
for f in QEMCAProfitModel.component_objects(pyo.Objective, active=True):
print ('Objective', f) # print the name of the objective
# loop over subobjectives (in this course we will only have one)
for index in f:
print (' ', index, pyo.value(f[index]))
print('')
print('Decision Variable Values at Optimum')
# loop over decision variable objects in model
for v in QEMCAProfitModel.component_objects(pyo.Var, active=True):
print ('Variable', v) # print variable name
for index in v: # loop over index of variable object
# print index/optimal values
print (' ', index, pyo.value(v[index]))
print('')
print ('Duals/Shadow Prices at Optimum')
# loop over constraint objects in model
for d in QEMCAProfitModel.component_objects(pyo.Constraint, active=True):
print ('Constraint',d) # print constraint name
for index in d: # loop over index of constraint object
# print dual value of constraint
print (' ', index, QEMCAProfitModel.dual[d[index]])
print('')
print ("Slack at Optimum")
# loop over constraint objects in model
for i in QEMCAProfitModel.component_objects(pyo.Constraint, active=True):
print (" Constraint",Ci) # print constraint name
for index in Ci: # loop over index of constraint object
# print constraint slack information
print (" ", index, Ci[index].lslack(), ' - ', Ci[index].uslack())
#Define Concrete model
QEMCAProfitModel = pyo.ConcreteModel
#Define variable dictionary for decision variable index
#Crop index
I = [1,2,3,4,5,6]
#Define parameters
#Define mean profit per crop - ETB
mPi = {1:15635.2, 2:9571.36, 3:13392.6, 4:39834.6, 5:49074.1, 6:11365.6}
#Define mean cost per hectar of crop - ETB/Hecatre
Ci = {1:10467.37, 2:8160.47, 3:12392.22, 4:5702.01, 5:8265.88, 6:8514.31}
#Define objective function
QEMCAProfitModel.L = pyo.Var(I,within=pyo.NonNegativeReals)
QEMCAProfitModel.Profit = pyo.Objective(expr=sum(mPi[i] * QEMCAProfitModel.L[i] for i `in I), sense=pyo.maximize)`
#Profit un EBT
#Define constraints
def Budget_rule(QEMCAProfitModel, i):
return sum(Ci[i] * QEMCAProfitModel.L[i] for i in I) <= 850636
QEMCAProfitModel.Budget_rule = pyo.Constraint(I, rule=Budget_rule)
def Land_rule(QEMCAProfitModel, i):
return sum(QEMCAProfitModel.L[i] for i in I) <= 47.77
QEMCAProfitModel.Land_rule = pyo.Constraint(I, rule=Land_rule)
def Land_rule_min(QEMCAProfitModel, i):
return sum(QEMCAProfitModel.L[i] for i in I) >= 0
QEMCAProfitModel.Land_rule_min = pyo.Constraint(I, rule=Land_rule_min)
def Consumption_rule_1(QEMCAProfitModel, i):
return 4560.64 * QEMCAProfitModel.L[1] >= 51250
QEMCAProfitModel.Consumption_rule_1 = pyo.Constraint(I, rule=Consumption_rule_1)
def Consumption_rule_2(QEMCAProfitModel, i):
return 5764.71 * QEMCAProfitModel.L[2] >= 36710
QEMCAProfitModel.Consumption_rule_2 = pyo.Constraint(I, rule=Consumption_rule_2)
def Consumption_rule_3(QEMCAProfitModel, i):
return 9908.3 * QEMCAProfitModel.L[3] >= 30316
QEMCAProfitModel.Consumption_rule_3 = pyo.Constraint(I, rule=Consumption_rule_3)
def Consumption_rule_4(QEMCAProfitModel, i):
return 16614.81 * QEMCAProfitModel.L[4] >= 4850
QEMCAProfitModel.Consumption_rule_4 = pyo.Constraint(I, rule=Consumption_rule_4)
def Consumption_rule_5(QEMCAProfitModel, i):
return 20323.81 * QEMCAProfitModel.L[5] >= 4450
QEMCAProfitModel.Consumption_rule_5 = pyo.Constraint(I, rule=Consumption_rule_5)
def Consumption_rule_6(QEMCAProfitModel, i):
return 4400 * QEMCAProfitModel.L[6] >= 7100
QEMCAProfitModel.Consumption_rule_6 = pyo.Constraint(I, rule=Consumption_rule_6)
QEMCAProfitModel.pprint()
QEMCAProfitModel.dual = pyo.Suffix(direction=pyo.Suffix.IMPORT)
opt = pyo.SolverFactory('glpk')
results = opt.solve(QEMCAProfitModel)
QEMCAProfitModel.display()
QEMCAProfitModel.dual.display()
pyomoresults(QEMCAProfitModel)
'''
I'm getting this error (Error retrieving component IndexedVar[1]: The component has not been constructed.), and I can't figure out where I went wrong with my code below, I'm coding a linear program in pyomo, I'm not versed in python, I'm basically repeating when I learned for linear programming. Can you help me out? The error seems to be in the line defining the objective function... please help
'''
#!/usr/bin/env python3
-- coding: utf-8 --
"""
Created on Sun Jun 12 20:01:39 2022
@author: israathiab
"""
import pyomo.environ as pyo
def pyomoresults(QEMCAProfitModel): # define a function to print optimization results
""""Print results of the optimization of model 'QEMCAProfitModel' including
objective value at optimum, optimal decision variable values and
dual/slack information on constraints"""
print('')
print('=========================')
print('QEMCAProfitModel Maximization Solution')
print('=========================')
print('')
print('Objective Function Value at Optimum')
# loop over objective objects in the model
for f in QEMCAProfitModel.component_objects(pyo.Objective, active=True):
print ('Objective', f) # print the name of the objective
# loop over subobjectives (in this course we will only have one)
for index in f:
print (' ', index, pyo.value(f[index]))
print('')
print('Decision Variable Values at Optimum')
# loop over decision variable objects in model
for v in QEMCAProfitModel.component_objects(pyo.Var, active=True):
print ('Variable', v) # print variable name
for index in v: # loop over index of variable object
# print index/optimal values
print (' ', index, pyo.value(v[index]))
print('')
print ('Duals/Shadow Prices at Optimum')
# loop over constraint objects in model
for d in QEMCAProfitModel.component_objects(pyo.Constraint, active=True):
print ('Constraint',d) # print constraint name
for index in d: # loop over index of constraint object
# print dual value of constraint
print (' ', index, QEMCAProfitModel.dual[d[index]])
print('')
print ("Slack at Optimum")
# loop over constraint objects in model
for i in QEMCAProfitModel.component_objects(pyo.Constraint, active=True):
print (" Constraint",Ci) # print constraint name
for index in Ci: # loop over index of constraint object
# print constraint slack information
print (" ", index, Ci[index].lslack(), ' - ', Ci[index].uslack())
#Define Concrete model
QEMCAProfitModel = pyo.ConcreteModel
#Define variable dictionary for decision variable index
#Crop index
I = [1,2,3,4,5,6]
#Define parameters
#Define mean profit per crop - ETB
mPi = {1:15635.2, 2:9571.36, 3:13392.6, 4:39834.6, 5:49074.1, 6:11365.6}
#Define mean cost per hectar of crop - ETB/Hecatre
Ci = {1:10467.37, 2:8160.47, 3:12392.22, 4:5702.01, 5:8265.88, 6:8514.31}
#Define objective function
QEMCAProfitModel.L = pyo.Var(I,within=pyo.NonNegativeReals)
QEMCAProfitModel.Profit = pyo.Objective(expr=sum(mPi[i] * QEMCAProfitModel.L[i] for i `in I), sense=pyo.maximize)`
#Profit un EBT
#Define constraints
def Budget_rule(QEMCAProfitModel, i):
return sum(Ci[i] * QEMCAProfitModel.L[i] for i in I) <= 850636
QEMCAProfitModel.Budget_rule = pyo.Constraint(I, rule=Budget_rule)
def Land_rule(QEMCAProfitModel, i):
return sum(QEMCAProfitModel.L[i] for i in I) <= 47.77
QEMCAProfitModel.Land_rule = pyo.Constraint(I, rule=Land_rule)
def Land_rule_min(QEMCAProfitModel, i):
return sum(QEMCAProfitModel.L[i] for i in I) >= 0
QEMCAProfitModel.Land_rule_min = pyo.Constraint(I, rule=Land_rule_min)
def Consumption_rule_1(QEMCAProfitModel, i):
return 4560.64 * QEMCAProfitModel.L[1] >= 51250
QEMCAProfitModel.Consumption_rule_1 = pyo.Constraint(I, rule=Consumption_rule_1)
def Consumption_rule_2(QEMCAProfitModel, i):
return 5764.71 * QEMCAProfitModel.L[2] >= 36710
QEMCAProfitModel.Consumption_rule_2 = pyo.Constraint(I, rule=Consumption_rule_2)
def Consumption_rule_3(QEMCAProfitModel, i):
return 9908.3 * QEMCAProfitModel.L[3] >= 30316
QEMCAProfitModel.Consumption_rule_3 = pyo.Constraint(I, rule=Consumption_rule_3)
def Consumption_rule_4(QEMCAProfitModel, i):
return 16614.81 * QEMCAProfitModel.L[4] >= 4850
QEMCAProfitModel.Consumption_rule_4 = pyo.Constraint(I, rule=Consumption_rule_4)
def Consumption_rule_5(QEMCAProfitModel, i):
return 20323.81 * QEMCAProfitModel.L[5] >= 4450
QEMCAProfitModel.Consumption_rule_5 = pyo.Constraint(I, rule=Consumption_rule_5)
def Consumption_rule_6(QEMCAProfitModel, i):
return 4400 * QEMCAProfitModel.L[6] >= 7100
QEMCAProfitModel.Consumption_rule_6 = pyo.Constraint(I, rule=Consumption_rule_6)
QEMCAProfitModel.pprint()
QEMCAProfitModel.dual = pyo.Suffix(direction=pyo.Suffix.IMPORT)
opt = pyo.SolverFactory('glpk')
results = opt.solve(QEMCAProfitModel)
QEMCAProfitModel.display()
QEMCAProfitModel.dual.display()
pyomoresults(QEMCAProfitModel)
'''
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
定义模型对象时,您会缺少parens:
应该
没有parens,
qemcaprofitModel
是 classconcretemodel
而不是类的实例。触发组件“构造”的“魔术”(setAttr __
block数据实例上的方法)仅在block> block
instances(并且模型是一个块)上工作。请注意,同一方法还负责将属性名称复制到组件名称。由于没有触发,因此您在错误消息中看到默认组件名称(类名称,
indexedvar
)。You are missing parens when defining the model object:
should be
Without the parens,
QEMCAProfitModel
is the classConcreteModel
instead of an instance of the class. The "magic" that triggers component "construction" (the__setattr__
method on Block data instances) only works onBlock
instances (and a model is a block).Note that the same method is also responsible for copying the attribute name to the component name. Since that is not being triggered, you are seeing the default component name (the class name,
IndexedVar
) in the error message.