|
| 1 | +# Copyright (c) 2013: Steven G. Johnson and contributors |
| 2 | +# |
| 3 | +# Use of this source code is governed by an MIT-style license that can be found |
| 4 | +# in the LICENSE.md file or at https://opensource.org/licenses/MIT. |
| 5 | + |
| 6 | +module TestCAPI |
| 7 | + |
| 8 | +using NLopt |
| 9 | +using Test |
| 10 | + |
| 11 | +function runtests() |
| 12 | + for name in names(@__MODULE__; all = true) |
| 13 | + if !startswith("$(name)", "test_") |
| 14 | + continue |
| 15 | + end |
| 16 | + @testset "$(name)" begin |
| 17 | + getfield(@__MODULE__, name)() |
| 18 | + end |
| 19 | + end |
| 20 | + return |
| 21 | +end |
| 22 | + |
| 23 | +function test_issue_163() |
| 24 | + opt = Opt(:LN_COBYLA, 2) |
| 25 | + opt.min_objective = (x, g) -> sum(x .^ 2) |
| 26 | + inequality_constraint!(opt, 2, (result, x, g) -> (result .= 1 .- x)) |
| 27 | + (minf, minx, ret) = optimize(opt, [2.0, 2.0]) |
| 28 | + @test minx ≈ [1.0, 1.0] |
| 29 | + return |
| 30 | +end |
| 31 | + |
| 32 | +function test_issue_132() |
| 33 | + opt = Opt(:LN_COBYLA, 2) |
| 34 | + err = ErrorException( |
| 35 | + "Getting `initial_step` is unsupported. Use " * |
| 36 | + "`initial_step(opt, x)` to access the initial step at a point `x`.", |
| 37 | + ) |
| 38 | + @test_throws err opt.initial_step |
| 39 | + return |
| 40 | +end |
| 41 | + |
| 42 | +function test_issue_156_CapturedException() |
| 43 | + f(x, g = []) = (error("test error"); x[1]^2) |
| 44 | + opt = Opt(:LN_SBPLX, 1) |
| 45 | + opt.min_objective = f |
| 46 | + @test_throws CapturedException optimize(opt, [0.1234]) |
| 47 | + @test NLopt.nlopt_exception === nothing |
| 48 | + try |
| 49 | + optimize(opt, [0.1234]) |
| 50 | + catch e |
| 51 | + # Check that the backtrace is being printed |
| 52 | + @test length(sprint(show, e)) > 100 |
| 53 | + end |
| 54 | + return |
| 55 | +end |
| 56 | + |
| 57 | +function test_issue_156_ForcedStop() |
| 58 | + f(x, g = []) = (throw(NLopt.ForcedStop()); x[1]^2) |
| 59 | + opt = Opt(:LN_SBPLX, 1) |
| 60 | + opt.min_objective = f |
| 61 | + fmin, xmin, ret = optimize(opt, [0.1234]) |
| 62 | + @test ret == :FORCED_STOP |
| 63 | + @test NLopt.nlopt_exception === nothing |
| 64 | + return |
| 65 | +end |
| 66 | + |
| 67 | +function test_issue_156_no_error() |
| 68 | + f(x, g = []) = (x[1]^2) |
| 69 | + opt = Opt(:LN_SBPLX, 1) |
| 70 | + opt.min_objective = f |
| 71 | + fmin, xmin, ret = optimize(opt, [0.1234]) |
| 72 | + @test ret ∈ (:SUCCESS, :FTOL_REACHED, :XTOL_REACHED) |
| 73 | + @test NLopt.nlopt_exception === nothing |
| 74 | + return |
| 75 | +end |
| 76 | + |
| 77 | +function test_invalid_algorithms() |
| 78 | + @test_throws ArgumentError("unknown algorithm BILL") Algorithm(:BILL) |
| 79 | + @test_throws ArgumentError("unknown algorithm BILL") Opt(:BILL, 420) |
| 80 | + return |
| 81 | +end |
| 82 | + |
| 83 | +function test_issue_133() |
| 84 | + function rosenbrock(x::Vector, grad::Vector) |
| 85 | + if length(grad) > 0 |
| 86 | + grad[1] = -400 * x[1] * (x[2] - x[1]^2) - 2 * (1 - x[1]) |
| 87 | + grad[2] = 200 * (x[2] - x[1]^2) |
| 88 | + end |
| 89 | + return (1 - x[1])^2 + 100 * (x[2] - x[1]^2)^2 |
| 90 | + end |
| 91 | + function ineq01(x::Vector, grad::Vector) |
| 92 | + if length(grad) > 0 |
| 93 | + grad[1] = 1 |
| 94 | + grad[2] = 2 |
| 95 | + end |
| 96 | + return x[1] + 2 * x[2] - 1 |
| 97 | + end |
| 98 | + function ineq02(x::Vector, grad::Vector) |
| 99 | + if length(grad) > 0 |
| 100 | + grad[1] = 2 * x[1] |
| 101 | + grad[2] = 1 |
| 102 | + end |
| 103 | + return x[1]^2 + x[2] - 1 |
| 104 | + end |
| 105 | + function ineq03(x::Vector, grad::Vector) |
| 106 | + if length(grad) > 0 |
| 107 | + grad[1] = 2 * x[1] |
| 108 | + grad[2] = -1 |
| 109 | + end |
| 110 | + return x[1]^2 - x[2] - 1 |
| 111 | + end |
| 112 | + function eq01(x::Vector, grad::Vector) |
| 113 | + if length(grad) > 0 |
| 114 | + grad[1] = 2 |
| 115 | + grad[2] = 1 |
| 116 | + end |
| 117 | + return 2 * x[1] + x[2] - 1 |
| 118 | + end |
| 119 | + opt = Opt(:LD_SLSQP, 2) |
| 120 | + opt.lower_bounds = [0, -0.5] |
| 121 | + opt.upper_bounds = [1, 2] |
| 122 | + opt.xtol_rel = 1e-21 |
| 123 | + opt.min_objective = rosenbrock |
| 124 | + opt.inequality_constraint = ineq01 |
| 125 | + opt.inequality_constraint = ineq02 |
| 126 | + opt.inequality_constraint = ineq03 |
| 127 | + opt.equality_constraint = eq01 |
| 128 | + (minf, minx, ret) = optimize(opt, [0.5, 0]) |
| 129 | + println("got $minf at $minx with constraints (returned $ret)") |
| 130 | + @test minx[1] ≈ 0.4149 rtol = 1e-3 |
| 131 | + @test minx[2] ≈ 0.1701 rtol = 1e-3 |
| 132 | + remove_constraints!(opt) |
| 133 | + (minf, minx, ret) = optimize(opt, [0.5, 0]) |
| 134 | + println("got $minf at $minx after removing constraints (returned $ret)") |
| 135 | + @test minx[1] ≈ 1 rtol = 1e-5 |
| 136 | + @test minx[2] ≈ 1 rtol = 1e-5 |
| 137 | + return |
| 138 | +end |
| 139 | + |
| 140 | +function test_tutorial() |
| 141 | + count = 0 # keep track of # function evaluations |
| 142 | + function myfunc(x::Vector, grad::Vector) |
| 143 | + if length(grad) > 0 |
| 144 | + grad[1] = 0 |
| 145 | + grad[2] = 0.5 / sqrt(x[2]) |
| 146 | + end |
| 147 | + count::Int += 1 |
| 148 | + println("f_$count($x)") |
| 149 | + return sqrt(x[2]) |
| 150 | + end |
| 151 | + function myconstraint(x::Vector, grad::Vector, a, b) |
| 152 | + if length(grad) > 0 |
| 153 | + grad[1] = 3a * (a * x[1] + b)^2 |
| 154 | + grad[2] = -1 |
| 155 | + end |
| 156 | + return (a * x[1] + b)^3 - x[2] |
| 157 | + end |
| 158 | + opt = Opt(:LD_MMA, 2) |
| 159 | + opt.lower_bounds = [-Inf, 0.0] |
| 160 | + opt.xtol_rel = 1e-4 |
| 161 | + opt.min_objective = myfunc |
| 162 | + opt.inequality_constraint = (x, g) -> myconstraint(x, g, 2, 0) |
| 163 | + opt.inequality_constraint = (x, g) -> myconstraint(x, g, -1, 1) |
| 164 | + # test algorithm-parameter API |
| 165 | + opt.params["verbosity"] = 0 |
| 166 | + opt.params["inner_maxeval"] = 10 |
| 167 | + opt.params["dual_alg"] = NLopt.LD_MMA |
| 168 | + @test opt.params == Dict( |
| 169 | + "verbosity" => 0, |
| 170 | + "inner_maxeval" => 10, |
| 171 | + "dual_alg" => Int(NLopt.LD_MMA), |
| 172 | + ) |
| 173 | + @test get(opt.params, "foobar", 3.14159) === 3.14159 |
| 174 | + (minf, minx, ret) = optimize(opt, [1.234, 5.678]) |
| 175 | + println("got $minf at $minx after $count iterations (returned $ret)") |
| 176 | + @test minx[1] ≈ 1 / 3 rtol = 1e-5 |
| 177 | + @test minx[2] ≈ 8 / 27 rtol = 1e-5 |
| 178 | + @test minf ≈ sqrt(8 / 27) rtol = 1e-5 |
| 179 | + @test ret == :XTOL_REACHED |
| 180 | + @test opt.numevals == count |
| 181 | + return |
| 182 | +end |
| 183 | + |
| 184 | +end # module |
| 185 | + |
| 186 | +TestCAPI.runtests() |
0 commit comments