我試圖創建Pyomo 5.1.1抽象模型實例化一個具體的模型,然後使用Python內的值來填充它(即不使用AMPL文件)。我基本上遵循Pyomo documentation example,但我得到「恆定客觀檢測」。從抽象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
但我找不到描述蟒蛇內初始化的任何文件。