Python Pyomo运行ploblem,即使使用abstractmodel

Python Pyomo运行ploblem,即使使用abstractmodel,python,pyomo,Python,Pyomo,我对pyomo模型不熟悉。我密切关注关于pyomo文档的教程,并尝试使用abstract1.py1中的以下代码创建一个AbstractModel from __future__ import division from pyomo.environ import * model = AbstractModel() model.m = Param(within = NonNegativeIntegers) model.n = Param(within = NonNegativeIntegers)

我对pyomo模型不熟悉。我密切关注关于pyomo文档的教程,并尝试使用abstract1.py1中的以下代码创建一个AbstractModel

from __future__ import division
from pyomo.environ import *

model = AbstractModel()

model.m = Param(within = NonNegativeIntegers)
model.n = Param(within = NonNegativeIntegers)

model.I = RangeSet(1, model.m)
model.J = RangeSet(1, model.n)

model.a = Param(model.I, model.J)
model.b = Param(model.I)
model.c = Param(model.J)

# the next line declares a variable indexed by the set J
model.x = Var(model.J, domain=NonNegativeReals)


def obj_expression(model):
    return summation(model.c, model.x)

model.OBJ = Objective(rule=obj_expression)

#Declaration of constraint
def ax_constraint_rule(model, i):
    # return the expression for the constraint for i
    return sum(model.a[i,j] * model.x[j] for j in model.J) >= model.b[i]


# the next line creates one constraint for each member of the set model.I
model.AxbConstraint = Constraint(model.I, rule=ax_constraint_rule)
然后我创建了AMPL格式的数据,这正是abstract1.dat中的示例

# one way to input the data in AMPL format
# for indexed parameters, the indexes are given before the value

param m := 1 ;
param n := 2 ;

param a :=
1 1 3
1 2 4
;

param c:=
1 2
2 3
;

param b := 1 1 ;
然后,我尝试通过Anaconda命令提示符运行模型,如下所示。 (基础)D:\PostDocResearch\Python>pyomo solve abstract1.py abstract1.dat--solver=glpk 不幸的是,我遇到了以下错误消息

[    0.00] Setting up Pyomo environment
[    0.00] Applying Pyomo preprocessing actions
[    0.00] Creating model
[    0.02] Pyomo Finished
ERROR: Unexpected exception while running model:
        'AbstractModel' object has no attribute 'a'
errorcode: 1
retval: None
后来我就这样了

 (base) D:\PostDocResearch\Python>pyomo solve abstract1.py abstract1.dat --solver=glpk
[    0.00] Setting up Pyomo environment
[    0.00] Applying Pyomo preprocessing actions
[    0.80] Creating model
[    0.82] Applying solver
[    0.89] Processing results
    Number of solutions: 1
    Solution Information
      Gap: 0.0
      Status: feasible
      Function Value: 0.666666666666666
    Solver results file: results.yml
[    0.89] Applying Pyomo postprocessing actions
[    0.89] Pyomo Finished
errorcode: 0
retval: instance: <pyomo.core.base.PyomoModel.ConcreteModel object at 0x000001CCDE0EF818>
local:
    time_initial_import: 0.799086332321167
    usermodel: <module 'abstract1' from 'D:\\PostDocResearch\\Python\\abstract1.py'>
options: <pyutilib.misc.config.ConfigDict object at 0x000001CCDE0B7408>
results: {'Problem': [{'Name': 'unknown', 'Lower bound': 0.666666666666667, 'Upper bound': 0.666666666666667, 'Number of objectives': 1, 'Number of constraints': 2, 'Number of variables': 3, 'Number of nonzeros': 3, 'Sense': 'minimize'}], 'Solver': [{'Status': 'ok', 'Termination condition': 'optimal', 'Statistics': {'Branch and bound': {'Number of bounded subproblems': 0, 'Number of created subproblems': 0}}, 'Error rc': 0, 'Time': 0.04153299331665039}], 'Solution': [OrderedDict([('number of solutions', 1), ('number of solutions displayed', 1)]), {'Gap': 0.0, 'Status': 'feasible', 'Message': None, 'Problem': {}, 'Objective': {'OBJ': {'Value': 0.666666666666666}}, 'Variable': {'x[1]': {'Value': 0.333333333333333}, 'x[2]': {'Value': 0.0}}, 'Constraint': {}}]}
(基本)D:\PostDocResearch\Python>pyomo solve abstract1.py abstract1.dat--solver=glpk
[0.00]设置Pyomo环境
[0.00]应用Pyomo预处理操作
[0.80]创建模型
[0.82]应用解算器
[0.89]处理结果
解决方案数量:1
解决方案信息
差距:0.0
现状:可行
功能值:0.666
解算器结果文件:results.yml
[0.89]应用Pyomo后处理操作
[0.89]毕莫完工
错误代码:0
retval:实例:
本地:
初始导入时间:0.799086332321167
用户模型:
选项:
结果:{'Problem':[{'Name':'unknown','Lower bound':0.667,'Upper bound':0.667,'Number of objectives':1,'Number of constraints':2,'Number of variables':3,'Sense':'minimize','Solver':[{'Status':'ok','Termination condition':'optimal','Statistics':{'Branch and bound':{'Number of bounded subproblems':0,'Number of created subproblems':0},'Error rc':0,'Time':0.0415329931665039},'Solution':[orderedict([('Number of solutions',1),('Number of solutions displated',1)],{'Gap':0.0,'Status':'exability','Message None',Problem':{},'Objective':{'OBJ':{'Value':0.66666},'Variable':{'x'[1] “:{'Value':0.333},'x[2]':{'Value':0.0},'Constraint':{}]}
有人能帮我弄清楚吗


谢谢

我刚刚用cbc solver运行了你的抽象模型,它运行得很好。也许在你第一次运行时,你键入了数据文件的名称或其他什么。谢谢@Jeff H。它现在运行,但在
错误代码之后我无法停止消息。
还有一件事我无法安装
cbc
编译器。我刚刚运行了你的抽象模型el与cbc solver一起运行,效果很好。也许在您第一次运行时,您键入了数据文件的名称或其他内容。谢谢@Jeff H。它现在运行,但在
错误代码之后,我无法停止消息。
另外,我无法安装
cbc
编译器。