Results object does not contain solution values

2,759 views
Skip to first unread message

Prem Narasimhan

unread,
Jan 13, 2016, 6:21:29 PM1/13/16
to Pyomo Forum
Hi,
I tried running the sample diet problem using the diet.dat file. Instead of running it thru a command line argument, I wanted to run it within the Python shell. So I wrote this program:

==============================================
import pyomo
from pyomo.environ import *
from pyomo.opt import SolverFactory

# Create a solver
opt2 = SolverFactory("glpk")

infinity = float('inf')

# get the model from another file
from diet import model


infinity = float('inf')

# Create a model instance and optimize
instance = model.create_instance("c:\python27\diet.dat")


results=opt2.solve(instance, tee=True)
print results
===============================================
The output is:

Problem: 
- Name: unknown
  Lower bound: 15.05
  Upper bound: 15.05
  Number of objectives: 1
  Number of constraints: 10
  Number of variables: 10
  Number of nonzeros: 77
  Sense: minimize
Solver: 
- Status: ok
  Termination condition: optimal
  Statistics: 
    Branch and bound: 
      Number of bounded subproblems: 23
      Number of created subproblems: 23
  Error rc: 0
  Time: 0.095999956131
Solution: 
- number of solutions: 0
  number of solutions displayed: 0
========================================================
The optimization seems to have run successfully, and the results object shows that the Solver status is OK, and the termination condition is optimal, and yet it doesn't print the solution (it says "number of solutions=0")

So has the optimization been successful or not? If successful, how can I get the opt2.solve() to print the results? Also, I tried the tee=True option. The display says it printed to some file and claim to have written 22 solution records to the file, but I cannot locate the file in the computer! 

Thanks in advance!!

Prem


David Woodruff

unread,
Jan 13, 2016, 7:46:23 PM1/13/16
to pyomo...@googlegroups.com
A quick thing is to add is:
instance.display()


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

Prem Narasimhan

unread,
Jan 14, 2016, 8:12:34 AM1/14/16
to Pyomo Forum
The instance.display() also does not show the solution values. The url does not help me understand why the solution values are unavailable. Please help!!

Again, the main issue I am facing is this:
I want to stay in the Python environment to solve the LP and parse the results rather than to formulate the problem, run the LP from the Windows command prompt, then parsing the resultant json results file. I find that the latter approach gets the results object populated with solution values, but the former does not. Please help!

Thanks,
Prem

Gabriel Hackebeil

unread,
Jan 14, 2016, 9:17:43 AM1/14/16
to pyomo...@googlegroups.com
In the latest version of Pyomo the solution gets loaded into the instance by default, so instance.display() does actually show the solution values, just not in the form you are used to seeing. I think the following can be used to re-populate the results object with the solution using human-readable names (like what the pyomo command does):

results = opt.solve(instance)
instance.solutions.store_to(results)
print(results)

Gabe

Prem Narasimhan

unread,
Jan 14, 2016, 12:11:57 PM1/14/16
to Pyomo Forum
Thanks much Gabe, that worked like a charm!


On Wednesday, January 13, 2016 at 6:21:29 PM UTC-5, Prem Narasimhan wrote:

oli...@member.ams.org

unread,
Jan 25, 2016, 10:23:11 AM1/25/16
to Pyomo Forum
Thanks a lot, I found this post when googling for exactly the same question.

I came across this problem when evaluating whether I can use pyomo as a computing environment for a class in Operations Research which I am starting to teach (next week!).   I have not used pyomo before, but I am an experienced user of Numpy/Scipy user.  Part of my goal is to use Python as the base environment for applied/scientific computing across a variety of different courses, and it seems that pyomo could be very useful.

While I like a lot of what I see, this problem leaves me slightly worried and I hope that someone could answer the following detailed version of the question:

1. This issue runs counter to all print/web resources I could find (except for this post).  How stable is the Pyomo API?  Is it likely that I end up in a complete API mess half-way throughout the class?

