Skip to content

Commit 958d418

Browse files
committed
Collect dummy structs in a central place.
1 parent f290420 commit 958d418

4 files changed

Lines changed: 46 additions & 38 deletions

File tree

src/AlgorithmsInterface.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ include("interface/interface.jl")
2020
include("stopping_criterion.jl")
2121
include("logging.jl")
2222

23+
include("test_suite.jl")
24+
2325
# general interface
2426
export Algorithm, Problem, State
2527
export initialize_state, initialize_state!

src/stopping_criterion.jl

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,16 @@ indicates_convergence(stopping_criterion::StoppingCriterion)
5454
@doc """
5555
indicates_convergence(stopping_criterion::StoppingCriterion, ::StoppingCriterionState)
5656
57-
Return whether or not a [`StoppingCriterion`](@ref) indicates convergence when it is in [`StoppingCriterionState`](@ref).
57+
Return whether or not a [`StoppingCriterion`](@ref) indicates convergence when it is in [`StoppingCriterionState`](@ref),
58+
i.e. also check whether the state indicates that the criterion has been active.
5859
59-
By default this checks whether the [`StoppingCriterion`](@ref) has actually stopped.
6060
If so it returns whether `stopping_criterion` itself indicates convergence, otherwise it returns `false`,
6161
since the algorithm has then not yet stopped.
6262
"""
6363
function indicates_convergence(
64-
stopping_criterion::StoppingCriterion,
65-
stopping_criterion_state::StoppingCriterionState,
64+
stopping_criterion::StoppingCriterion, stopping_criterion_state::StoppingCriterionState,
6665
)
67-
return isnothing(get_reason(stopping_criterion, stopping_criterion_state)) &&
68-
indicates_convergence(stopping_criterion)
66+
return isnothing(get_reason(stopping_criterion, stopping_criterion_state)) && indicates_convergence(stopping_criterion)
6967
end
7068

7169
_doc_is_finished = """
@@ -86,11 +84,8 @@ once per iteration, the other one merely inspects the current status without mut
8684
@doc "$(_doc_is_finished)"
8785
function is_finished(problem::Problem, algorithm::Algorithm, state::State)
8886
return is_finished(
89-
problem,
90-
algorithm,
91-
state,
92-
algorithm.stopping_criterion,
93-
state.stopping_criterion_state,
87+
problem, algorithm, state,
88+
algorithm.stopping_criterion, state.stopping_criterion_state,
9489
)
9590
end
9691

@@ -100,11 +95,8 @@ is_finished(::Problem, ::Algorithm, ::State, ::StoppingCriterion, ::StoppingCrit
10095
@doc "$(_doc_is_finished)"
10196
function is_finished!(problem::Problem, algorithm::Algorithm, state::State)
10297
return is_finished!(
103-
problem,
104-
algorithm,
105-
state,
106-
algorithm.stopping_criterion,
107-
state.stopping_criterion_state,
98+
problem, algorithm, state,
99+
algorithm.stopping_criterion, state.stopping_criterion_state,
108100
)
109101
end
110102

@@ -115,7 +107,7 @@ is_finished!(::Problem, ::Algorithm, ::State, ::StoppingCriterion, ::StoppingCri
115107
summary(io::IO, stopping_criterion::StoppingCriterion, stopping_criterion_state::StoppingCriterionState)
116108
117109
Provide a summary of the status of a stopping criterion – its parameters and whether
118-
it currently indicates to stop. It should not be longer than one line
110+
it currently indicates to stop.
119111
120112
# Example
121113

src/test_suite.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""
2+
AlgorithmsInterface.Test
3+
4+
The module `AlgorithmsInterface.Test` contains concrete (dummy) instances
5+
to test prats of the interface.
6+
"""
7+
module Test
8+
using ..AlgorithmsInterface
9+
10+
struct DummyAlgorithm{S<:AlgorithmsInterface.StoppingCriterion} <: AlgorithmsInterface.Algorithm
11+
stopping_criterion::S
12+
end
13+
struct DummyProblem <: AlgorithmsInterface.Problem end
14+
mutable struct DummyState{S <: AlgorithmsInterface.StoppingCriterionState} <: AlgorithmsInterface.State
15+
stopping_criterion_state::S
16+
iteration::Int
17+
end
18+
end

