Я новичок в модели pyomo. Я внимательно следил за учебником по документации pyomo и попытался создать AbstractModel со следующим кодом в abstract1.py1.
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 следующим образом. (base) D: \ PostDocResearch \ Python> pyomo solution 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': {}}]}
Может ли кто-нибудь помочь мне разобраться?
Спасибо