Skip to content

Commit 80e498b

Browse files
devmotionpkofod
andauthored
Fix unnecessary allocations in maximization due to insufficient broadcasting (#1189)
* Fix unnecessary allocations in maximization due to insufficient broadcasting * Fix a few more cases --------- Co-authored-by: Patrick Kofod Mogensen <[email protected]>
1 parent 6f0b96d commit 80e498b

File tree

4 files changed

+31
-13
lines changed

4 files changed

+31
-13
lines changed

src/maximize.jl

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ res(r::MaximizationWrapper) = r.res
99
# Univariate warppers
1010
# ==============================================================================
1111
function maximize(f, lb::Real, ub::Real, method::AbstractOptimizer; kwargs...)
12-
fmax = x -> -f(x)
12+
fmax = let f=f
13+
x -> -f(x)
14+
end
1315
MaximizationWrapper(optimize(fmax, lb, ub, method; kwargs...))
1416
end
1517

1618
function maximize(f, lb::Real, ub::Real; kwargs...)
17-
fmax = x -> -f(x)
19+
fmax = let f=f
20+
x -> -f(x)
21+
end
1822
MaximizationWrapper(optimize(fmax, lb, ub; kwargs...))
1923
end
2024

@@ -23,7 +27,9 @@ end
2327
# Multivariate warppers
2428
# ==============================================================================
2529
function maximize(f, x0::AbstractArray, options::Options = Options())
26-
fmax = x -> -f(x)
30+
fmax = let f=f
31+
x -> -f(x)
32+
end
2733
MaximizationWrapper(optimize(fmax, x0, options))
2834
end
2935
function maximize(
@@ -32,7 +38,9 @@ function maximize(
3238
method::AbstractOptimizer,
3339
options = Optim.Options()
3440
)
35-
fmax = x -> -f(x)
41+
fmax = let f=f
42+
x -> -f(x)
43+
end
3644
MaximizationWrapper(optimize(fmax, x0, method, options))
3745
end
3846
function maximize(
@@ -42,8 +50,12 @@ function maximize(
4250
method::AbstractOptimizer,
4351
options = Optim.Options()
4452
)
45-
fmax = x -> -f(x)
46-
gmax = (G, x) -> (g(G, x); G .= -G)
53+
fmax = let f=f
54+
x -> -f(x)
55+
end
56+
gmax = let g=g
57+
(G, x) -> (g(G, x); G .= .-G)
58+
end
4759
MaximizationWrapper(optimize(fmax, gmax, x0, method, options))
4860
end
4961

@@ -55,9 +67,15 @@ function maximize(
5567
method::AbstractOptimizer,
5668
options = Optim.Options()
5769
)
58-
fmax = x -> -f(x)
59-
gmax = (G, x) -> (g(G, x); G .= -G)
60-
hmax = (H, x) -> (h(H, x); H .= -H)
70+
fmax = let f=f
71+
x -> -f(x)
72+
end
73+
gmax = let g=g
74+
(G, x) -> (g(G, x); G .= .-G)
75+
end
76+
hmax = let h=h
77+
(H, x) -> (h(H, x); H .= .-H)
78+
end
6179
MaximizationWrapper(optimize(fmax, gmax, hmax, x0, method, options))
6280
end
6381

src/multivariate/solvers/first_order/cg.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ function reset!(cg::ConjugateGradient, cgs::ConjugateGradientState, obj, x)
110110
if cg.P !== nothing
111111
project_tangent!(cg.manifold, cgs.pg, x)
112112
end
113-
cgs.s .= -cgs.pg
113+
cgs.s .= .-cgs.pg
114114
cgs.f_x_previous = typeof(cgs.f_x_previous)(NaN)
115115
end
116116
function initial_state(method::ConjugateGradient, options, d, initial_x)

src/multivariate/solvers/second_order/krylov_trust_region.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ function cg_steihaug!(
113113

114114
fill!(z, 0.0) # the search direction is initialized to the 0 vector,
115115
r .= g # so at first the whole gradient is the residual.
116-
d .= -r # the first direction is the direction of steepest descent.
116+
d .= .-r # the first direction is the direction of steepest descent.
117117
rho0 = 1e100 # just a big number
118118

119119
state.cg_iters = 0

test/general/maximize.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
g! = gradient(prob)
3838
h! = hessian(prob)
3939
fmax(x) = -f(x)
40-
gmax = (G, x) -> (g!(G, x); G .= -G)
41-
hmax = (H, x) -> (h!(H, x); H .= -H)
40+
gmax = (G, x) -> (g!(G, x); G .= .-G)
41+
hmax = (H, x) -> (h!(H, x); H .= .-H)
4242

4343
function test_same_content(f1, f2)
4444
for prop in (:minimizer, :minimum)

0 commit comments

Comments
 (0)