Skip to content

Error with Ipopt.jl and ipopt.out file #470

@pollinico

Description

@pollinico

Hi,
I have a simple test code, that used to work and now gives an error.
When I run the code the first time, Ipopt manages to solve the toy problem and print the output to a .txt file.
The second time I run the code, without killing the Julia REPL/terminal, the output file is still held by some process and the code cannot run because it can not access the output file.

The error:

ERROR: IPOPT: Couldn't open output file.
Stacktrace:
 [1] error(s::String)
   @ Base .\error.jl:35
 [2] OpenIpoptOutputFile
   @ C:\Users\nicolo\.julia\packages\Ipopt\Tn8LH\src\C_wrapper.jl:355 [inlined]
 [3] optimize(solver::IPOPT{…}, cache::SNOW.DenseCache{…}, x0::Vector{…}, lx::Vector{…}, ux::Vector{…}, lg::Vector{…}, ug::Vector{…}, rows::Vector{…}, cols::Vector{…})
   @ SNOW C:\Users\nicolo\.julia\packages\SNOW\E3ZCP\src\ipopt.jl:118
 [4] minimize(func!::Function, x0::Vector{…}, ng::Int64, lx::Vector{…}, ux::Vector{…}, lg::Vector{…}, ug::Vector{…}, options::Options{…})
   @ SNOW C:\Users\nicolo\.julia\packages\SNOW\E3ZCP\src\interface.jl:73
 [5] runOptimization(objFun::typeof(f), objFunGrad::typeof(∇f), conFun::typeof(g), conFunGrad::typeof(∇g), ng::Int64, x0::Vector{…}, lx::Vector{…}, ux::Vector{…}, lc::Vector{…}, uc::Vector{…}, gradType::String)
   @ Main c:\Users\nicolo\Dropbox\Work\Code\Julia\Tests\Test_SNOW_IPOPT\Use_SNOWjl.jl:33
 [6] top-level scope
   @ c:\Users\nicolo\Dropbox\Work\Code\Julia\Tests\Test_SNOW_IPOPT\Use_SNOWjl.jl:64
Some type information was truncated. Use `show(err)` to see complete types.'

Julia version: 1.11.4
Ipopt.jl v1.8.0
Code giving the issue:

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

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

f(x) = (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2 # Objective function (Rosenbrock)
∇f(x) = [-2*(1.0 - x[1]) - 400*(x[2] - x[1]^2)*x[1],  200.0 * (x[2] - x[1]^2)]

g1(x) = x[1]^2 + x[2]^2 - 1 # 1st constraint
∇g1(x) = [2*x[1], 2*x[2]]

g2(x) = x[1] + 3*x[2] - 5 # 2nd constraint
∇g2(x) = [1.0, 3.0]

g(x) = [g1(x); g2(x)]
∇g(x) = [∇g1(x)'; ∇g2(x)']

ng = 2 # Number of constraints
x0 = [1.3, 0.5] # Starting point
lx = [-2.0, -1.0] # Lower bound for design variables
ux = [+2.0, +3.0] # Upper bound for design variables
lc = [-Inf; -Inf] # Lower bound for nonlinear constraints
uc = [0.0; 0.0] # Upper bound for nonlinear constraints
gradType = "analytic" # options: automatic, analytic

xopt, fopt, info = runOptimization(f, ∇f, g, ∇g, ng, x0, lx, ux, lc, uc, gradType)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions