Pyomo:当使用 python 脚本时,在解决 ILP 后有没有快速显示 Objective 值的方法?

Pyomo: When using python script, are there any quick ways to show the Objective value after solving the ILP?

我之前完成了一个 ILP,它运行正常。

opt = SolverFactory('glpk')
model = AbstractModel()
model.obj = Objective(...)

# variables, constraints ...

instance = model.create_instance()
results = opt.solve(instance)

因为我想获取每个变量的值而且还想解决 objective 函数,所以我尝试通过与我对变量所做的类似的方式访问 Objective 函数,但所有我能得到的是一个表达式。

我使用以下代码:

print(instance.obj.value)

但只得到这样的警告:

WARNING: DEPRECATED: The .value property getter on SimpleObjective is deprecated. Use the .expr property getter instead

当我把代码改成

print(instance.obj.expr)

我得到的只是一个表情。所以我想知道除了获取所有需要的变量并自己重新计算之外,还有什么方法可以获取 objective 函数的值吗?

必须显式调用表达式 属性 getter。

obj_val = instance.obj.expr()
print(obj_val)

获取objective函数值的最佳方式是使用Pyomo

提供的value函数
print(value(instance.obj))