无法使用DataPortal从dat文件加载参数数据

时间:2019-04-25 20:57:32

标签: python pyomo

我正在尝试解决此处概述的饮食问题(https://nbviewer.jupyter.org/github/Pyomo/PyomoGallery/blob/master/diet/DietProblem.ipynb)。我很难将数据文件diet.dat导入到AbstractModel中。当我尝试运行下面在Spyder中编写的代码时,出现以下错误:

ValueError: Parameter 'Vmax' defined with '1' dimensions, but data has '1' values: [75.0]. Are you missing a value for a 1-dimensional index?"

我在做什么错了?

import pyomo.environ as pe
infinity = float('inf')

model = pe.AbstractModel()
data = pe.DataPortal()

# Sets
model.F = pe.Set()
model.N = pe.Set()

# Parameters
model.c = pe.Param(model.F, within=pe.PositiveReals) # cost of serving of each food F
model.a = pe.Param(model.F,model.N, within=pe.NonNegativeReals) # amount of nutirent N in food F
model.Nmin = pe.Param(model.N, within=pe.NonNegativeReals, default=0.0) # minimum level of nutrient N
model.Nmax = pe.Param(model.N, within=pe.NonNegativeReals, default=infinity) # maximum level of nutrient N
model.V = pe.Param(model.F, within=pe.PositiveReals) # volume per serving of food F
model.Vmax = pe.Param(within=pe.PositiveReals) # maximum volume of consumed food

# Variables
model.x = pe.Var(model.F,within=pe.NonNegativeIntegers) # number of servings of food i to consume

@model.Constraint(model.N) # limit nutrient consumption for each nutrient
def _nut_min_limit(m,j):
    value = sum(m.a[i,j]*m.x[i] for i in model.F)
    return m.Nmin[j] <= value <= m.Nmax[j]

@model.Constraint() # limit the voluem of food consumed
def _vol_max_limit(m):
    return sum(m.V[i]*m.x[i] for i in model.F) <= m.Vmax

@model.Constraint() # consumption lower bound
def _min_consumption(m):
    return (m.x[i] for i in model.F)  >= 0

@model.Objective()
def _obj(m):
    return sum(m.c[i]*m.x[i] for i in model.F)

data.load(filename='diet.dat')

solver = pe.SolverFactory('glpk')
solver.solve(model)

0 个答案:

没有答案