How to get the optimal value of objective function

3,876 views
Skip to first unread message

auz107

unread,
Nov 18, 2013, 6:04:55 PM11/18/13
to coopr...@googlegroups.com
I am solving a simple LP in pyomo (using cplex) and need to write the optimal value of the objective function into a file. While I can get the optimal values of variables, I couldn't find anyway of doing the same thing for objective function value. Any help is greatly appreciated.

Watson, Jean-paul

unread,
Nov 19, 2013, 1:05:55 PM11/19/13
to coopr...@googlegroups.com
Assuming you are scripting (it sounds like you are), and have loaded the results into your Pyomo model…

You just need to say something like: obj_value = my_model.my_objective()

The () operator computes the value of the objective on-the-fly, using current values for the variables.

jpw

--
You received this message because you are subscribed to the Google Groups "coopr forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to coopr-forum...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

auz107

unread,
Nov 19, 2013, 2:11:48 PM11/19/13
to coopr...@googlegroups.com
Thanks. This is something that is missing in the manual and is not easy to find. I was able to find another way of getting the value of objective function by looking at details of results.Solution:

results = opt.solve(instance)
obj_value = results.Solution.Objective.__default_objective__['value']

but obviously the way you did is much easier.

JolleG

unread,
Mar 4, 2015, 8:39:12 AM3/4/15
to pyomo...@googlegroups.com, coopr...@googlegroups.com, alirzo...@gmail.com
Hi,

I agree that this is still not well documented. I tried to print the value of the objective function as

print instance.OBJ,

when my objective was declared as model.OBJ. But this just printed: "OBJ". Furthermore,

print instance.OBJ.value

gave me: 'None'. However, with the help of auz107 I tried

print instance.OBJ()

which gave me the desired result.
Reply all
Reply to author
Forward
0 new messages