Search code examples
pyomoipopt

Getting Values from IPOPT Display Pyomo


Here is my code for a Concrete Model of the Rosenbrock.

from pyomo.environ import *
from pyomo.opt import SolverFactory
import numpy as np
import math
import statistics
import time

m = ConcreteModel()

m.x = Var()
m.y = Var()
m.z = Var()

def rosenbrock(model):
    return (1.0-m.x)2 + 100.0*(m.y - m.x2)2 + (1.0-m.y)2 + 100.0*(m.z - m.y2)2

m.obj = Objective(rule=rosenbrock, sense=minimize)

dist = 0.0
xval = yval = zval = error = times = []
for i in range(50):
    m.x = np.random.uniform(low=-5.0, high=5.0)
    m.y = np.random.uniform(low=-5.0, high=5.0)
    m.z = np.random.uniform(low=-5.0, high=5.0)
    solver = SolverFactory('ipopt')
    t1 = time.time()
    results = solver.solve(m, tee=True)

The solver.solve line when passed the tee=True prints out this beautiful display of all sorts of nice information. I want to access that information from the prinout and have scoured Pyomo and IPOPT documentation and cannot seem to understand how to access the values that are printed to the screen. I've also included a short example of the printout, I want to save the values from each run so that I can iterate and gather statistics over the total range.

Number of nonzeros in equality constraint Jacobian...:        0
Number of nonzeros in inequality constraint Jacobian.:        0
Number of nonzeros in Lagrangian Hessian.............:        5

Total number of variables............................:        3
                     variables with only lower bounds:        0
                variables with lower and upper bounds:        0
                     variables with only upper bounds:        0
Total number of equality constraints.................:        0
Total number of inequality constraints...............:        0
        inequality constraints with only lower bounds:        0
   inequality constraints with lower and upper bounds:        0
        inequality constraints with only upper bounds:        0

****OMITTED****

Number of objective function evaluations             = 45
Number of objective gradient evaluations             = 23
Number of equality constraint evaluations            = 0
Number of inequality constraint evaluations          = 0
Number of equality constraint Jacobian evaluations   = 0
Number of inequality constraint Jacobian evaluations = 0
Number of Lagrangian Hessian evaluations             = 22
Total CPU secs in IPOPT (w/o function evaluations)   =      0.020
Total CPU secs in NLP function evaluations           =      0.000

I need some of these values but I see no feasible interface to access them from my search of the documentation, any wizards know how to do this? Thanks.


Solution

  • See this Ipopt solver wrapper that was contributed to Pyomo. It's essentially a parser for the Ipopt output log and you should be able to generalize/expand it to collect any values that aren't currently collected.

    https://github.com/Pyomo/pyomo/blob/master/pyomo/contrib/parmest/ipopt_solver_wrapper.py