Search code examples
juliajulia-jumpipopt

Access the complementarity infeasibility, dual infeasibility, and the primal infeasibility


how can I access the complementarity infeasibility, the dual infeasibility, and primal infeasibility after optimizing with Ipopt? Do I need to use a callback function for this or does IPOPT have a function for this?


Solution

  • Your other answer is correct; you can access the primal and dual infeasibility using a solver-specific callback:

    https://github.com/jump-dev/Ipopt.jl#solver-specific-callback

    There is no support for accessing the complementarity infeasibility.

    Ipopt.jl v1.2.0 adds support for this:

    julia> using JuMP
    
    julia> import Ipopt
    
    julia> model = Model(Ipopt.Optimizer)
    A JuMP Model
    Feasibility problem with:
    Variables: 0
    Model mode: AUTOMATIC
    CachingOptimizer state: EMPTY_OPTIMIZER
    Solver name: Ipopt
    
    julia> @variable(model, 0 <= x <= π)
    x
    
    julia> @NLobjective(model, Min, (x - cos(x))^2)
    
    julia> function my_callback(
               alg_mod::Cint,
               iter_count::Cint,
               obj_value::Float64,
               inf_pr::Float64,
               inf_du::Float64,
               mu::Float64,
               d_norm::Float64,
               regularization_size::Float64,
               alpha_du::Float64,
               alpha_pr::Float64,
               ls_trials::Cint,
            )
               m, n = 0, 1
               x, z_L, z_U = zeros(n), zeros(n), zeros(n)
               g, lambda = zeros(m), zeros(m)
               scaled = false
               prob = unsafe_backend(model).inner
               Ipopt.GetIpoptCurrentIterate(prob, scaled, n, x, z_L, z_U, m, g, lambda)
               x_L_violation, x_U_violation = zeros(n), zeros(n)
               compl_x_L, compl_x_U, grad_lag_x = zeros(n), zeros(n), zeros(n)
               nlp_constraint_violation, compl_g = zeros(m), zeros(m)
               Ipopt.GetIpoptCurrentViolations(
                   prob,
                   scaled,
                   n,
                   x_L_violation,
                   x_U_violation,
                   compl_x_L,
                   compl_x_U,
                   grad_lag_x,
                   m,
                   nlp_constraint_violation,
                   compl_g,
               )
               @show x
               @show z_L
               @show z_U
               @show x_L_violation
               @show x_U_violation
               @show compl_x_L
               @show compl_x_U
               @show grad_lag_x
               return false
           end
    my_callback (generic function with 1 method)
    
    julia> MOI.set(model, Ipopt.CallbackFunction(), my_callback)
    
    julia> optimize!(model)
    This is Ipopt version 3.14.4, running with linear solver MUMPS 5.4.1.
    
    Number of nonzeros in equality constraint Jacobian...:        0
    Number of nonzeros in inequality constraint Jacobian.:        0
    Number of nonzeros in Lagrangian Hessian.............:        1
    
    Total number of variables............................:        1
                         variables with only lower bounds:        0
                    variables with lower and upper bounds:        1
                         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
    
    x = [0.00999999]
    z_L = [1.0]
    z_U = [1.0]
    x_L_violation = [0.0]
    x_U_violation = [0.0]
    compl_x_L = [0.01]
    compl_x_U = [3.13159269500572]
    grad_lag_x = [-1.999698671463963]
    iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
       0  9.8000102e-01 0.00e+00 2.00e+00  -1.0 0.00e+00    -  0.00e+00 0.00e+00   0
    
    Number of Iterations....: 0
    
                                       (scaled)                 (unscaled)
    Objective...............:   9.8000102332194250e-01    9.8000102332194250e-01
    Dual infeasibility......:   1.9996986714639631e+00    1.9996986714639631e+00
    Constraint violation....:   0.0000000000000000e+00    0.0000000000000000e+00
    Variable bound violation:   0.0000000000000000e+00    0.0000000000000000e+00
    Complementarity.........:   3.1315926950057200e+00    3.1315926950057200e+00
    Overall NLP error.......:   3.1315926950057200e+00    3.1315926950057200e+00
    
    
    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 seconds in IPOPT                               = 0.064
    
    EXIT: Stopping optimization at current point as requested by user.
    

    See the Ipopt documentation for details: