|
1 | | -using NonconvexPercival |
2 | | -using Test |
| 1 | +using NonconvexPercival, LinearAlgebra, Test |
3 | 2 |
|
4 | | -@testset "NonconvexPercival.jl" begin |
5 | | - # Write your tests here. |
| 3 | +f(x::AbstractVector) = sqrt(x[2]) |
| 4 | +g(x::AbstractVector, a, b) = (a*x[1] + b)^3 - x[2] |
| 5 | + |
| 6 | +alg = AugLag() |
| 7 | + |
| 8 | +@testset "Simple constraints" begin |
| 9 | + m = Model(f) |
| 10 | + addvar!(m, [0.0, 0.0], [10.0, 10.0]) |
| 11 | + add_ineq_constraint!(m, x -> g(x, 2, 0)) |
| 12 | + add_ineq_constraint!(m, x -> g(x, -1, 1)) |
| 13 | + for first_order in (true, false) |
| 14 | + options = AugLagOptions(first_order = first_order) |
| 15 | + r = optimize(m, alg, [1.234, 2.345], options = options) |
| 16 | + @test abs(r.minimum - sqrt(8/27)) < 1e-6 |
| 17 | + @test norm(r.minimizer - [1/3, 8/27]) < 1e-6 |
| 18 | + end |
| 19 | +end |
| 20 | + |
| 21 | +@testset "Equality constraints" begin |
| 22 | + m = Model(f) |
| 23 | + addvar!(m, [0.0, 0.0], [10.0, 10.0]) |
| 24 | + add_ineq_constraint!(m, x -> g(x, 2, 0)) |
| 25 | + add_ineq_constraint!(m, x -> g(x, -1, 1)) |
| 26 | + add_eq_constraint!(m, x -> sum(x) - 1/3 - 8/27) |
| 27 | + for first_order in (true, false) |
| 28 | + options = AugLagOptions(first_order = first_order) |
| 29 | + r = optimize(m, alg, [1.234, 2.345], options = options) |
| 30 | + @test abs(r.minimum - sqrt(8/27)) < 1e-6 |
| 31 | + @test norm(r.minimizer - [1/3, 8/27]) < 1e-6 |
| 32 | + end |
| 33 | +end |
| 34 | + |
| 35 | +@testset "Equality constraints BigFloat" begin |
| 36 | + T = BigFloat |
| 37 | + m = Model(f) |
| 38 | + addvar!(m, T.([0.0, 0.0]), T.([10.0, 10.0])) |
| 39 | + add_ineq_constraint!(m, x -> g(x, T(2), T(0))) |
| 40 | + add_ineq_constraint!(m, x -> g(x, T(-1), T(1))) |
| 41 | + add_eq_constraint!(m, x -> sum(x) - T(1/3) - T(8/27)) |
| 42 | + for first_order in (true, false) |
| 43 | + options = AugLagOptions(first_order = first_order) |
| 44 | + r = optimize(m, alg, T.([1.234, 2.345]), options = options) |
| 45 | + @test abs(r.minimum - sqrt(T(8/27))) < T(1e-6) |
| 46 | + @test norm(r.minimizer - T.([1/3, 8/27])) < T(1e-6) |
| 47 | + @test r.minimum isa T |
| 48 | + @test eltype(r.minimizer) <: T |
| 49 | + end |
| 50 | +end |
| 51 | + |
| 52 | +@testset "Block constraints" begin |
| 53 | + m = Model(f) |
| 54 | + addvar!(m, [0.0, 0.0], [10.0, 10.0]) |
| 55 | + add_ineq_constraint!(m, FunctionWrapper(x -> [g(x, 2, 0), g(x, -1, 1)], 2)) |
| 56 | + |
| 57 | + for first_order in (true, false) |
| 58 | + options = AugLagOptions(first_order = first_order) |
| 59 | + r = optimize(m, alg, [1.234, 2.345], options = options) |
| 60 | + @test abs(r.minimum - sqrt(8/27)) < 1e-6 |
| 61 | + @test norm(r.minimizer - [1/3, 8/27]) < 1e-6 |
| 62 | + end |
| 63 | +end |
| 64 | + |
| 65 | +@testset "Infinite bounds" begin |
| 66 | + @testset "Infinite upper bound" begin |
| 67 | + m = Model(f) |
| 68 | + addvar!(m, [0.0, 0.0], [Inf, Inf]) |
| 69 | + add_ineq_constraint!(m, x -> g(x, 2, 0)) |
| 70 | + add_ineq_constraint!(m, x -> g(x, -1, 1)) |
| 71 | + |
| 72 | + for first_order in (true, false) |
| 73 | + options = AugLagOptions(first_order = first_order) |
| 74 | + r = optimize(m, alg, [1.234, 2.345], options = options) |
| 75 | + @test abs(r.minimum - sqrt(8/27)) < 1e-6 |
| 76 | + @test norm(r.minimizer - [1/3, 8/27]) < 1e-6 |
| 77 | + end |
| 78 | + end |
| 79 | + #= |
| 80 | + @testset "Infinite lower bound" begin |
| 81 | + m = Model(f) |
| 82 | + addvar!(m, [-Inf, -Inf], [10, 10]) |
| 83 | + add_ineq_constraint!(m, x -> g(x, 2, 0)) |
| 84 | + add_ineq_constraint!(m, x -> g(x, -1, 1)) |
| 85 | +
|
| 86 | + for first_order in (true, false) |
| 87 | + options = AugLagOptions(first_order = first_order) |
| 88 | + r = optimize(m, alg, [1.234, 2.345], options = options) |
| 89 | + @test abs(r.minimum - sqrt(8/27)) < 1e-6 |
| 90 | + @test norm(r.minimizer - [1/3, 8/27]) < 1e-6 |
| 91 | + end |
| 92 | + end |
| 93 | + @testset "Infinite upper and lower bound" begin |
| 94 | + m = Model(f) |
| 95 | + addvar!(m, [-Inf, -Inf], [Inf, Inf]) |
| 96 | + add_ineq_constraint!(m, x -> g(x, 2, 0)) |
| 97 | + add_ineq_constraint!(m, x -> g(x, -1, 1)) |
| 98 | +
|
| 99 | + for first_order in (true, false) |
| 100 | + options = AugLagOptions(first_order = first_order) |
| 101 | + r = optimize(m, alg, [1.234, 2.345], options = options) |
| 102 | + @test abs(r.minimum - sqrt(8/27)) < 1e-6 |
| 103 | + @test norm(r.minimizer - [1/3, 8/27]) < 1e-6 |
| 104 | + end |
| 105 | + end |
| 106 | + =# |
6 | 107 | end |
0 commit comments