Skip to content

Set output_file via addOption instead of OpenIpoptOutputFile#18

Open
odow wants to merge 1 commit into
byuflowlab:masterfrom
odow:patch-1
Open

Set output_file via addOption instead of OpenIpoptOutputFile#18
odow wants to merge 1 commit into
byuflowlab:masterfrom
odow:patch-1

Conversation

@odow

@odow odow commented Apr 1, 2025

Copy link
Copy Markdown

The file opened by OpenIpoptOutputFile is not released until FreeIpoptProblem is called. This is a problem with Julia's garbage collector, which is not guaranteed to run immediately. Thus, solving a model twice with the same log file can cause an error.

This was raised by https://discourse.julialang.org/t/ipopt-suppress-output-file-generation/127595
and also by jump-dev/Ipopt.jl#470

(I have no idea who two people independently opened the same issue on the same day!)

Comment thread src/ipopt.jl
print_level = options["print_level"]
end
Ipopt.OpenIpoptOutputFile(prob, filename, print_level)
addOption(prob, "output_file", get(options, "output_file", "ipopt.out"))

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The options say https://coin-or.github.io/Ipopt/OPTIONS.html#OPT_output_file

image

But this is not needed for me locally:

julia> isfile("ipopt.out")
false

julia> using SNOW # http://flow.byu.edu/SNOW.jl/index.html

julia> function runOptimization(objFun, objFunGrad, conFun, conFunGrad, ng, x0, lx, ux, lc, uc, gradType)
           function objCon!(g, x)
               f = objFun(x)
               g .= conFun(x)
               return f
           end

           function objConGrad!(g, df, dg, x)
               f = objFun(x)
               df .= objFunGrad(x)
               g .= conFun(x)
               dg .= conFunGrad(x)
               return f
           end

           ip_options = Dict(
               "max_iter" => 1000,
               "tol" => 1e-6,
               "output_file" => "ipopt.out")

           if gradType == "automatic"

               options = Options(solver = IPOPT(ip_options), 
                                   derivatives=ForwardAD())  # choosing IPOPT solver
               xopt, fopt, info = minimize(objCon!, x0, ng, lx, ux, lc, uc, options)

           elseif gradType == "analytic"

               options = Options(solver = IPOPT(ip_options), 
                                   derivatives=UserDeriv())  # choosing IPOPT solver
               xopt, fopt, info = minimize(objConGrad!, x0, ng, lx, ux, lc, uc, options)

           end

           println("xstar = ", round.(xopt, digits=3))
           println("fstar = ", round(fopt, digits=3))
           println("info = ", info)

           return xopt, fopt, info
       end
runOptimization (generic function with 1 method)

