您好我正在测试我的Pyomo安装,当我尝试解决任何优化问题时,它会返回一个错误,内容如下:
"退出pyomo求解:未定义模型,并且' pyomo_create_model'模块test.py"
中未提供
我正在执行
pyomo解决test.py diet.dat --solver = glpk
from pyomo.environ import *
infinity = float('inf')
model = AbstractModel()
# Foods
model.F = Set()
# Nutrients
model.N = Set()
# Cost of each food
model.c = Param(model.F, within=PositiveReals)
# Amount of nutrient in each food
model.a = Param(model.F, model.N, within=NonNegativeReals)
# Lower and upper bound on each nutrient
model.Nmin = Param(model.N, within=NonNegativeReals, default=0.0)
model.Nmax = Param(model.N, within=NonNegativeReals, default=infinity)
# Volume per serving of food
model.V = Param(model.F, within=PositiveReals)
# Maximum volume of food consumed
model.Vmax = Param(within=PositiveReals)
# Number of servings consumed of each food
model.x = Var(model.F, within=NonNegativeIntegers)
# Minimize the cost of food that is consumed
def cost_rule(model):
return sum(model.c[i]*model.x[i] for i in model.F)
model.cost = Objective(rule=cost_rule)
# Limit nutrient consumption for each nutrient
def nutrient_rule(model, j):
value = sum(model.a[i,j]*model.x[i] for i in model.F)
return model.Nmin[j] <= value <= model.Nmax[j]
model.nutrient_limit = Constraint(model.N, rule=nutrient_rule)
# Limit the volume of food consumed
def volume_rule(model):
return sum(model.V[i]*model.x[i] for i in model.F) <= model.Vmax
model.volume = Constraint(rule=volume_rule)
和我的diet.dat文件是
param: F: c V :=
"Cheeseburger" 1.84 4.0
"Ham Sandwich" 2.19 7.5
"Hamburger" 1.84 3.5
"Fish Sandwich" 1.44 5.0
"Chicken Sandwich" 2.29 7.3
"Fries" .77 2.6
"Sausage Biscuit" 1.29 4.1
"Lowfat Milk" .60 8.0
"Orange Juice" .72 12.0 ;
param Vmax := 75.0;
param: N: Nmin Nmax :=
Cal 2000 .
Carbo 350 375
Protein 55 .
VitA 100 .
VitC 100 .
Calc 100 .
Iron 100 . ;
param a:
Cal Carbo Protein VitA VitC Calc Iron :=
"Cheeseburger" 510 34 28 15 6 30 20
"Ham Sandwich" 370 35 24 15 10 20 20
"Hamburger" 500 42 25 6 2 25 20
"Fish Sandwich" 370 38 14 2 0 15 10
"Chicken Sandwich" 400 42 31 8 15 15 8
"Fries" 220 26 3 0 15 0 2
"Sausage Biscuit" 345 27 15 4 0 20 15
"Lowfat Milk" 110 12 9 10 4 30 0
"Orange Juice" 80 20 1 2 120 2 2 ;
如何修复无模型错误?在test.py的第3行代码中定义了模型
答案 0 :(得分:0)
我认为您的求解器有问题,或者您没有将test.py和diet.dat放在同一目录中。
以下是我的结果:
hidden@hidden:~/Desktop$ pyomo solve test.py diet.dat --solver=gurobi
[ 0.00] Setting up Pyomo environment
[ 0.00] Applying Pyomo preprocessing actions
[ 0.00] Creating model
[ 0.03] Applying solver
[ 0.65] Processing results
Number of solutions: 1
Solution Information
Gap: 0.0
Status: optimal
Function Value: 15.049999999999999
Solver results file: results.json
[ 0.65] Applying Pyomo postprocessing actions
[ 0.65] Pyomo Finished
PS:我复制/粘贴你的代码并没有改变任何内容。