test/stopping_criterion.jl

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
11
using Test
22
using AlgorithmsInterface
3+
using AlgorithmsInterface: Test as AIT
34
using Dates
45

5-
struct DummyAlgorithm <: Algorithm
6-
stopping_criterion::StoppingCriterion
7-
end
8-
struct DummyProblem <: Problem end
9-
mutable struct DummyState{S <: StoppingCriterionState} <: State
10-
stopping_criterion_state::S
11-
iteration::Int
12-
end
13-
14-
problem = DummyProblem()
6+
problem = AIT.DummyProblem()
157

168
@testset "StopAfterIteration" begin
179
s1 = StopAfterIteration(2)
1810
@test s1 isa StoppingCriterion
19-
@test string(s1) == "StopAfterIteration(2)"
20-
21-
algorithm = DummyAlgorithm(s1)
11+
@test repr(s1) == "StopAfterIteration(2)"
12+
@test !indicates_convergence(s1)
13+
algorithm = AIT.DummyAlgorithm(s1)
2214
s1_state = initialize_state(problem, algorithm, s1)
23-
state_finished = DummyState(s1_state, 2)
24-
state_not_finished = DummyState(s1_state, 1)
15+
@test !indicates_convergence(s1, s1_state)
16+
state_finished = AIT.DummyState(s1_state, 2)
17+
state_not_finished = AIT.DummyState(s1_state, 1)
2518
@test is_finished(problem, algorithm, state_finished)
2619
@test !is_finished(problem, algorithm, state_not_finished)
2720
end
@@ -31,28 +24,31 @@ end
3124
@test s1 isa StoppingCriterion
3225
@test string(s1) == "StopAfter(Second(1))"
3326

34-
algorithm = DummyAlgorithm(s1)
27+
algorithm = AIT.DummyAlgorithm(s1)
3528
s1_state = initialize_state(problem, algorithm, s1)
36-
state_not_finished = DummyState(s1_state, 1)
29+
state_not_finished = AIT.DummyState(s1_state, 1)
3730
@test !is_finished(problem, algorithm, state_not_finished)
3831
s1_state.time = Second(2)
3932
@test is_finished(problem, algorithm, state_not_finished)
4033
end
4134

4235
@testset "StopWhenAll" begin
4336
s1 = StopAfterIteration(2) & StopAfter(Second(1))
37+
s1b = StopWhenAll([StopAfterIteration(2), StopAfter(Second(1))])
38+
@test s1 == s1b
4439
@test s1 isa StoppingCriterion
4540
@test sprint((io, x) -> show(io, MIME"text/plain"(), x), s1) ==
4641
"StopWhenAll with the Stopping Criteria:\n StopAfterIteration(2)\n StopAfter(Second(1))"
4742

48-
algorithm = DummyAlgorithm(s1)
43+
algorithm = AIT.DummyAlgorithm(s1)
4944
s1_state = initialize_state(problem, algorithm, s1)
50-
state_not_finished = DummyState(s1_state, 1)
45+
state_not_finished = AIT.DummyState(s1_state, 1)
5146
@test !is_finished(problem, algorithm, state_not_finished)
5247
s1_state.criteria_states[2].time = Second(2)
5348
@test !is_finished(problem, algorithm, state_not_finished)
5449
state_not_finished.iteration = 2
5550
@test is_finished(problem, algorithm, state_not_finished)
51+
@test !indicates_convergence(s1)
5652
end
5753

5854
@testset "StopWhenAny" begin
@@ -61,9 +57,9 @@ end
6157
@test sprint((io, x) -> show(io, MIME"text/plain"(), x), s1) ==
6258
"StopWhenAny with the Stopping Criteria:\n StopAfterIteration(2)\n StopAfter(Second(1))"
6359

64-
algorithm = DummyAlgorithm(s1)
60+
algorithm = AIT.DummyAlgorithm(s1)
6561
s1_state = initialize_state(problem, algorithm, s1)
66-
state_not_finished = DummyState(s1_state, 1)
62+
state_not_finished = AIT.DummyState(s1_state, 1)
6763
@test !is_finished(problem, algorithm, state_not_finished)
6864
s1_state.criteria_states[2].time = Second(2)
6965
@test is_finished(problem, algorithm, state_not_finished)

0 commit comments

Comments
 (0)