Pyomo:在使用python脚本的时候,有没有什么快速的方法可以在解决ILP后显示Objective值? [英] Pyomo: When using python script, are there any quick ways to show the Objective value after solving the ILP?

查看:51
本文介绍了Pyomo:在使用python脚本的时候,有没有什么快速的方法可以在解决ILP后显示Objective值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前完成了一个 ILP,它工作正常.

I finished an ILP before and it works properly.

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

# variables, constraints ...

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

因为我想获得每个变量的值,而且还想得到目标函数,所以我尝试通过与我对变量所做的类似的方式访问目标函数,但我只能得到一个表达式.

since I want to get the value of each variable but also the objective function solved, I try to access the Objective function by the way similar as what I did to the variable but all I can get is an expression.

我使用以下代码:

print(instance.obj.value)

但只得到这样的警告:

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

当我将代码更改为

print(instance.obj.expr)

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

All I get is an expression. So I wanna know is there any way to get the value of the objective function other than getting all the variables needed and re-calculating by myself again?

推荐答案

获取目标函数值的最好方法是使用Pyomo提供的value函数

The best way to get the value of the objective function is to use the value function provided by Pyomo

print(value(instance.obj))

这篇关于Pyomo:在使用python脚本的时候,有没有什么快速的方法可以在解决ILP后显示Objective值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