Printing Jump NLexpression during solution

i’m having trouble debugging my NL model in Jump. Here is a MWE:

using JuMP, Ipopt

function j_test(a)

    m = Model(Ipopt.Optimizer)
	@variable(m,x >= 1.0)
	@variable(m,y)

	@NLexpression(m, q, x^a + log(x + a) )

    @NLobjective(m, Min, y^2 + q)
    optimize!(m)

	(x = value(x), y = value(y))

end

If called with a positive paramter a, this works:

julia> j_test(10)
This is Ipopt version 3.13.2, running with linear solver mumps.
NOTE: Other linear solvers might be more efficient (see Ipopt documentation).

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

Total number of variables............................:        2
                     variables with only lower bounds:        1
                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

iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
   0  3.5034260e+00 0.00e+00 1.00e+01  -1.0 0.00e+00    -  0.00e+00 0.00e+00   0
   1  3.4891590e+00 0.00e+00 3.81e-01  -1.0 5.20e-03    -  1.00e+00 2.50e-01f  3
   2  3.4194021e+00 0.00e+00 1.63e-02  -1.7 6.59e-03    -  1.00e+00 1.00e+00f  1
   3  3.3984007e+00 0.00e+00 1.54e-03  -3.8 2.06e-03    -  1.00e+00 1.00e+00f  1
   4  3.3980456e+00 0.00e+00 4.44e-07  -3.8 3.52e-05    -  1.00e+00 1.00e+00f  1
   5  3.3978970e+00 0.00e+00 7.81e-08  -5.7 1.47e-05    -  1.00e+00 1.00e+00f  1
   6  3.3978952e+00 0.00e+00 1.22e-11  -8.6 1.85e-07    -  1.00e+00 1.00e+00f  1

Number of Iterations....: 6

                                   (scaled)                 (unscaled)
Objective...............:   3.3978951743982369e+00    3.3978951743982369e+00
Dual infeasibility......:   1.2230216839270724e-11    1.2230216839270724e-11
Constraint violation....:   0.0000000000000000e+00    0.0000000000000000e+00
Complementarity.........:   2.5089529785247992e-09    2.5089529785247992e-09
Overall NLP error.......:   2.5089529785247992e-09    2.5089529785247992e-09


Number of objective function evaluations             = 13
Number of objective gradient evaluations             = 7
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             = 6
Total CPU secs in IPOPT (w/o function evaluations)   =      0.004
Total CPU secs in NLP function evaluations           =      0.000

EXIT: Optimal Solution Found.
(x = 1.0, y = 0.0)

with a = -1 there is a clear problem because at the lower bound of x we have log(0):

julia> j_test(-1.)
This is Ipopt version 3.13.2, running with linear solver mumps.
NOTE: Other linear solvers might be more efficient (see Ipopt documentation).

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

Total number of variables............................:        2
                     variables with only lower bounds:        1
                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
...

ERROR: Problem in step computation, but emergency mode cannot be activated.

Number of Iterations....: 55

                                   (scaled)                 (unscaled)
Objective...............:  -3.1952610935758843e-07   -3.1952610935758841e+01
Dual infeasibility......:   2.0470907397013076e+06    2.0470907397013075e+14
Constraint violation....:   0.0000000000000000e+00    0.0000000000000000e+00
Complementarity.........:   1.2544324387886203e-13    1.2544324387886202e-05
Overall NLP error.......:   2.0470907397013076e+06    2.0470907397013075e+14


Number of objective function evaluations             = 81
Number of objective gradient evaluations             = 56
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             = 56
Total CPU secs in IPOPT (w/o function evaluations)   =      0.041
Total CPU secs in NLP function evaluations           =      0.001

EXIT: Error in step computation (regularization becomes too large?)!
(x = 1.0000000000000049, y = 0.0)

and with a<1.0 one gets an immediate error

julia> j_test(-1.5)
This is Ipopt version 3.13.2, running with linear solver mumps.
NOTE: Other linear solvers might be more efficient (see Ipopt documentation).

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

Total number of variables............................:        2
                     variables with only lower bounds:        1
                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


Number of Iterations....: 0

Number of objective function evaluations             = 1
Number of objective gradient evaluations             = 1
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             = 0
Total CPU secs in IPOPT (w/o function evaluations)   =      0.000
Total CPU secs in NLP function evaluations           =      0.000

EXIT: Invalid number in NLP function or derivative detected.
(x = 1.0099999899999998, y = 0.0)

I have a large model with many @NLexpressions. is there a way I can somehow check their value along the way after I call optimize?

1 Like

You can use value(q), which will evaluate the expression at the current primal point (i.e., value(x)).

There is no easy way to show the values of the expressions during the solve. It requires a callback which isn’t accessible from JuMP at present.