-
-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implementing some new Trust region radius update schemes #159
Conversation
@ChrisRackauckas is this the correct way to implement this? here, we take the input from the user regarding what update scheme they would like to use as a String |
No, never take in a string. Take in a type instance, like the algorithm choice. |
okay, got it, so I define all the radius update schemes as composite types? and then use them? just like the |
Hi @ChrisRackauckas , I made some changes regarding how we choose the relevant radius update scheme. Is this approach alright? |
Codecov Report
@@ Coverage Diff @@
## master #159 +/- ##
==========================================
- Coverage 91.28% 84.22% -7.06%
==========================================
Files 7 7
Lines 505 558 +53
==========================================
+ Hits 461 470 +9
- Misses 44 88 +44
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
src/trustRegion.jl
Outdated
struct RadiusUpdate{B} | ||
simple::B | ||
hei::B | ||
yuan::B | ||
bastin::B |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
struct RadiusUpdate{B} | |
simple::B | |
hei::B | |
yuan::B | |
bastin::B | |
struct RadiusUpdate | |
simple::Bool | |
hei::Bool | |
yuan::Bool | |
bastin::Bool |
src/trustRegion.jl
Outdated
return RadiusUpdate{Bool}(true, false, false, false) | ||
elseif hei | ||
return RadiusUpdate{Bool}(false, true, false, false) | ||
elseif yuan | ||
return RadiusUpdate{Bool}(false, false, true, false) | ||
elseif bastin | ||
return RadiusUpdate{Bool}(false, false, false, true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return RadiusUpdate{Bool}(true, false, false, false) | |
elseif hei | |
return RadiusUpdate{Bool}(false, true, false, false) | |
elseif yuan | |
return RadiusUpdate{Bool}(false, false, true, false) | |
elseif bastin | |
return RadiusUpdate{Bool}(false, false, false, true) | |
return RadiusUpdate{Bool}(true, false, false, false) | |
elseif hei | |
return RadiusUpdate{Bool}(false, true, false, false) | |
elseif yuan | |
return RadiusUpdate(false, false, true, false) | |
elseif bastin | |
return RadiusUpdate(false, false, false, true) |
src/trustRegion.jl
Outdated
function RadiusUpdate(;simple::Bool = Val{true}(), # 3 different radius update schemes | ||
hei::Bool = Val{false}(), | ||
yuan::Bool = Val{false}(), | ||
bastin::Bool = Val{false}()) | ||
if simple | ||
return RadiusUpdate{Bool}(true, false, false, false) | ||
elseif hei | ||
return RadiusUpdate{Bool}(false, true, false, false) | ||
elseif yuan |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't make sense. If it's going to be non-dispatching logic, use an EnumX.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yes, I am sorry I misinterpreted the use of Val
, but now it's clear. I have changed the overall approach.
@ChrisRackauckas I think this approach would be better! It would be great if you can take a look. I have a question though: |
and thanks for all the feedback that you have been giving, I am learning a lot :) |
See the ReturnCodes implementation and EnumX.jl https://github.com/SciML/SciMLBase.jl/blob/master/src/retcodes.jl |
Okay, so by this you mean that if the user makes a typo If yes, couldn't we simply check for the validity of the input beforehand and raise an exception in case of any mismatch? |
@ChrisRackauckas Is this approach with EnumX alright? I have used my previous approach of creating a |
src/trustRegion.jl
Outdated
Bastin | ||
end | ||
|
||
struct RadiusUpdate |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aren't they mutually exclusive? We should just use enum.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the structs can be deleted.
Everything should use the enums and the structs should be deleted. |
src/trustRegion.jl
Outdated
AbstractNewtonAlgorithm{CS, AD, FDT, ST, CJ} | ||
linsolve::L | ||
precs::P | ||
radius_update_scheme::RUS |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
radius_update_scheme::RUS | |
radius_update_scheme::RadiusUpdateSchemes.T |
It doesn't need to be parametric.,
src/trustRegion.jl
Outdated
@@ -138,6 +147,7 @@ mutable struct TrustRegionCache{iip, fType, algType, uType, resType, pType, | |||
retcode::SciMLBase.ReturnCode.T | |||
abstol::tolType | |||
prob::probType | |||
radius_update_scheme::radType |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here too
src/trustRegion.jl
Outdated
return nothing | ||
end | ||
|
||
function trust_region_step!(cache::TrustRegionCache) | ||
function trust_region_step!(cache::TrustRegionCache, ::Val{0}) # conventional radius update scheme |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make a single dispatch and branch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
function trust_region_step!(cache::TrustRegionCache, ::Val{0}) # conventional radius update scheme | |
function trust_region_step!(cache::TrustRegionCache) | |
@unpack radius_update_scheme = cache | |
if radius_update_scheme == RadiusUpdateSchemes.Simple | |
#method 1 | |
elseif radius_update_scheme == RadiusUpdateSchemes.Hei | |
#method 2 | |
... | |
end | |
end |
do you mean like this in a single method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cool! also if I need some helper functions, like in Hei's scheme, there is a monotonic function used which depends on the actual:predicted change ratio. So these kind of helper functions can go in utils.jl
, right?
@@ -10,6 +10,7 @@ using ForwardDiff: Dual | |||
using LinearAlgebra | |||
using StaticArraysCore | |||
using RecursiveArrayTools | |||
import EnumX |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing from the Project.toml
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah i added the pkg in the env but didn't commit it yet. will do it in the next commit
cache.force_stop = true | ||
end | ||
|
||
elseif radius_update_scheme === RadiusUpdateSchemes.Hei |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
error for not implemented
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay, like a TypeError when the radius_update_scheme
type doesn't match RadiusUpdateSchemes.T
?
src/trustRegion.jl
Outdated
# Parameters for the Schemes | ||
p1 = 0 | ||
p2 = 0 | ||
p3 = 0 | ||
p4 = 0 | ||
ϵ = 1e-8 | ||
if radius_update_scheme === RadiusUpdateSchemes.Hei | ||
step_threshold = 0 | ||
shrink_threshold = 0.25 | ||
expand_threshold = 0.25 | ||
p1 = 5.0 # M | ||
p2 = 0.1 # β | ||
p3 = 0.15 # γ1 | ||
p4 = 0.15 # γ2 | ||
elseif radius_update_scheme === RadiusUpdateSchemes.Yuan | ||
step_threshold = 0.0001 | ||
shrink_threshold = 0.25 | ||
expand_threshold = 0.25 | ||
p1 = 2.0 # μ | ||
p2 = 1/6 # c5 | ||
p3 = 6 # c6 | ||
p4 = 0 | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these values should match the type of u0
.
src/trustRegion.jl
Outdated
|
||
# yuan's scheme | ||
@unpack fu = cache | ||
cache.trust_r = p1 * cache.internalnorm(jacobian(cache, f) * fu) # we need the gradient at the new (k+1)th point WILL THIS BECOME ALLOCATING? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you don't needto calculate the whole Jacobian here, we can fix that in a future PR though.
Last step here is to make sure the tests don't fail, and fix the constants definitions so that they match the state types. |
Oh yes thanks, I understood it. I was just testing the two new methods myself and will make a commit real soon. |
Hi @ChrisRackauckas I can't quite get how can we get the gradient in line 421 without the whole jacobian? Did you mean something through jvp? |
No description provided.