@nico can you try
using Ipopt
function eval_f(x::Vector{Float64})
return x[1] * x[4] * (x[1] + x[2] + x[3]) + x[3]
end
function eval_g(x::Vector{Float64}, g::Vector{Float64})
g[1] = x[1] * x[2] * x[3] * x[4]
g[2] = x[1]^2 + x[2]^2 + x[3]^2 + x[4]^2
return
end
function eval_grad_f(x::Vector{Float64}, grad_f::Vector{Float64})
grad_f[1] = x[1] * x[4] + x[4] * (x[1] + x[2] + x[3])
grad_f[2] = x[1] * x[4]
grad_f[3] = x[1] * x[4] + 1
grad_f[4] = x[1] * (x[1] + x[2] + x[3])
return
end
function eval_jac_g(
x::Vector{Float64},
rows::Vector{Int32},
cols::Vector{Int32},
values::Union{Nothing,Vector{Float64}},
)
if values === nothing
rows .= [1, 1, 1, 1, 2, 2, 2, 2]
cols .= [1, 2, 3, 4, 1, 2, 3, 4]
else
values[1] = x[2] * x[3] * x[4] # 1,1
values[2] = x[1] * x[3] * x[4] # 1,2
values[3] = x[1] * x[2] * x[4] # 1,3
values[4] = x[1] * x[2] * x[3] # 1,4
values[5] = 2 * x[1] # 2,1
values[6] = 2 * x[2] # 2,2
values[7] = 2 * x[3] # 2,3
values[8] = 2 * x[4] # 2,4
end
return
end
function build_problem()
prob = Ipopt.CreateIpoptProblem(
4, # n
[1.0, 1.0, 1.0, 1.0], # x_L
[5.0, 5.0, 5.0, 5.0], # x_U
2, # m
[25.0, 40.0], # g_L
[Inf, 40.0], # g_U
8,
10,
eval_f,
eval_g,
eval_grad_f,
eval_jac_g,
nothing, # eval_h,
)
Ipopt.AddIpoptStrOption(prob, "hessian_approximation", "limited-memory")
return prob
end
function Ipopt.FreeIpoptProblem(prob::Ipopt.IpoptProblem)
if prob.ipopt_problem === C_NULL
println("Calling FreeIpoptProblem with NULL")
return
end
println("Calling FreeIpoptProblem with valid pointer")
@ccall Ipopt.libipopt.FreeIpoptProblem(prob::Ptr{Cvoid})::Cvoid
prob.ipopt_problem = C_NULL
return
end
p1 = build_problem();
Ipopt.OpenIpoptOutputFile(p1, "ipopt.log", 5)
Ipopt.IpoptSolve(p1)
Ipopt.FreeIpoptProblem(p1)
print(read("ipopt.log", String))
p2 = build_problem();
p2.x = [1.0, 4.75, 3.82, 1.379];
Ipopt.OpenIpoptOutputFile(p2, "ipopt.log", 5)
Ipopt.IpoptSolve(p2)
Ipopt.FreeIpoptProblem(p2)
print(read("ipopt.log", String))