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

寵の児 提交于 2020-01-04 07:09:58

问题


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.

I use the following code:

print(instance.obj.value)

But only got the warning like this:

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

When I change the code to

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?


回答1:


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

print(value(instance.obj))



回答2:


The expression property getter has to be called explicitly.

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


来源:https://stackoverflow.com/questions/48928108/pyomo-when-using-python-script-are-there-any-quick-ways-to-show-the-objective

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!