2. Are these changes documented somewhere?  Is there a reasonably complete documentation of the basic features of the API?  (I don't need anything fancy, but the basic call patterns should not be subject to trial and error...)

3. What is the rational for "instance.solutions.store_to(results)"?  The line seems to suggest that "instance" is modified by "result" so that I should look at "instance" later, but it is exactly the other way round.  And why does "result" not know what the result is after having called the solver (I looked and it's there in the auxiliary files!)?  I am trying to understand the mental model of Pyomo.

4. I also have difficulties unpacking the results for further processing in python.  If I write

In [19]: results['Solution']
Out[19]: <pyomo.opt.results.solution.SolutionSet at 0x7f033bf10710>
 
and the docstring only gives me the string representation of the solution, which is not what I want. Is there any documentation on SolutionSet? I could not find it.  In fact, to me the big advantage of using Python/Pyomo in the first place is that I can seamlessly integrate pre- and postprocessing code.

I am not afraid of filling in some gaps in the documentation if necessary, but here I stumble around with a lack of even basic understanding of how the API is designed to work, which is a bit of a shame as I think there is a lot of potential.

Any hints and advice appreciated,
Marcel


Siirola, John D

unread,
Jan 25, 2016, 11:10:53 AM1/25/16
to pyomo...@googlegroups.com

1)      In general, the publically-documented API is very stable.  Our general policy is that things that are documented in The Book (APIs & examples) will work through the major release that the book covers.  The first edition applies to the Coopr 3.x series.  The second edition (we are finishing it now) will cover the yet-to-be released 5.x series.  There are only a few backwards incompatibilities between 4.x and 3.x.  The key ones are documented here: http://www.pyomo.org/blog/2015/12/26/updating-to-pyomo-41  Note that public APIs that are not documented in the book (e.g., DAE, GDP, etc) are more flexible and may change – although we try to maintain backwards compatibility where we can.  Finally, anything beginning with an underscore (_) should be considered private, and we are at liberty to change or eliminate it with no warning.

2)      (see above).  We don’t have a formal document describing the API, although we are planning to include one covering key public APIs in the Second edition of the book.

3)      This was a performance issue: generating the results object is rather expensive for large models.  Historically, Pyomo was separated from the more generic solver interfaces in coopr.opt.  As part of the move from Coopr to Pyomo, we have started to remove that separation.  In almost every case, the user would (should) just load the results back into the instance and query the results in the context of the instance.  This is because, for example, the results object may not have all the variables or information you want (e.g., if the variable was not used in any constraint, it will not be sent to the solver).  SO, we changed things so that – by default – the results from a solve() are directly put into the instance.  If you still need a results object (and I would argue that most people do not) you can create it by “storing the results from an instance into the results object”.  The instance is not modified by store_to (literally, we are storing the results to the results object).  Our primary use for the store_to method and the SolutionSet portion of the results object is for certain scripting cases where we need a convenient mechanism for transmitting solver results in a distributed environment.

4)      In general the solution portion of results objects are meant to be opaque and really shouldn’t be interrogated by users. Bill may want to say more here.

 

john

oli...@member.ams.org

unread,
Jan 26, 2016, 6:36:10 AM1/26/16
to Pyomo Forum
Siirola, John D writes:
[...]

 > SO, we changed things so that – by default – the results from a
 > solve() are directly put into the instance.  If you still need a
 > results object (and I would argue that most people do not) you can
 > create it by “storing the results from an instance into the results
 > object”.

Thanks for your detailed answer.

Yes, I can extract the solution from the "instance" object just beautifully within Python.  I guess this should be the preferred answer to the original poster's question.

Best regards,
Marcel

Norman

unread,
Feb 15, 2016, 12:49:34 PM2/15/16
to Pyomo Forum
Hey,
I also would like to use Pyomo as a linking tool to external solvers from a script i am writing and i would like to add some pre- and post- processing,
to use the Advantages of Python(NumPy, SciPy, etc.) for this kind of programming.
(Like Marcel mentioned in his post from 25th January - Point 4) )

Is there any documented way to read the results for further processing within a script?
@Marcel: Can you tell how you are reading the results, because i couldn't figure it out by myself?

Thanks and best regards,
Norman

oli...@member.ams.org

unread,
Feb 15, 2016, 1:22:07 PM2/15/16
to Pyomo Forum
I actually started using Pyomo in-class (learning as I teach) and have
written up some (at this point extremely basic) examples: 

http://math.jacobs-university.de/oliver/teaching/jacobs/spring2016/ilme202/files/

I will keep adding more as the class moves along.

Pyomo keeps variables and parameters as Python dictionaries. Once you have a
dictionary, it's trivial to convert to Numpy arrays via dict.values()
and dict.keys() methods.

With regard to efficiency, this should be fine as the expensive bits
are in the solver anyway, so going to Numpy is just for convenience of array
manipulation.

I am planning to add some examples generating output via matplotlib as
well, but that's still to be done...

Hope this helps,
Marcel

Norman

unread,
Feb 16, 2016, 8:31:18 AM2/16/16
to Pyomo Forum
Hey
thanks for your quick response.
Your answer and your examples are helping a lot.
Thank you very much,
Norman
Reply all
Reply to author
Forward
0 new messages