Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamed82008 committed Nov 15, 2023
1 parent 0ddf464 commit 171bc10
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 28 deletions.
38 changes: 25 additions & 13 deletions src/juniper.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@params struct JuniperIpoptOptions
nt::NamedTuple
subsolver_options
subsolver_options::Any
first_order::Bool
end
function JuniperIpoptOptions(;
Expand All @@ -12,7 +12,8 @@ function JuniperIpoptOptions(;
),
kwargs...,
)
first_order = !hasproperty(subsolver_options.nt, :hessian_approximation) ||
first_order =
!hasproperty(subsolver_options.nt, :hessian_approximation) ||
subsolver_options.nt.hessian_approximation == "limited-memory"
return JuniperIpoptOptions((; kwargs...), subsolver_options, first_order)
end
Expand All @@ -26,8 +27,10 @@ end
counter::Base.RefValue{Int}
end
function JuniperIpoptWorkspace(
model::VecModel, x0::AbstractVector = getinit(model);
options = JuniperIpoptOptions(), kwargs...,
model::VecModel,
x0::AbstractVector = getinit(model);
options = JuniperIpoptOptions(),
kwargs...,
)
integers = model.integer
@assert length(integers) == length(x0)
Expand All @@ -42,19 +45,24 @@ function JuniperIpoptWorkspace(
string(k) => nt2[k]
end
optimizer = JuMP.optimizer_with_attributes(
Juniper.Optimizer, "nl_solver" => nl_solver, Dict(solver_options)...,
Juniper.Optimizer,
"nl_solver" => nl_solver,
Dict(solver_options)...,
)
problem, counter = get_jump_problem(
model, copy(x0); first_order = options.first_order,
optimizer = optimizer, integers = integers,
model,
copy(x0);
first_order = options.first_order,
optimizer = optimizer,
integers = integers,
)
return JuniperIpoptWorkspace(model, problem, copy(x0), integers, options, counter)
end
@params struct JuniperIpoptResult <: AbstractResult
minimizer
minimum
problem
status
minimizer::Any
minimum::Any
problem::Any
status::Any
fcalls::Int
end

Expand All @@ -71,12 +79,16 @@ function optimize!(workspace::JuniperIpoptWorkspace)
term_status = MOI.get(moi_model, MOI.TerminationStatus())
primal_status = MOI.get(moi_model, MOI.PrimalStatus())
return JuniperIpoptResult(
minimizer, objval, problem, (term_status, primal_status), counter[],
minimizer,
objval,
problem,
(term_status, primal_status),
counter[],
)
end

struct JuniperIpoptAlg <: AbstractOptimizer end

function Workspace(model::VecModel, optimizer::JuniperIpoptAlg, args...; kwargs...,)
function Workspace(model::VecModel, optimizer::JuniperIpoptAlg, args...; kwargs...)
return JuniperIpoptWorkspace(model, args...; kwargs...)
end
30 changes: 15 additions & 15 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using NonconvexJuniper, LinearAlgebra, Test

f(x::AbstractVector) = x[2] < 0 ? Inf : sqrt(x[2])
g(x::AbstractVector, a, b) = (a*x[1] + b)^3 - x[2]
g(x::AbstractVector, a, b) = (a * x[1] + b)^3 - x[2]

@testset "First order - $first_order" for first_order in [true, false]
options = JuniperIpoptOptions(first_order = first_order)
Expand All @@ -14,8 +14,8 @@ g(x::AbstractVector, a, b) = (a*x[1] + b)^3 - x[2]
alg = JuniperIpoptAlg()
r1 = NonconvexJuniper.optimize(m, alg, [1.234, 2.345], options = options)
global s1 = sum(r1.minimizer)
@test abs(r1.minimum - sqrt(8/27)) < 1e-6
@test norm(r1.minimizer - [1/3, 8/27]) < 1e-6
@test abs(r1.minimum - sqrt(8 / 27)) < 1e-6
@test norm(r1.minimizer - [1 / 3, 8 / 27]) < 1e-6

setinteger!(m, 1, true)
r2 = NonconvexJuniper.optimize(m, alg, [1.234, 2.345], options = options)
Expand All @@ -37,8 +37,8 @@ g(x::AbstractVector, a, b) = (a*x[1] + b)^3 - x[2]
add_eq_constraint!(m, x -> sum(x) - s1)
alg = JuniperIpoptAlg()
r1 = NonconvexJuniper.optimize(m, alg, [1.234, 2.345], options = options)
@test abs(r1.minimum - sqrt(8/27)) < 1e-6
@test norm(r1.minimizer - [1/3, 8/27]) < 1e-6
@test abs(r1.minimum - sqrt(8 / 27)) < 1e-6
@test norm(r1.minimizer - [1 / 3, 8 / 27]) < 1e-6

m = Model(f)
addvar!(m, [0.0, 0.0], [10.0, 10.0])
Expand Down Expand Up @@ -66,8 +66,8 @@ g(x::AbstractVector, a, b) = (a*x[1] + b)^3 - x[2]

alg = JuniperIpoptAlg()
r1 = NonconvexJuniper.optimize(m, alg, [1.234, 2.345], options = options)
@test abs(r1.minimum - sqrt(8/27)) < 1e-6
@test norm(r1.minimizer - [1/3, 8/27]) < 1e-6
@test abs(r1.minimum - sqrt(8 / 27)) < 1e-6
@test norm(r1.minimizer - [1 / 3, 8 / 27]) < 1e-6

setinteger!(m, 1, true)
r2 = NonconvexJuniper.optimize(m, alg, [1.234, 2.345], options = options)
Expand All @@ -88,13 +88,13 @@ g(x::AbstractVector, a, b) = (a*x[1] + b)^3 - x[2]

alg = JuniperIpoptAlg()
r1 = NonconvexJuniper.optimize(m, alg, [1.234, 2.345], options = options)
@test abs(r1.minimum - sqrt(8/27)) < 1e-6
@test norm(r1.minimizer - [1/3, 8/27]) < 1e-6
@test abs(r1.minimum - sqrt(8 / 27)) < 1e-6
@test norm(r1.minimizer - [1 / 3, 8 / 27]) < 1e-6

setinteger!(m, 1, true)
r2 = NonconvexJuniper.optimize(m, alg, [1.234, 2.345], options = options)
@test r2.minimizer[1] - round(Int, r2.minimizer[1]) 0 atol = 1e-7

setinteger!(m, 1, false)
setinteger!(m, 2, true)
r3 = NonconvexJuniper.optimize(m, alg, [1.234, 2.345], options = options)
Expand All @@ -108,8 +108,8 @@ g(x::AbstractVector, a, b) = (a*x[1] + b)^3 - x[2]

alg = JuniperIpoptAlg()
r = NonconvexJuniper.optimize(m, alg, [1.234, 2.345], options = options)
@test abs(r.minimum - sqrt(8/27)) < 1e-6
@test norm(r.minimizer - [1/3, 8/27]) < 1e-6
@test abs(r.minimum - sqrt(8 / 27)) < 1e-6
@test norm(r.minimizer - [1 / 3, 8 / 27]) < 1e-6
end
@testset "Infinite upper and lower bound" begin
m = Model(f)
Expand All @@ -119,8 +119,8 @@ g(x::AbstractVector, a, b) = (a*x[1] + b)^3 - x[2]

alg = JuniperIpoptAlg()
r = NonconvexJuniper.optimize(m, alg, [1.234, 2.345], options = options)
@test abs(r.minimum - sqrt(8/27)) < 1e-6
@test norm(r.minimizer - [1/3, 8/27]) < 1e-6
@test abs(r.minimum - sqrt(8 / 27)) < 1e-6
@test norm(r.minimizer - [1 / 3, 8 / 27]) < 1e-6
end
end
end

0 comments on commit 171bc10

Please sign in to comment.