Skip to content

Commit 35251d3

Browse files
authored
Update maximize.jl (#1119)
* Update maximize.jl * Update maximize.jl * Update maximize.jl
1 parent 77501f4 commit 35251d3

File tree

2 files changed

+63
-10
lines changed

2 files changed

+63
-10
lines changed

src/maximize.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ end
3838
function maximize(f, g, h, x0::AbstractArray, method::AbstractOptimizer, options = Optim.Options(); kwargs...)
3939
fmax = x->-f(x)
4040
gmax = (G,x)->(g(G,x); G.=-G)
41-
hmax = (H,x)->(h(G,x); H.=-H)
41+
hmax = (H,x)->(h(H,x); H.=-H)
4242
MaximizationWrapper(optimize(fmax, gmax, hmax, x0, method, options; kwargs...))
4343
end
4444

test/general/maximize.jl

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,79 @@
11
@testset "maximization wrapper" begin
22
@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)
55
@test Optim.maximum(resmax) == -Optim.minimum(resmin)
66
@test resmax.res.minimum == resmin.minimum
77
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)
1010
@test Optim.maximum(resmax) == -Optim.minimum(resmin)
1111
@test resmax.res.minimum == resmin.minimum
1212
end
1313
end
1414
@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])
1717
@test Optim.maximum(resmax) == -Optim.minimum(resmin)
1818
@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])
2230
@test Optim.maximum(resmax) == -Optim.minimum(resmin)
2331
@test resmax.res.minimum == resmin.minimum
2432
end
2533
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+
2679
end

0 commit comments

Comments
 (0)