Store the solver output in a python variable

171 views
Skip to first unread message

XG

unread,
Aug 21, 2017, 5:17:18 PM8/21/17
to Pyomo Forum
Sometimes the solver stops with a termination condition that is not known to pyomo, where the termination condition returned by pyomo is 'other' or 'unknown'. In such cases it is useful to use tee=True to look at the actual solver output and find out what the problem is. My question is that whether it is possible to have pyomo does not repress the solver output but store it in a python variable instead of printing it in the output, e.g., something like this:

pymoSolver = SolverFactory('gurobi')
optSoln = pyomoSolver.solve(myOptModel, tee = True)  # and somehow store the solver output into a variable

If str(optSoln.solver.termination_condition).lower() in ['other', 'unknown']:
       # Print out the actual solver output stored in a variable
       print 'The actual solver output is as follows: ',variable_storing_solver_output
else:
      # Do other stuff and does not show the solver output


The reason is that I normally don't like to see the actual solver output, unless the termination condition is 'other' or 'unknown'. Thanks! 

XG

unread,
Aug 23, 2017, 11:08:10 AM8/23/17
to Pyomo Forum
So, I assume there is no way to achieve this? If so, can the solver output be stored in a file instead but use tee = False?

David Woodruff

unread,
Aug 24, 2017, 1:59:40 PM8/24/17
to pyomo...@googlegroups.com
Here is a rough sketch of the most direct way to do what you want (I think):

import pyutilib.misc.redirect_io
logfile = "foobar.log"
with open(logfile, 'w') as output:
    pyutilib.misc.redirect_io.setup_redirect(output)


    pymoSolver = SolverFactory('gurobi')
    optSoln = pyomoSolver.solve(myOptModel, tee = True)

If str(optSoln.solver.termination_condition).lower() in ['other', 'unknown']:
    # do something with the log file
else:
    # Do other stuff and does not show the solver output
 pyutilib.misc.redirect_io.reset_redirect()

Notes: I did not test this. Also note that Gurobi has an option to write a log file, so you might want to go that route.
Dave

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages