我正在尝试在 Pyomo 5.1.1 中创建一个抽象模型,然后用 python 中的值填充它(即不使用 AMPL 文件)。我基本上遵循Pyomo 文档示例,但得到“检测到恒定目标”。
import pyomo.environ as oe
model = oe.AbstractModel()
model.I = oe.Set()
model.J = oe.Set()
model.a = oe.Param(model.I,model.J)
model.b = oe.Param(model.I)
model.c = oe.Param(model.J)
model.x = oe.Var(model.J,domain=oe.NonNegativeReals)
def obj_expression(model):
return oe.summation(model.c,model.x)
model.OBJ = oe.Objective(rule=obj_expression)
def ax_constraint_rule(model,i):
return sum(model.a[i,j]*model.x[j] for j in model.J) >= model.b[i]
model.AxbConstraint = oe.Constraint(model.I,rule=ax_constraint_rule)
然后,我尝试用实际值初始化这个模型
aa = np.array([[1,2,1,4],[5,2,2,4]])
bb = np.array([2,4])
cc = np.array([1,2,4,2])
cmodel = model.create_instance()
cmodel.a.values = aa
cmodel.b.values = bb
cmodel.c.values = cc
opt = oe.SolverFactory("glpk")
results = opt.solve(cmodel)
我收到以下错误:
WARNING:pyomo.core:Constant objective detected, replacing with a placeholder to prevent solver failure.
WARNING:pyomo.core:Empty constraint block written in LP format - solver may error
WARNING: Constant objective detected, replacing with a placeholder to prevent solver failure.
WARNING: Empty constraint block written in LP format - solver may error
显然我的初始化方式有问题,cmodel
但我找不到任何描述python中初始化的文档。