Skip to content

Commit 6d55016

Browse files
authored
Fix the use of fgh! for optimize with lower order methods. (#127)
* Fix the use of fgh! for optimize with lower order methods. * Update incomplete.jl * Update incomplete.jl
1 parent e7f78c9 commit 6d55016

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/objective_types/incomplete.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct InplaceObjective{DF, FDF, FGH, Hv, FGHv}
1414
fghv::FGHv
1515
end
1616
InplaceObjective(;df=nothing, fdf=nothing, fgh=nothing, hv=nothing, fghv=nothing) = InplaceObjective(df, fdf, fgh, hv, fghv)
17-
17+
const InPlaceObjectiveFGH = InplaceObjective{<:Nothing, <:Nothing, <:Any, <:Any, <:Any}
1818
struct NotInplaceObjective{DF, FDF, FGH}
1919
df::DF
2020
fdf::FDF
@@ -41,8 +41,14 @@ fdf(t::Union{InplaceObjective, NotInplaceObjective}) = t.fdf
4141
# Mutating version
4242
make_f(t::InplaceObjective, x, F::Real) = x -> fdf(t)(F, nothing, x)
4343
make_f(t::InplaceObjective, x, F) = (F, x) -> fdf(t)(F, nothing, x)
44+
make_f(t::InPlaceObjectiveFGH, x, F::Real) = x -> t.fgh(F, nothing, nothing, x)
45+
46+
4447
make_df(t::InplaceObjective, x, F) = (DF, x) -> fdf(t)(nothing, DF, x)
48+
make_df(t::InPlaceObjectiveFGH, x, F) = (DF, x) -> t.fgh(nothing, DF, nothing, x)
49+
4550
make_fdf(t::InplaceObjective, x, F::Real) = (G, x) -> fdf(t)(F, G, x)
51+
make_fdf(t::InPlaceObjectiveFGH, x, F::Real) = (G, x) -> t.fgh(F, G, nothing, x)
4652
make_fdf(t::InplaceObjective, x, F) = fdf(t)
4753

4854
# Non-mutating version

test/incomplete.jl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,41 @@ end
182182
end
183183

184184
end
185+
186+
@testset "https://github.com/JuliaNLSolvers/Optim.jl/issues/718" begin
187+
f(x) = (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2
188+
function g!(G, x)
189+
G[1] = -2.0 * (1.0 - x[1]) - 400.0 * (x[2] - x[1]^2) * x[1]
190+
G[2] = 200.0 * (x[2] - x[1]^2)
191+
end
192+
function h!(H, x)
193+
H[1, 1] = 2.0 - 400.0 * x[2] + 1200.0 * x[1]^2
194+
H[1, 2] = -400.0 * x[1]
195+
H[2, 1] = -400.0 * x[1]
196+
H[2, 2] = 200.0
197+
end
198+
199+
function fg!(F,G,x)
200+
G == nothing || g!(G,x)
201+
F == nothing || return f(x)
202+
nothing
203+
end
204+
function fgh!(F,G,H,x)
205+
G == nothing || g!(G,x)
206+
H == nothing || h!(H,x)
207+
F == nothing || return f(x)
208+
nothing
209+
end
210+
211+
gx = [0.0,0.0]
212+
x=[0.0,0.0]
213+
214+
@test NLSolversBase.make_f(only_fgh!(fgh!),[0.0,0.0],0.0)(x) == 1.0
215+
@test NLSolversBase.make_df(only_fgh!(fgh!),[0.0,0.0],0.0)(gx, x) == nothing
216+
@test gx == [-2.0, 0.0]
217+
218+
gx = [0.0,0.0]
219+
@test NLSolversBase.make_fdf(only_fgh!(fgh!),[0.0,0.0],0.0)(gx, x) == 1.0
220+
@test gx == [-2.0, 0.0]
221+
222+
end

0 commit comments

Comments
 (0)