julia> f(x) = (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2 # Objective function (Rosenbrock)
f (generic function with 1 method)

julia> ∇f(x) = [-2*(1.0 - x[1]) - 400*(x[2] - x[1]^2)*x[1],  200.0 * (x[2] - x[1]^2)]
∇f (generic function with 1 method)

julia> g1(x) = x[1]^2 + x[2]^2 - 1 # 1st constraint
g1 (generic function with 1 method)

julia> ∇g1(x) = [2*x[1], 2*x[2]]
∇g1 (generic function with 1 method)

julia> g2(x) = x[1] + 3*x[2] - 5 # 2nd constraint
g2 (generic function with 1 method)

julia> ∇g2(x) = [1.0, 3.0]
∇g2 (generic function with 1 method)

julia> g(x) = [g1(x); g2(x)]
g (generic function with 1 method)

julia> ∇g(x) = [∇g1(x)'; ∇g2(x)']
∇g (generic function with 1 method)

julia> ng = 2 # Number of constraints
2

julia> x0 = [1.3, 0.5] # Starting point
2-element Vector{Float64}:
 1.3
 0.5

julia> lx = [-2.0, -1.0] # Lower bound for design variables
2-element Vector{Float64}:
 -2.0
 -1.0

julia> ux = [+2.0, +3.0] # Upper bound for design variables
2-element Vector{Float64}:
 2.0
 3.0

julia> lc = [-Inf; -Inf] # Lower bound for nonlinear constraints
2-element Vector{Float64}:
 -Inf
 -Inf

julia> uc = [0.0; 0.0] # Upper bound for nonlinear constraints
2-element Vector{Float64}:
 0.0
 0.0

julia> gradType = "analytic" # options: automatic, analytic
"analytic"

julia> xopt, fopt, info = runOptimization(f, ∇f, g, ∇g, ng, x0, lx, ux, lc, uc, gradType)
This is Ipopt version 3.14.17, running with linear solver MUMPS 5.7.3.

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

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

iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
   0  1.4170000e+02 9.40e-01 3.97e+01   0.0 0.00e+00    -  0.00e+00 0.00e+00   0
   1  2.0650547e+01 1.70e+00 8.01e+01  -5.8 3.56e+01    -  2.50e-02 6.13e-02f  1
   2  2.0242224e+01 1.70e+00 7.91e+01  -5.9 1.72e+00    -  1.66e-01 1.93e-02h  1
   3  8.1098765e+01 5.11e-01 7.01e+01  -1.5 2.54e+00    -  6.47e-01 1.00e+00h  1
   4  7.0226247e-01 0.00e+00 4.74e+00  -0.9 3.82e-01    -  1.00e+00 1.00e+00f  1
   5  1.0811860e-01 0.00e+00 1.48e+00  -2.4 1.68e-01    -  1.00e+00 1.00e+00h  1
   6  6.1736979e-02 0.00e+00 2.26e-02  -4.1 3.71e-02    -  1.00e+00 1.00e+00h  1
   7  6.0347069e-02 0.00e+00 4.93e-02  -5.3 1.62e-02    -  1.00e+00 1.00e+00h  1
   8  4.5768251e-02 2.58e-03 1.29e-01  -6.5 3.20e-01    -  1.00e+00 5.80e-01h  1
   9  4.5834458e-02 0.00e+00 6.34e-02  -4.7 7.56e-03    -  1.00e+00 1.00e+00h  1
iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
  10  4.5676766e-02 0.00e+00 2.12e-05  -6.7 1.35e-03    -  1.00e+00 1.00e+00h  1
  11  4.5674807e-02 1.19e-10 9.53e-08 -11.0 2.58e-05    -  1.00e+00 9.99e-01h  1

Number of Iterations....: 11

                                   (scaled)                 (unscaled)
Objective...............:   7.3740406022125646e-03    4.5674807490104635e-02
Dual infeasibility......:   9.5306477994326699e-08    5.9032832469685975e-07
Constraint violation....:   1.1878070222849125e-10    1.1878070222849125e-10
Variable bound violation:   0.0000000000000000e+00    0.0000000000000000e+00
Complementarity.........:   1.2430637868727329e-11    7.6995370958897100e-11
Overall NLP error.......:   9.5306477994326699e-08    5.9032832469685975e-07


Number of objective function evaluations             = 12
Number of objective gradient evaluations             = 12
Number of equality constraint evaluations            = 0
Number of inequality constraint evaluations          = 12
Number of equality constraint Jacobian evaluations   = 0
Number of inequality constraint Jacobian evaluations = 12
Number of Lagrangian Hessian evaluations             = 0
Total seconds in IPOPT                               = 0.199

EXIT: Optimal Solution Found.
xstar = [0.786, 0.618]
fstar = 0.046
info = Solve_Succeeded
([0.7864151557284765, 0.6176983187279504], 0.045674807490104635, :Solve_Succeeded)

shell> cat ipopt.out
This is Ipopt version 3.14.17, running with linear solver MUMPS 5.7.3.

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

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

iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
   0  1.4170000e+02 9.40e-01 3.97e+01   0.0 0.00e+00    -  0.00e+00 0.00e+00   0
   1  2.0650547e+01 1.70e+00 8.01e+01  -5.8 3.56e+01    -  2.50e-02 6.13e-02f  1
   2  2.0242224e+01 1.70e+00 7.91e+01  -5.9 1.72e+00    -  1.66e-01 1.93e-02h  1
   3  8.1098765e+01 5.11e-01 7.01e+01  -1.5 2.54e+00    -  6.47e-01 1.00e+00h  1
   4  7.0226247e-01 0.00e+00 4.74e+00  -0.9 3.82e-01    -  1.00e+00 1.00e+00f  1
   5  1.0811860e-01 0.00e+00 1.48e+00  -2.4 1.68e-01    -  1.00e+00 1.00e+00h  1
   6  6.1736979e-02 0.00e+00 2.26e-02  -4.1 3.71e-02    -  1.00e+00 1.00e+00h  1
   7  6.0347069e-02 0.00e+00 4.93e-02  -5.3 1.62e-02    -  1.00e+00 1.00e+00h  1
   8  4.5768251e-02 2.58e-03 1.29e-01  -6.5 3.20e-01    -  1.00e+00 5.80e-01h  1
   9  4.5834458e-02 0.00e+00 6.34e-02  -4.7 7.56e-03    -  1.00e+00 1.00e+00h  1
iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
  10  4.5676766e-02 0.00e+00 2.12e-05  -6.7 1.35e-03    -  1.00e+00 1.00e+00h  1
  11  4.5674807e-02 1.19e-10 9.53e-08 -11.0 2.58e-05    -  1.00e+00 9.99e-01h  1

Number of Iterations....: 11

                                   (scaled)                 (unscaled)
Objective...............:   7.3740406022125646e-03    4.5674807490104635e-02
Dual infeasibility......:   9.5306477994326699e-08    5.9032832469685975e-07
Constraint violation....:   1.1878070222849125e-10    1.1878070222849125e-10
Variable bound violation:   0.0000000000000000e+00    0.0000000000000000e+00
Complementarity.........:   1.2430637868727329e-11    7.6995370958897100e-11
Overall NLP error.......:   9.5306477994326699e-08    5.9032832469685975e-07


Number of objective function evaluations             = 12
Number of objective gradient evaluations             = 12
Number of equality constraint evaluations            = 0
Number of inequality constraint evaluations          = 12
Number of equality constraint Jacobian evaluations   = 0
Number of inequality constraint Jacobian evaluations = 12
Number of Lagrangian Hessian evaluations             = 0
Total seconds in IPOPT                               = 0.199

EXIT: Optimal Solution Found.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant