|
1 | 1 | @testset "maximization wrapper" begin
|
2 | 2 | @testset "univariate" begin
|
3 |
| - resmax = maximize(x->x^3, -1, 9) |
4 |
| - resmin = optimize(x->-x^3, -1, 9) |
| 3 | + resmax = maximize(x -> x^3, -1, 9) |
| 4 | + resmin = optimize(x -> -x^3, -1, 9) |
5 | 5 | @test Optim.maximum(resmax) == -Optim.minimum(resmin)
|
6 | 6 | @test resmax.res.minimum == resmin.minimum
|
7 | 7 | for meth in (Brent(), GoldenSection())
|
8 |
| - resmax = maximize(x->x^3, -1, 9, meth) |
9 |
| - resmin = optimize(x->-x^3, -1, 9, meth) |
| 8 | + resmax = maximize(x -> x^3, -1, 9, meth) |
| 9 | + resmin = optimize(x -> -x^3, -1, 9, meth) |
10 | 10 | @test Optim.maximum(resmax) == -Optim.minimum(resmin)
|
11 | 11 | @test resmax.res.minimum == resmin.minimum
|
12 | 12 | end
|
13 | 13 | end
|
14 | 14 | @testset "multivariate" begin
|
15 |
| - resmax = maximize(x->x[1]^3+x[2]^2, [3.0, 0.0]) |
16 |
| - resmin = optimize(x->-x[1]^3-x[2]^2, [3.0, 0.0]) |
| 15 | + resmax = maximize(x -> x[1]^3 + x[2]^2, [3.0, 0.0]) |
| 16 | + resmin = optimize(x -> -x[1]^3 - x[2]^2, [3.0, 0.0]) |
17 | 17 | @test Optim.maximum(resmax) == -Optim.minimum(resmin)
|
18 | 18 | @test resmax.res.minimum == resmin.minimum
|
19 |
| - for meth in (NelderMead(), BFGS(), LBFGS(), GradientDescent(), Newton(), NewtonTrustRegion(), SimulatedAnnealing()) |
20 |
| - resmax = maximize(x->x[1]^3+x[2]^2, [3.0, 0.0]) |
21 |
| - resmin = optimize(x->-x[1]^3-x[2]^2, [3.0, 0.0]) |
| 19 | + for meth in ( |
| 20 | + NelderMead(), |
| 21 | + BFGS(), |
| 22 | + LBFGS(), |
| 23 | + GradientDescent(), |
| 24 | + Newton(), |
| 25 | + NewtonTrustRegion(), |
| 26 | + SimulatedAnnealing(), |
| 27 | + ) |
| 28 | + resmax = maximize(x -> x[1]^3 + x[2]^2, [3.0, 0.0]) |
| 29 | + resmin = optimize(x -> -x[1]^3 - x[2]^2, [3.0, 0.0]) |
22 | 30 | @test Optim.maximum(resmax) == -Optim.minimum(resmin)
|
23 | 31 | @test resmax.res.minimum == resmin.minimum
|
24 | 32 | end
|
25 | 33 | end
|
| 34 | + |
| 35 | + prob = MVP.UnconstrainedProblems.examples["Powell"] |
| 36 | + f = objective(prob) |
| 37 | + g! = gradient(prob) |
| 38 | + h! = hessian(prob) |
| 39 | + fmax(x) = -f(x) |
| 40 | + gmax = (G, x) -> (g!(G, x); G .= -G) |
| 41 | + hmax = (H, x) -> (h!(H, x); H .= -H) |
| 42 | + |
| 43 | + resmax_f = maximize(fmax, prob.initial_x) |
| 44 | + resmin_f = optimize(f, prob.initial_x) |
| 45 | + for prop in (:iterations, :ls_success, :minimizer, :minimum) |
| 46 | + @test getproperty(resmax_f.res, prop) == getproperty(resmin_f, prop) |
| 47 | + end |
| 48 | + |
| 49 | + resmax_f_nm = maximize(fmax, prob.initial_x, NelderMead()) |
| 50 | + resmin_f_nm = optimize(f, prob.initial_x, NelderMead()) |
| 51 | + for prop in (:iterations, :ls_success, :minimizer, :minimum) |
| 52 | + @test getproperty(resmax_f_nm.res, prop) == getproperty(resmin_f_nm, prop) |
| 53 | + end |
| 54 | + |
| 55 | + resmax_f_bfgs = maximize(fmax, prob.initial_x, BFGS()) |
| 56 | + resmin_f_bfgs = optimize(f, prob.initial_x, BFGS()) |
| 57 | + for prop in (:iterations, :ls_success, :minimizer, :minimum) |
| 58 | + @test getproperty(resmax_f_bfgs.res, prop) == getproperty(resmin_f_bfgs, prop) |
| 59 | + end |
| 60 | + |
| 61 | + resmax_f_newton = maximize(fmax, prob.initial_x, Newton()) |
| 62 | + resmin_f_newton = optimize(f, prob.initial_x, Newton()) |
| 63 | + for prop in (:iterations, :ls_success, :minimizer, :minimum) |
| 64 | + @test getproperty(resmax_f_newton.res, prop) == getproperty(resmin_f_newton, prop) |
| 65 | + end |
| 66 | + |
| 67 | + resmax_fg = maximize(fmax, gmax, prob.initial_x, BFGS()) |
| 68 | + resmin_fg = optimize(f, g!, prob.initial_x, BFGS()) |
| 69 | + for prop in (:iterations, :ls_success, :minimizer, :minimum) |
| 70 | + @test getproperty(resmax_fg.res, prop) == getproperty(resmin_fg, prop) |
| 71 | + end |
| 72 | + |
| 73 | + resmax_fgh = maximize(fmax, gmax, hmax, prob.initial_x, Newton()) |
| 74 | + resmin_fgh = optimize(f, g!, h!, prob.initial_x, Newton()) |
| 75 | + for prop in (:iterations, :ls_success, :minimizer, :minimum) |
| 76 | + @test getproperty(resmax_fgh.res, prop) == getproperty(resmin_fgh, prop) |
| 77 | + end |
| 78 | + |
26 | 79 | end
|
0 commit comments