Search code examples
pythonpyomo

Pyomo: When using python script, are there any quick ways to show the Objective value after solving the 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.

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?


Solution

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

    print(value(instance.obj))