Skip to content

Commit 59acc36

Browse files
authored
Add keywork use_only_objgrad in functions calling grad! (#232)
* Apply JSOBestieTemplate update * Add keywork use_only_objgrad in functions calling grad!
1 parent 572458b commit 59acc36

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

src/trust-region/tron-trust-region.jl

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,10 @@ function aredpred!(
9292
Δm::T,
9393
x_trial::V,
9494
step::V,
95-
slope::T,
95+
slope::T;
96+
kwargs...,
9697
) where {T, V}
97-
ared, pred, tr.good_grad = aredpred_common(nlp, f, f_trial, Δm, x_trial, step, tr.gt, slope)
98+
ared, pred, tr.good_grad = aredpred_common(nlp, f, f_trial, Δm, x_trial, step, tr.gt, slope; kwargs...)
9899
γ = f_trial - f - slope
99100
tr.quad_min = γ <= 0 ? tr.increase_factor : max(tr.large_decrease_factor, -slope / γ / 2)
100101
return ared, pred
@@ -108,10 +109,11 @@ function aredpred!(
108109
Δm::T,
109110
x_trial::V,
110111
step::V,
111-
slope::T,
112+
slope::T;
113+
kwargs...
112114
) where {T, V}
113115
Fx = similar(x_trial, nls.nls_meta.nequ)
114-
return aredpred!(tr, nls, Fx, f, f_trial, Δm, x_trial, step, slope)
116+
return aredpred!(tr, nls, Fx, f, f_trial, Δm, x_trial, step, slope; kwargs...)
115117
end
116118

117119
function aredpred!(
@@ -123,9 +125,10 @@ function aredpred!(
123125
Δm::T,
124126
x_trial::V,
125127
step::V,
126-
slope::T,
128+
slope::T;
129+
kwargs...
127130
) where {T, V}
128-
ared, pred, tr.good_grad = aredpred_common(nls, Fx, f, f_trial, Δm, x_trial, step, tr.gt, slope)
131+
ared, pred, tr.good_grad = aredpred_common(nls, Fx, f, f_trial, Δm, x_trial, step, tr.gt, slope; kwargs...)
129132
γ = f_trial - f - slope
130133
tr.quad_min = γ <= 0 ? tr.increase_factor : max(tr.large_decrease_factor, -slope / γ / 2)
131134
return ared, pred

src/trust-region/trust-region.jl

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ function aredpred_common(
4444
x_trial::V,
4545
step::V,
4646
g_trial::V,
47-
slope::T,
47+
slope::T;
48+
use_only_objgrad::Bool = false,
4849
) where {T, V}
4950
absf = abs(f)
5051
ϵ = eps(T)
@@ -54,7 +55,11 @@ function aredpred_common(
5455
ared = f_trial - f + max(one(T), absf) * 10 * ϵ
5556
if (abs(Δm) < 10_000 * ϵ) || (abs(ared) < 10_000 * ϵ * absf)
5657
# correct for roundoff error
57-
grad!(nlp, x_trial, g_trial)
58+
if use_only_objgrad
59+
objgrad!(nlp, x_trial, g_trial)
60+
else
61+
grad!(nlp, x_trial, g_trial)
62+
end
5863
good_grad = true
5964
slope_trial = dot(g_trial, step)
6065
ared = (slope_trial + slope) / 2
@@ -71,7 +76,8 @@ function aredpred_common(
7176
x_trial::V,
7277
step::V,
7378
g_trial::V,
74-
slope::T,
79+
slope::T;
80+
kwargs...,
7581
) where {T, V}
7682
absf = abs(f)
7783
ϵ = eps(T)
@@ -110,9 +116,10 @@ function aredpred!(
110116
Δm::T,
111117
x_trial::V,
112118
step::V,
113-
slope::T,
119+
slope::T;
120+
kwargs...
114121
) where {T, V}
115-
ared, pred, tr.good_grad = aredpred_common(nlp, f, f_trial, Δm, x_trial, step, tr.gt, slope)
122+
ared, pred, tr.good_grad = aredpred_common(nlp, f, f_trial, Δm, x_trial, step, tr.gt, slope; kwargs...)
116123
return ared, pred
117124
end
118125

@@ -124,10 +131,11 @@ function aredpred!(
124131
Δm::T,
125132
x_trial::V,
126133
step::V,
127-
slope::T,
134+
slope::T;
135+
kwargs...
128136
) where {T, V}
129137
Fx = similar(x_trial, nls.nls_meta.nequ)
130-
ared, pred, tr.good_grad = aredpred_common(nls, Fx, f, f_trial, Δm, x_trial, step, tr.gt, slope)
138+
ared, pred, tr.good_grad = aredpred_common(nls, Fx, f, f_trial, Δm, x_trial, step, tr.gt, slope; kwargs...)
131139
return ared, pred
132140
end
133141

@@ -140,9 +148,10 @@ function aredpred!(
140148
Δm::T,
141149
x_trial::V,
142150
step::V,
143-
slope::T,
151+
slope::T;
152+
kwargs...
144153
) where {T, V}
145-
ared, pred, tr.good_grad = aredpred_common(nls, Fx, f, f_trial, Δm, x_trial, step, tr.gt, slope)
154+
ared, pred, tr.good_grad = aredpred_common(nls, Fx, f, f_trial, Δm, x_trial, step, tr.gt, slope; kwargs...)
146155
return ared, pred
147156
end
148157

0 commit comments

Comments
 (0)