Skip to content

Commit 04b6c92

Browse files
authored
Add posibility of calculating [value] gradient and hessian for Newton… (#137)
* Add posibility of calculating [value] gradient and hessian for NewtonTrustRegion efficiency. * Impove gradient_hessiangit add src/ Project.toml .travis.yml .
1 parent a331a28 commit 04b6c92

File tree

4 files changed

+53
-11
lines changed

4 files changed

+53
-11
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ os:
44
- linux
55
- osx
66
julia:
7-
- 1.2
7+
- 1
88
- nightly
99
notifications:
1010
email: false

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
1212
DiffResults = "1.0"
1313
ForwardDiff = "0.10"
1414
FiniteDiff = "2.0"
15-
julia = "1.2"
15+
julia = "1.5"
1616

1717
[extras]
1818
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/objective_types/incomplete.jl

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,23 +93,63 @@ end
9393
const InPlaceFGH = InplaceObjective{<:Nothing,<:Nothing,TH,<:Nothing,<:Nothing} where {TH}
9494
const InPlaceFG_HV = InplaceObjective{<:Nothing,TFG,<:Nothing,THv,<:Nothing} where {TFG,THv}
9595
const InPlaceFGHV = InplaceObjective{<:Nothing,<:Nothing,<:Nothing,<:Nothing,TFGHv} where {TFGHv}
96-
9796
function TwiceDifferentiable(t::InPlaceFGH, x::AbstractArray, F::Real = real(zero(eltype(x))), G::AbstractArray = alloc_DF(x, F), H = alloc_H(x, F)) where {TH}
9897
f = x -> t.fgh(F, nothing, nothing, x)
9998
df = (G, x) -> t.fgh(nothing, G, nothing, x)
10099
fdf = (G, x) -> t.fgh(F, G, nothing, x)
100+
fdfh = (G, H, x) -> t.fgh(F, G, H, x)
101+
dfh = (G, H, x) -> t.fgh(nothing, G, H, x)
101102
h = (H, x) -> t.fgh(F, nothing, H, x)
102-
TwiceDifferentiable(f, df, fdf, h, x, F, G, H)
103-
end
104103

105-
function TwiceDifferentiable(t::InPlaceFGH, x::AbstractVector, F::Real = real(zero(eltype(x))), G::AbstractVector = similar(x)) where {TH}
104+
x_f, x_df, x_h = x_of_nans(x), x_of_nans(x), x_of_nans(x)
106105

107-
H = alloc_H(x, F)
106+
TwiceDifferentiable(f, df, fdf, dfh, fdfh, h,
107+
copy(F), copy(G), copy(H),
108+
x_f, x_df, x_h,
109+
[0,], [0,], [0,])
110+
end
111+
function TwiceDifferentiable(t::InPlaceFGH, x::AbstractVector{T}, F::Real = real(zero(eltype(x))), G::AbstractVector{Tx} = alloc_DF(x, F)) where {T, Tx}
108112
f = x -> t.fgh(F, nothing, nothing, x)
109113
df = (G, x) -> t.fgh(nothing, G, nothing, x)
110114
fdf = (G, x) -> t.fgh(F, G, nothing, x)
115+
fdfh = (G, H, x) -> t.fgh(F, G, H, x)
116+
dfh = (G, H, x) -> t.fgh(nothing, G, H, x)
111117
h = (H, x) -> t.fgh(F, nothing, H, x)
112-
TwiceDifferentiable(f, df, fdf, h, x, F, G, H)
118+
119+
H = alloc_H(x, F)
120+
x_f, x_df, x_h = x_of_nans(x), x_of_nans(x), x_of_nans(x)
121+
122+
TwiceDifferentiable(f, df, fdf, dfh, fdfh, h,
123+
copy(F), copy(G), copy(H),
124+
x_f, x_df, x_h,
125+
[0,], [0,], [0,])
126+
end
127+
function value_gradient_hessian!!(obj, x)
128+
obj.f_calls .+= 1
129+
obj.df_calls .+= 1
130+
obj.h_calls .+= 1
131+
obj.x_f .= x
132+
obj.x_df .= x
133+
obj.x_h .= x
134+
if obj.fdfh === nothing
135+
obj.F = obj.fdf(obj.DF, x)
136+
obj.h(obj.H, x)
137+
else
138+
obj.F = obj.fdfh(obj.DF, obj.H, x)
139+
end
140+
end
141+
142+
function gradient_hessian!!(obj, x)
143+
if obj.dfh === nothing
144+
gradient!!(obj, x)
145+
hessian!!(obj, x)
146+
else
147+
obj.df_calls .+= 1
148+
obj.h_calls .+= 1
149+
obj.x_df .= x
150+
obj.x_h .= x
151+
obj.dfh(obj.DF, obj.H, x)
152+
end
113153
end
114154

115155
function TwiceDifferentiableHV(t::InPlaceFG_HV, x::AbstractVector)

src/objective_types/twicedifferentiable.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ mutable struct TwiceDifferentiable{T,TDF,TH,TX} <: AbstractObjective
33
f
44
df
55
fdf
6+
dfh
7+
fdfh
68
h
79
F::T
810
DF::TDF
@@ -22,7 +24,7 @@ function TwiceDifferentiable(f, g, fg, h, x::TX, F::T = real(zero(eltype(x))), G
2224
fg! = fdf!_from_fdf(fg, F, inplace)
2325
h! = h!_from_h(h, F, inplace)
2426

25-
TwiceDifferentiable{T,TG,TH,TX}(f, g!, fg!, h!,
27+
TwiceDifferentiable{T,TG,TH,TX}(f, g!, fg!, nothing, nothing, h!,
2628
copy(F), copy(G), copy(H),
2729
x_f, x_df, x_h,
2830
[0,], [0,], [0,])
@@ -39,7 +41,7 @@ function TwiceDifferentiable(f, g, h,
3941
fg! = make_fdf(x, F, f, g!)
4042
x_f, x_df, x_h = x_of_nans(x), x_of_nans(x), x_of_nans(x)
4143

42-
return TwiceDifferentiable(f, g!, fg!, h!, F, G, H, x_f, x_df, x_h, [0,], [0,], [0,])
44+
return TwiceDifferentiable(f, g!, fg!, nothing, nothing, h!, F, G, H, x_f, x_df, x_h, [0,], [0,], [0,])
4345
end
4446

4547

@@ -98,7 +100,7 @@ function TwiceDifferentiable(d::OnceDifferentiable, x_seed::AbstractVector{T} =
98100
return TwiceDifferentiable(d.f, d.df, d.fdf, h!, x_seed, F, gradient(d))
99101
end
100102

101-
function TwiceDifferentiable(f, x::AbstractVector, F::Real = real(zero(eltype(x)));
103+
function TwiceDifferentiable(f, x::AbstractArray, F::Real = real(zero(eltype(x)));
102104
autodiff = :finite, inplace = true)
103105
if is_finitediff(autodiff)
104106

0 commit comments

Comments
 (0)