Skip to content

Commit b749501

Browse files
committed
fix oop perform step and Fan scheme initialization
1 parent 79ab992 commit b749501

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

src/trustRegion.jl

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ function SciMLBase.__init(prob::NonlinearProblem{uType, iip}, alg::TrustRegion,
300300
p2 = convert(floatType, 0.25) # c5
301301
p3 = convert(floatType, 12.0) # c6
302302
p4 = convert(floatType, 1.0e18) # M
303-
initial_trust_radius = convert(trustType, p1 * (norm(fu)^0.99))
303+
initial_trust_radius = convert(trustType, p1 * (norm(fu1)^0.99))
304304
elseif radius_update_scheme === RadiusUpdateSchemes.Bastin
305305
step_threshold = convert(trustType, 0.05)
306306
shrink_threshold = convert(trustType, 0.05)
@@ -362,11 +362,8 @@ function perform_step!(cache::TrustRegionCache{false})
362362
dogleg!(cache)
363363

364364
# Compute the potentially new u
365-
if u isa Number
366-
cache.u_tmp = u + cache.du
367-
else
368-
@. cache.u_tmp = u + cache.du
369-
end
365+
cache.u_tmp = u + cache.du
366+
370367
cache.fu_new = f(cache.u_tmp, p)
371368
trust_region_step!(cache)
372369
cache.stats.nf += 1
@@ -592,29 +589,29 @@ end
592589
function dogleg!(cache::TrustRegionCache{false})
593590
@unpack u_tmp, u_cauchy, trust_r = cache
594591

595-
# Test if the full step is within the trust region.
592+
# Take the full Gauss-Newton step if lies within the trust region.
596593
if norm(u_tmp) trust_r
597594
cache.du = deepcopy(u_tmp)
598595
return
599596
end
600597

601-
# Calcualte Cauchy point, optimum along the steepest descent direction.
598+
## Take intersection of steepest descent direction and trust region if Cauchy point lies outside of trust region
602599
l_grad = norm(cache.g)
603600
d_cauchy = l_grad^3 / dot(cache.g, cache.H, cache.g) # distance of the cauchy point from the current iterate
604601
if d_cauchy > trust_r # cauchy point lies outside of trust region
605602
cache.du = - (trust_r/l_grad) * cache.g # step to the end of the trust region
606603
return
607604
end
608605

609-
# cauchy point lies inside the trust region
610-
u_cauchy = - (d_cauchy/l_grad) * cache.g
611-
612-
# Find the intersection point on the boundary.
613-
u_tmp -= u_cauchy # calf of the dogleg, use u_tmp to avoid allocation
614-
θ = dot(u_tmp, u_cauchy) # ~ cos(∠(calf,thigh))
615-
l_calf = dot(u_tmp,u_tmp) # length of the calf of the dogleg
616-
aux = max^2 + l_calf*(trust_r^2 - d_cauchy^2), 0) # technically guaranteed to be non-negative but hedging against floating point issues
617-
τ = ( - θ + sqrt( aux ) ) / l_calf # stepsize along dogleg
606+
# Take the intersection of dogled with trust region if Cauchy point lies inside the trust region
607+
u_cauchy = - (d_cauchy/l_grad) * cache.g # compute Cauchy point
608+
u_tmp -= u_cauchy # calf of the dogleg -- use u_tmp to avoid allocation
609+
a = dot(u_tmp, u_tmp)
610+
b = 2*dot(u_cauchy, u_tmp)
611+
c = d_cauchy^2 - trust_r^2
612+
aux = max(b^2 - 4*a*c, 0.0) # technically guaranteed to be non-negative but hedging against floating point issues
613+
τ = (-b + sqrt(aux)) / (2*a) # stepsize along dogleg to trust region boundary
614+
618615
cache.du = u_cauchy + τ * u_tmp
619616
end
620617

0 commit comments

Comments
 (0)