diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml index ce8609b..d007585 100644 --- a/.github/workflows/format.yml +++ b/.github/workflows/format.yml @@ -1,30 +1,30 @@ -name: Format +name: Runic formatting on: push: - branches: [master] - tags: [v*] + branches: + - 'master' + - 'release-' + tags: + - '*' pull_request: - jobs: - format: - name: "Format Check" + runic: + name: Runic runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 - - uses: julia-actions/setup-julia@v2 + - uses: actions/checkout@v5 + # - uses: julia-actions/setup-julia@v2 + # with: + # version: '1' + # - uses: julia-actions/cache@v2 + - uses: fredrikekre/runic-action@v1 with: - version: 1 - - uses: julia-actions/cache@v2 - - name: Install JuliaFormatter and format - run: | - using Pkg - Pkg.add(PackageSpec(name="JuliaFormatter", version="1")) - using JuliaFormatter - format("."; verbose=true) - shell: julia --color=yes {0} - - name: Suggest formatting changes - uses: reviewdog/action-suggester@v1 + version: '1' + format_files: true + # Fail on next step instead + continue-on-error: ${{ github.event_name == 'pull_request' }} + - uses: reviewdog/action-suggester@v1 if: github.event_name == 'pull_request' with: - tool_name: JuliaFormatter - fail_on_error: true + tool_name: Runic + fail_level: warning diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..3e2823c --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,5 @@ +repos: + - repo: https://github.com/fredrikekre/runic-pre-commit + rev: v2.0.1 + hooks: + - id: runic diff --git a/docs/make.jl b/docs/make.jl index f72a36d..dc79dac 100755 --- a/docs/make.jl +++ b/docs/make.jl @@ -3,15 +3,17 @@ # if "--help" ∈ ARGS - println(""" - docs/make.jl + println( + """ + docs/make.jl - Render the `AlgorithmsInterface.jl` documentation with optional arguments + Render the `AlgorithmsInterface.jl` documentation with optional arguments - Arguments - * `--help` print this help and exit without rendering the documentation - * `--prettyurls` toggle the pretty urls part to true, which is always set on CI - """) + Arguments + * `--help` print this help and exit without rendering the documentation + * `--prettyurls` toggle the pretty urls part to true, which is always set on CI + """ + ) exit(0) end diff --git a/src/stopping_criterion.jl b/src/stopping_criterion.jl index d2398d6..2470fdf 100644 --- a/src/stopping_criterion.jl +++ b/src/stopping_criterion.jl @@ -62,11 +62,11 @@ If so it returns whether `stopping_criterion` itself indicates convergence, othe since the algorithm has then not yet stopped. """ function indicates_convergence( - stopping_criterion::StoppingCriterion, - stopping_criterion_state::StoppingCriterionState, -) + stopping_criterion::StoppingCriterion, + stopping_criterion_state::StoppingCriterionState, + ) return isnothing(get_reason(stopping_criterion, stopping_criterion_state)) && - indicates_convergence(stopping_criterion) + indicates_convergence(stopping_criterion) end _doc_is_finished = """ @@ -142,7 +142,7 @@ when _all_ indicate to stop. StopWhenAll(c::NTuple{N,StoppingCriterion} where N) StopWhenAll(c::StoppingCriterion,...) """ -struct StopWhenAll{TCriteria<:Tuple} <: StoppingCriterion +struct StopWhenAll{TCriteria <: Tuple} <: StoppingCriterion criteria::TCriteria end StopWhenAll(c::AbstractVector{<:StoppingCriterion}) = StopWhenAll(Tuple(c)) @@ -192,7 +192,7 @@ concatenation of all reasons (assuming that all non-indicating return `""`). StopWhenAny(c::Vector{N,StoppingCriterion} where N) StopWhenAny(c::StoppingCriterion...) """ -struct StopWhenAny{TCriteria<:Tuple} <: StoppingCriterion +struct StopWhenAny{TCriteria <: Tuple} <: StoppingCriterion criteria::TCriteria StopWhenAny(c::Vector{<:StoppingCriterion}) = new{typeof(tuple(c...))}(tuple(c...)) StopWhenAny(c::StoppingCriterion...) = new{typeof(c)}(c) @@ -243,7 +243,7 @@ This is for example used in combination with [`StopWhenAny`](@ref) and [`StopWhe GroupStoppingCriterionState(c::Vector{N,StoppingCriterionState} where N) GroupStoppingCriterionState(c::StoppingCriterionState...) """ -mutable struct GroupStoppingCriterionState{TCriteriaStates<:Tuple} <: StoppingCriterionState +mutable struct GroupStoppingCriterionState{TCriteriaStates <: Tuple} <: StoppingCriterionState criteria_states::TCriteriaStates at_iteration::Int GroupStoppingCriterionState(c::Vector{<:StoppingCriterionState}) = @@ -252,9 +252,9 @@ mutable struct GroupStoppingCriterionState{TCriteriaStates<:Tuple} <: StoppingCr end function get_reason( - stop_when::Union{StopWhenAll,StopWhenAny}, - stopping_criterion_states::GroupStoppingCriterionState, -) + stop_when::Union{StopWhenAll, StopWhenAny}, + stopping_criterion_states::GroupStoppingCriterionState, + ) stopping_criterion_states.at_iteration < 0 && return nothing criteria = stop_when.criteriaq stopping_criterion_states = stopping_criterion_states.criteria_states @@ -262,25 +262,25 @@ function get_reason( end function initialize_state( - problem::Problem, - algorithm::Algorithm, - stop_when::Union{StopWhenAll,StopWhenAny}; - kwargs..., -) + problem::Problem, + algorithm::Algorithm, + stop_when::Union{StopWhenAll, StopWhenAny}; + kwargs..., + ) return GroupStoppingCriterionState( ( initialize_state(problem, algorithm, stopping_criterion; kwargs...) for - stopping_criterion in stop_when.criteria + stopping_criterion in stop_when.criteria )..., ) end function initialize_state!( - stopping_criterion_states::GroupStoppingCriterionState, - problem::Problem, - algorithm::Algorithm, - stop_when::Union{StopWhenAll,StopWhenAny}; - kwargs..., -) + stopping_criterion_states::GroupStoppingCriterionState, + problem::Problem, + algorithm::Algorithm, + stop_when::Union{StopWhenAll, StopWhenAny}; + kwargs..., + ) for (stopping_criterion_state, stopping_criterion) in zip(stopping_criterion_states.criteria_states, stop_when.criteria) initialize_state!( @@ -296,35 +296,35 @@ function initialize_state!( end function is_finished( - problem::Problem, - algorithm::Algorithm, - state::State, - stop_when_all::StopWhenAll, - stopping_criterion_states::GroupStoppingCriterionState, -) + problem::Problem, + algorithm::Algorithm, + state::State, + stop_when_all::StopWhenAll, + stopping_criterion_states::GroupStoppingCriterionState, + ) k = state.iteration (k == 0) && (stopping_criterion_states.at_iteration = -1) # reset on init if all( - st -> is_finished(problem, algorithm, state, st[1], st[2]), - zip(stop_when_all.criteria, stopping_criterion_states.criteria_states), - ) + st -> is_finished(problem, algorithm, state, st[1], st[2]), + zip(stop_when_all.criteria, stopping_criterion_states.criteria_states), + ) return true end return false end function is_finished!( - problem::Problem, - algorithm::Algorithm, - state::State, - stop_when_all::StopWhenAll, - stopping_criterion_states::GroupStoppingCriterionState, -) + problem::Problem, + algorithm::Algorithm, + state::State, + stop_when_all::StopWhenAll, + stopping_criterion_states::GroupStoppingCriterionState, + ) k = state.iteration (k == 0) && (stopping_criterion_states.at_iteration = -1) # reset on init if all( - st -> is_finished!(problem, algorithm, state, st[1], st[2]), - zip(stop_when_all.criteria, stopping_criterion_states.criteria_states), - ) + st -> is_finished!(problem, algorithm, state, st[1], st[2]), + zip(stop_when_all.criteria, stopping_criterion_states.criteria_states), + ) stopping_criterion_states.at_iteration = k return true end @@ -332,35 +332,35 @@ function is_finished!( end function is_finished( - problem::Problem, - algorithm::Algorithm, - state::State, - stop_when_any::StopWhenAny, - stopping_criterion_states::GroupStoppingCriterionState, -) + problem::Problem, + algorithm::Algorithm, + state::State, + stop_when_any::StopWhenAny, + stopping_criterion_states::GroupStoppingCriterionState, + ) k = state.iteration (k == 0) && (stopping_criterion_states.at_iteration = -1) # reset on init if any( - st -> is_finished(problem, algorithm, state, st[1], st[2]), - zip(stop_when_any.criteria, stopping_criterion_states.criteria_states), - ) + st -> is_finished(problem, algorithm, state, st[1], st[2]), + zip(stop_when_any.criteria, stopping_criterion_states.criteria_states), + ) return true end return false end function is_finished!( - problem::Problem, - algorithm::Algorithm, - state::State, - stop_when_any::StopWhenAny, - stopping_criterion_states::GroupStoppingCriterionState, -) + problem::Problem, + algorithm::Algorithm, + state::State, + stop_when_any::StopWhenAny, + stopping_criterion_states::GroupStoppingCriterionState, + ) k = state.iteration (k == 0) && (stopping_criterion_states.at_iteration = -1) # reset on init if any( - st -> is_finished!(problem, algorithm, state, st[1], st[2]), - zip(stop_when_any.criteria, stopping_criterion_states.criteria_states), - ) + st -> is_finished!(problem, algorithm, state, st[1], st[2]), + zip(stop_when_any.criteria, stopping_criterion_states.criteria_states), + ) stopping_criterion_states.at_iteration = k return true end @@ -368,10 +368,10 @@ function is_finished!( end function Base.summary( - io::IO, - stop_when_any::StopWhenAny, - stopping_criterion_states::GroupStoppingCriterionState, -) + io::IO, + stop_when_any::StopWhenAny, + stopping_criterion_states::GroupStoppingCriterionState, + ) has_stopped = (stopping_criterion_states.at_iteration >= 0) s = has_stopped ? "reached" : "not reached" r = "Stop When _one_ of the following are fulfilled:\n" @@ -383,10 +383,10 @@ function Base.summary( return print(io, "$(r)Overall: $s") end function Base.summary( - io::IO, - stop_when_all::StopWhenAll, - stopping_criterion_states::GroupStoppingCriterionState, -) + io::IO, + stop_when_all::StopWhenAll, + stopping_criterion_states::GroupStoppingCriterionState, + ) has_stopped = (stopping_criterion_states.at_iteration >= 0) s = has_stopped ? "reached" : "not reached" r = "Stop When _all_ of the following are fulfilled:\n" @@ -441,33 +441,33 @@ end initialize_state(::Problem, ::Algorithm, ::StopAfterIteration; kwargs...) = DefaultStoppingCriterionState() function initialize_state!( - stopping_criterion_state::DefaultStoppingCriterionState, - ::Problem, - ::Algorithm, - ::StopAfterIteration; - kwargs..., -) + stopping_criterion_state::DefaultStoppingCriterionState, + ::Problem, + ::Algorithm, + ::StopAfterIteration; + kwargs..., + ) stopping_criterion_state.at_iteration = -1 return stopping_criterion_state end function is_finished( - ::Problem, - ::Algorithm, - state::State, - stop_after_iteration::StopAfterIteration, - stopping_criterion_state::DefaultStoppingCriterionState, -) + ::Problem, + ::Algorithm, + state::State, + stop_after_iteration::StopAfterIteration, + stopping_criterion_state::DefaultStoppingCriterionState, + ) return state.iteration >= stop_after_iteration.max_iterations end function is_finished!( - ::Problem, - ::Algorithm, - state::State, - stop_after_iteration::StopAfterIteration, - stopping_criterion_state::DefaultStoppingCriterionState, -) + ::Problem, + ::Algorithm, + state::State, + stop_after_iteration::StopAfterIteration, + stopping_criterion_state::DefaultStoppingCriterionState, + ) k = state.iteration (k == 0) && (stopping_criterion_state.at_iteration = -1) if k >= stop_after_iteration.max_iterations @@ -477,9 +477,9 @@ function is_finished!( return false end function get_reason( - stop_after_iteration::StopAfterIteration, - stopping_criterion_state::DefaultStoppingCriterionState, -) + stop_after_iteration::StopAfterIteration, + stopping_criterion_state::DefaultStoppingCriterionState, + ) if stopping_criterion_state.at_iteration >= stop_after_iteration.max_iterations return "At iteration $(stopping_criterion_state.at_iteration) the algorithm reached its maximal number of iterations ($(stop_after_iteration.max_iterations)).\n" end @@ -487,10 +487,10 @@ function get_reason( end indicates_convergence(stop_after_iteration::StopAfterIteration) = false function Base.summary( - io::IO, - stop_after_iteration::StopAfterIteration, - stopping_criterion_state::DefaultStoppingCriterionState, -) + io::IO, + stop_after_iteration::StopAfterIteration, + stopping_criterion_state::DefaultStoppingCriterionState, + ) has_stopped = (stopping_criterion_state.at_iteration >= 0) s = has_stopped ? "reached" : "not reached" return print(io, "Max Iteration $(stop_after_iteration.max_iterations):\t$s") @@ -550,12 +550,12 @@ initialize_state(::Problem, ::Algorithm, ::StopAfter; kwargs...) = StopAfterTimePeriodState() function initialize_state!( - stopping_criterion_state::DefaultStoppingCriterionState, - ::Problem, - ::Algorithm, - ::StopAfter; - kwargs..., -) + stopping_criterion_state::DefaultStoppingCriterionState, + ::Problem, + ::Algorithm, + ::StopAfter; + kwargs..., + ) stopping_criterion_state.start = Nanosecond(0) stopping_criterion_state.time = Nanosecond(0) stopping_criterion_state.at_iteration = -1 @@ -563,23 +563,23 @@ function initialize_state!( end function is_finished( - ::Problem, - ::Algorithm, - state::State, - stop_after::StopAfter, - stop_after_state::StopAfterTimePeriodState, -) + ::Problem, + ::Algorithm, + state::State, + stop_after::StopAfter, + stop_after_state::StopAfterTimePeriodState, + ) k = state.iteration # Just check whether the (last recorded) time is beyond the threshold return (k > 0 && (stop_after_state.time > Nanosecond(stop_after.threshold))) end function is_finished!( - ::Problem, - ::Algorithm, - state::State, - stop_after::StopAfter, - stop_after_state::StopAfterTimePeriodState, -) + ::Problem, + ::Algorithm, + state::State, + stop_after::StopAfter, + stop_after_state::StopAfterTimePeriodState, + ) k = state.iteration if value(stop_after_state.start) == 0 || k <= 0 # (re)start timer stop_after_state.at_iteration = -1 @@ -595,19 +595,19 @@ function is_finished!( return false end function get_reason( - stop_after::StopAfter, - stopping_criterion_state::StopAfterTimePeriodState, -) + stop_after::StopAfter, + stopping_criterion_state::StopAfterTimePeriodState, + ) if (stopping_criterion_state.at_iteration >= 0) return "After iteration $(stopping_criterion_state.at_iteration) the algorithm ran for $(floor(stopping_criterion_state.time, typeof(stop_after.threshold))) (threshold: $(stop_after.threshold)).\n" end return nothing end function Base.summary( - io::IO, - stop_after::StopAfter, - stopping_criterion_state::StopAfterTimePeriodState, -) + io::IO, + stop_after::StopAfter, + stopping_criterion_state::StopAfterTimePeriodState, + ) has_stopped = (stopping_criterion_state.at_iteration >= 0) s = has_stopped ? "reached" : "not reached" return print(io, "stopped after $(stop_after.threshold):\t$s") diff --git a/test/newton.jl b/test/newton.jl index e83d2da..93ecc0f 100644 --- a/test/newton.jl +++ b/test/newton.jl @@ -30,10 +30,10 @@ function initialize_state(problem::RootFindingProblem, algorithm::NewtonMethod) return NewtonState(0, 1.0, scs) # hardcode initial guess to 1.0 end function initialize_state!( - problem::RootFindingProblem, - algorithm::NewtonMethod, - state::NewtonState, -) + problem::RootFindingProblem, + algorithm::NewtonMethod, + state::NewtonState, + ) state.iteration = 0 state.iterate = 1.0 initialize_state!( diff --git a/test/stopping_criterion.jl b/test/stopping_criterion.jl index a51a414..36d5a37 100644 --- a/test/stopping_criterion.jl +++ b/test/stopping_criterion.jl @@ -6,7 +6,7 @@ struct DummyAlgorithm <: Algorithm stopping_criterion::StoppingCriterion end struct DummyProblem <: Problem end -mutable struct DummyState{S<:StoppingCriterionState} <: State +mutable struct DummyState{S <: StoppingCriterionState} <: State stopping_criterion_state::S iteration::Int end @@ -43,7 +43,7 @@ end s1 = StopAfterIteration(2) & StopAfter(Second(1)) @test s1 isa StoppingCriterion @test sprint((io, x) -> show(io, MIME"text/plain"(), x), s1) == - "StopWhenAll with the Stopping Criteria:\n StopAfterIteration(2)\n StopAfter(Second(1))" + "StopWhenAll with the Stopping Criteria:\n StopAfterIteration(2)\n StopAfter(Second(1))" algorithm = DummyAlgorithm(s1) s1_state = initialize_state(problem, algorithm, s1) @@ -59,7 +59,7 @@ end s1 = StopAfterIteration(2) | StopAfter(Second(1)) @test s1 isa StoppingCriterion @test sprint((io, x) -> show(io, MIME"text/plain"(), x), s1) == - "StopWhenAny with the Stopping Criteria:\n StopAfterIteration(2)\n StopAfter(Second(1))" + "StopWhenAny with the Stopping Criteria:\n StopAfterIteration(2)\n StopAfter(Second(1))" algorithm = DummyAlgorithm(s1) s1_state = initialize_state(problem, algorithm, s1)