-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Remove unnecessary snippet
- Loading branch information
Showing
4 changed files
with
343 additions
and
546 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
""" | ||
RobustMultiNewton( | ||
::Type{T} = Float64; | ||
concrete_jac = nothing, | ||
linsolve = nothing, | ||
autodiff = nothing, vjp_autodiff = nothing, jvp_autodiff = nothing | ||
) | ||
A polyalgorithm focused on robustness. It uses a mixture of Newton methods with different | ||
globalizing techniques (trust region updates, line searches, etc.) in order to find a | ||
method that is able to adequately solve the minimization problem. | ||
Basically, if this algorithm fails, then "most" good ways of solving your problem fail and | ||
you may need to think about reformulating the model (either there is an issue with the model, | ||
or more precision / more stable linear solver choice is required). | ||
### Arguments | ||
- `T`: The eltype of the initial guess. It is only used to check if some of the algorithms | ||
are compatible with the problem type. Defaults to `Float64`. | ||
""" | ||
function RobustMultiNewton( | ||
::Type{T} = Float64; | ||
concrete_jac = nothing, | ||
linsolve = nothing, | ||
autodiff = nothing, vjp_autodiff = nothing, jvp_autodiff = nothing | ||
) where {T} | ||
common_kwargs = (; concrete_jac, linsolve, autodiff, vjp_autodiff, jvp_autodiff) | ||
if T <: Complex # Let's atleast have something here for complex numbers | ||
algs = ( | ||
NewtonRaphson(; common_kwargs...), | ||
) | ||
else | ||
algs = ( | ||
TrustRegion(; common_kwargs...), | ||
TrustRegion(; common_kwargs..., radius_update_scheme = RUS.Bastin), | ||
NewtonRaphson(; common_kwargs...), | ||
NewtonRaphson(; common_kwargs..., linesearch = BackTracking()), | ||
TrustRegion(; common_kwargs..., radius_update_scheme = RUS.NLsolve), | ||
TrustRegion(; common_kwargs..., radius_update_scheme = RUS.Fan) | ||
) | ||
end | ||
return NonlinearSolvePolyAlgorithm(algs) | ||
end | ||
|
||
""" | ||
FastShortcutNonlinearPolyalg( | ||
::Type{T} = Float64; | ||
concrete_jac = nothing, | ||
linsolve = nothing, | ||
must_use_jacobian::Val = Val(false), | ||
prefer_simplenonlinearsolve::Val = Val(false), | ||
autodiff = nothing, vjp_autodiff = nothing, jvp_autodiff = nothing, | ||
u0_len::Union{Int, Nothing} = nothing | ||
) where {T} | ||
A polyalgorithm focused on balancing speed and robustness. It first tries less robust methods | ||
for more performance and then tries more robust techniques if the faster ones fail. | ||
### Arguments | ||
- `T`: The eltype of the initial guess. It is only used to check if some of the algorithms | ||
are compatible with the problem type. Defaults to `Float64`. | ||
### Keyword Arguments | ||
- `u0_len`: The length of the initial guess. If this is `nothing`, then the length of the | ||
initial guess is not checked. If this is an integer and it is less than `25`, we use | ||
jacobian based methods. | ||
""" | ||
function FastShortcutNonlinearPolyalg( | ||
::Type{T} = Float64; | ||
concrete_jac = nothing, | ||
linsolve = nothing, | ||
must_use_jacobian::Val = Val(false), | ||
prefer_simplenonlinearsolve::Val = Val(false), | ||
autodiff = nothing, vjp_autodiff = nothing, jvp_autodiff = nothing, | ||
u0_len::Union{Int, Nothing} = nothing | ||
) where {T} | ||
start_index = 1 | ||
common_kwargs = (; concrete_jac, linsolve, autodiff, vjp_autodiff, jvp_autodiff) | ||
if must_use_jacobian isa Val{true} | ||
if T <: Complex | ||
algs = (NewtonRaphson(; common_kwargs...),) | ||
else | ||
algs = ( | ||
NewtonRaphson(; common_kwargs...), | ||
NewtonRaphson(; common_kwargs..., linesearch = BackTracking()), | ||
TrustRegion(; common_kwargs...), | ||
TrustRegion(; common_kwargs..., radius_update_scheme = RUS.Bastin) | ||
) | ||
end | ||
else | ||
# SimpleNewtonRaphson and SimpleTrustRegion are not robust to singular Jacobians | ||
# and thus are not included in the polyalgorithm | ||
if prefer_simplenonlinearsolve isa Val{true} | ||
if T <: Complex | ||
algs = ( | ||
SimpleBroyden(), | ||
Broyden(; init_jacobian = Val(:true_jacobian), autodiff), | ||
SimpleKlement(), | ||
NewtonRaphson(; common_kwargs...) | ||
) | ||
else | ||
start_index = u0_len !== nothing ? (u0_len ≤ 25 ? 4 : 1) : 1 | ||
algs = ( | ||
SimpleBroyden(), | ||
Broyden(; init_jacobian = Val(:true_jacobian), autodiff), | ||
SimpleKlement(), | ||
NewtonRaphson(; common_kwargs...), | ||
NewtonRaphson(; common_kwargs..., linesearch = BackTracking()), | ||
TrustRegion(; common_kwargs...), | ||
TrustRegion(; common_kwargs..., radius_update_scheme = RUS.Bastin) | ||
) | ||
end | ||
else | ||
if T <: Complex | ||
algs = ( | ||
Broyden(; autodiff), | ||
Broyden(; init_jacobian = Val(:true_jacobian), autodiff), | ||
Klement(; linsolve, autodiff), | ||
NewtonRaphson(; common_kwargs...) | ||
) | ||
else | ||
# TODO: This number requires a bit rigorous testing | ||
start_index = u0_len !== nothing ? (u0_len ≤ 25 ? 4 : 1) : 1 | ||
algs = ( | ||
Broyden(; autodiff), | ||
Broyden(; init_jacobian = Val(:true_jacobian), autodiff), | ||
Klement(; linsolve, autodiff), | ||
NewtonRaphson(; common_kwargs...), | ||
NewtonRaphson(; common_kwargs..., linesearch = BackTracking()), | ||
TrustRegion(; common_kwargs...), | ||
TrustRegion(; common_kwargs..., radius_update_scheme = RUS.Bastin) | ||
) | ||
end | ||
end | ||
end | ||
return NonlinearSolvePolyAlgorithm(algs; start_index) | ||
end | ||
|
||
""" | ||
FastShortcutNLLSPolyalg( | ||
::Type{T} = Float64; | ||
concrete_jac = nothing, | ||
linsolve = nothing, | ||
autodiff = nothing, vjp_autodiff = nothing, jvp_autodiff = nothing | ||
) | ||
A polyalgorithm focused on balancing speed and robustness. It first tries less robust methods | ||
for more performance and then tries more robust techniques if the faster ones fail. | ||
### Arguments | ||
- `T`: The eltype of the initial guess. It is only used to check if some of the algorithms | ||
are compatible with the problem type. Defaults to `Float64`. | ||
""" | ||
function FastShortcutNLLSPolyalg( | ||
::Type{T} = Float64; | ||
concrete_jac = nothing, | ||
linsolve = nothing, | ||
autodiff = nothing, vjp_autodiff = nothing, jvp_autodiff = nothing | ||
) where {T} | ||
common_kwargs = (; linsolve, autodiff, vjp_autodiff, jvp_autodiff) | ||
if T <: Complex | ||
algs = ( | ||
GaussNewton(; common_kwargs..., concrete_jac), | ||
LevenbergMarquardt(; common_kwargs..., disable_geodesic = Val(true)), | ||
LevenbergMarquardt(; common_kwargs...) | ||
) | ||
else | ||
algs = ( | ||
GaussNewton(; common_kwargs..., concrete_jac), | ||
LevenbergMarquardt(; common_kwargs..., disable_geodesic = Val(true)), | ||
TrustRegion(; common_kwargs..., concrete_jac), | ||
GaussNewton(; common_kwargs..., linesearch = BackTracking(), concrete_jac), | ||
TrustRegion(; | ||
common_kwargs..., radius_update_scheme = RUS.Bastin, concrete_jac | ||
), | ||
LevenbergMarquardt(; common_kwargs...) | ||
) | ||
end | ||
return NonlinearSolvePolyAlgorithm(algs) | ||
end |
Oops, something went wrong.