Skip to content

Commit

Permalink
Improve test coverage (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Aug 20, 2024
1 parent 6fd1870 commit 8a486d2
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 10 deletions.
10 changes: 1 addition & 9 deletions src/NLopt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ mutable struct Opt
exception::Any

function Opt(p::Ptr{Cvoid})
@assert p != C_NULL
opt = new(p, Array{Callback_Data}(undef, 1), nothing)
finalizer(destroy, opt)
return opt
Expand All @@ -130,9 +131,6 @@ function Opt(algorithm::Algorithm, n::Integer)
throw(ArgumentError("invalid dimension $n < 0"))
end
p = nlopt_create(algorithm, n)
if p == C_NULL
error("Error in nlopt_create")
end
return Opt(p)
end

Expand Down Expand Up @@ -229,12 +227,6 @@ function chk(o::Opt, result::Result)
throw(ArgumentError("invalid NLopt arguments" * _errmsg(o)))
elseif result == OUT_OF_MEMORY
throw(OutOfMemoryError())
elseif result == FORCED_STOP
exception = getfield(o, :exception)
setfield!(o, :exception, nothing)
if exception !== nothing && !isa(exception, ForcedStop)
throw(exception)
end
else
error("nlopt failure $result", _errmsg(o))
end
Expand Down
97 changes: 96 additions & 1 deletion test/C_API.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,102 @@ function test_readme_example_vector()
return
end

function test_readme_example_vector()
function test_readme_example_vector_real_tol()
function my_objective_fn(x::Vector, grad::Vector)
if length(grad) > 0
grad[1] = 0
grad[2] = 0.5 / sqrt(x[2])
end
return sqrt(x[2])
end
function my_constraint_fn(ret, x::Vector, grad::Matrix)
if length(grad) > 0
grad[1, 1] = 3 * 2 * (2 * x[1] + 0)^2
grad[2, 1] = -1
grad[1, 2] = 3 * -1 * (-1 * x[1] + 1)^2
grad[2, 2] = -1
end
ret[1] = (2 * x[1] + 0)^3 - x[2]
ret[2] = (-1 * x[1] + 1)^3 - x[2]
return
end
opt = Opt(:LD_MMA, 2)
lower_bounds!(opt, [-Inf, 0.0])
xtol_rel!(opt, 1e-4)
min_objective!(opt, my_objective_fn)
tol = [1 // 1_000_000, 1 // 1_000_000]
inequality_constraint!(opt, my_constraint_fn, tol)
min_f, min_x, ret = optimize(opt, [1.234, 5.678])
@test min_f 0.5443310477213124
@test min_x [0.3333333342139688, 0.29629628951338166]
@test ret == :XTOL_REACHED
return
end

function test_vector_equality_constraint_real_tol()
function my_objective_fn(x::Vector, grad::Vector)
if length(grad) > 0
grad[1] = 0
grad[2] = 0.5 / sqrt(x[2])
end
return sqrt(x[2])
end
function my_constraint_fn(ret, x::Vector, grad::Matrix)
if length(grad) > 0
grad[1, 1] = 3 * 2 * (2 * x[1] + 0)^2
grad[2, 1] = -1
grad[1, 2] = 3 * -1 * (-1 * x[1] + 1)^2
grad[2, 2] = -1
end
ret[1] = (2 * x[1] + 0)^3 - x[2]
ret[2] = (-1 * x[1] + 1)^3 - x[2]
return
end
opt = Opt(:LD_SLSQP, 2)
lower_bounds!(opt, [-Inf, 0.0])
xtol_rel!(opt, 1e-4)
min_objective!(opt, my_objective_fn)
tol = [1 // 1_000_000, 1 // 1_000_000]
equality_constraint!(opt, my_constraint_fn, tol)
min_f, min_x, ret = optimize(opt, [1.234, 5.678])
@test min_f 0.5443310477213124
@test min_x [0.3333333342139688, 0.29629628951338166]
@test ret == :XTOL_REACHED
return
end

function test_vector_equality_constraint_scalar_tol()
function my_objective_fn(x::Vector, grad::Vector)
if length(grad) > 0
grad[1] = 0
grad[2] = 0.5 / sqrt(x[2])
end
return sqrt(x[2])
end
function my_constraint_fn(ret, x::Vector, grad::Matrix)
if length(grad) > 0
grad[1, 1] = 3 * 2 * (2 * x[1] + 0)^2
grad[2, 1] = -1
grad[1, 2] = 3 * -1 * (-1 * x[1] + 1)^2
grad[2, 2] = -1
end
ret[1] = (2 * x[1] + 0)^3 - x[2]
ret[2] = (-1 * x[1] + 1)^3 - x[2]
return
end
opt = Opt(:LD_SLSQP, 2)
lower_bounds!(opt, [-Inf, 0.0])
xtol_rel!(opt, 1e-4)
min_objective!(opt, my_objective_fn)
equality_constraint!(opt, 2, my_constraint_fn, 1 // 1_000_000)
min_f, min_x, ret = optimize(opt, [1.234, 5.678])
@test min_f 0.5443310477213124
@test min_x [0.3333333342139688, 0.29629628951338166]
@test ret == :XTOL_REACHED
return
end

function test_vector_forced_stop()
function my_objective_fn(x::Vector, grad::Vector)
if length(grad) > 0
grad[1] = 0
Expand Down

0 comments on commit 8a486d2

Please sign in to comment.