Skip to content

Commit fcdc02f

Browse files
committed
Towards the first plain interface.
1 parent 555eecb commit fcdc02f

8 files changed

Lines changed: 156 additions & 29 deletions

File tree

docs/make.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ makedocs(;
4040
modules = [AlgorithmsInterface],
4141
authors = "Ronny Bergmann, Lukas Devos, and contributors.",
4242
sitename = "AlgorithmsInterface.jl",
43-
pages = ["Home" => "index.md", "References" => "references.md"],
43+
pages = [
44+
"Home" => "index.md",
45+
"Interface" => "interface.md",
46+
"Stopping criteria" => "stopping_criterion.md",
47+
"References" => "references.md",
48+
],
4449
plugins = [bib, links],
4550
)
4651
deploydocs(;

docs/src/index.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
# IterativeAlgorithms.jl
1+
# AlgorithmsInterface.jl
2+
3+
Welcome to the Documentation of `LieGAlgorithmsInterface.jl`.
4+
5+
```@meta
6+
CurrentModule = AlgorithmsInterface
7+
```
28

39
```@docs
4-
Algorithm
5-
Problem
6-
State
7-
step!
8-
get_iteration
10+
AlgorithmsInterface.AlgorithmsInterface
911
```

docs/src/interface.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Main Interface types
2+
3+
```@autodocs
4+
Modules = [AlgorithmsInterface]
5+
Pages = ["interface/interface.jl"]
6+
Order = [:type, :function]
7+
Private = true
8+
```
9+
10+
## Algorithm
11+
12+
```@autodocs
13+
Modules = [AlgorithmsInterface]
14+
Pages = ["interface/algorithm.jl"]
15+
Order = [:type, :function]
16+
Private = true
17+
```
18+
19+
## Problem
20+
21+
```@autodocs
22+
Modules = [AlgorithmsInterface]
23+
Pages = ["interface/problem.jl"]
24+
Order = [:type, :function]
25+
Private = true
26+
```
27+
28+
## State
29+
30+
```@autodocs
31+
Modules = [AlgorithmsInterface]
32+
Pages = ["interface/state.jl"]
33+
Order = [:type, :function]
34+
Private = true
35+
```

docs/src/stopping_criterion.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Stopping criterion
2+
3+
```@autodocs
4+
Modules = [AlgorithmsInterface]
5+
Pages = ["stopping_criterion.jl"]
6+
Order = [:type, :function]
7+
Private = true
8+
```

src/AlgorithmsInterface.jl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
@doc raw"""
2+
🏔️ AlgorithmsInterface.jl: an interface for iterative algorithms in Julia
3+
4+
* 📚 Documentation: [juliamanifolds.github.io/AlgorithmsInterface.jl/](https://juliamanifolds.github.io/AlgorithmsInterface.jl/)
5+
* 📦 Repository: [github.com/JuliaManifolds/AlgorithmsInterface.jl](https://github.com/JuliaManifolds/AlgorithmsInterface.jl)
6+
* 💬 Discussions: [github.com/JuliaManifolds/AlgorithmsInterface.jl/discussions](https://github.com/JuliaManifolds/AlgorithmsInterface.jl/discussions)
7+
* 🎯 Issues: [github.com/JuliaManifolds/AlgorithmsInterface.jl/issues](https://github.com/JuliaManifolds/AlgorithmsInterface.jl/issues)
8+
"""
19
module AlgorithmsInterface
210

311
using Dates: Millisecond, Nanosecond, Period, canonicalize, value
@@ -10,7 +18,11 @@ include("interface/interface.jl")
1018
include("stopping_criterion.jl")
1119

1220
export Algorithm, Problem, State
21+
export StoppingCriterion
22+
export StopAfter, StopAfterIteration
23+
export is_finished
24+
export initialize_state, initialize_state!, is_finished
1325
export get_iteration
14-
export step!
26+
export step!, solve, solve!
1527

1628
end # module AlgorithmsInterface

src/interface/interface.jl

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,31 @@
1-
function step! end
2-
@doc """
3-
step!(p::Problem, a::Algorithm, s::State)
1+
_doc_init_state = """
2+
s = initialize_state(p::Problem, a::Algorithm; kwargs...)
3+
initialize_state!(p::Problem, a::Algorithm, s::State; kwargs...)
44
5-
Perform the current step of an [`Algorithm`](@ref) `a` solving [`Problem`](@ref) `p`
6-
starting from [`State`](@ref) `s`.
5+
Initialize a [`State`](@ref) `s` base on a [`Problem`](@ref) `p` and an [`Algorithm`](@ref).
6+
The `kwargs...` should allow to initialize for example the initial point.
7+
This can be done in-place of `s`, then only values that did change have to be provided.
78
"""
8-
step!(p::Problem, a::Algorithm, s::State)
99

10+
function initialize_state end
11+
12+
@doc "$(_doc_init_state)"
13+
initialize_state(::Problem, ::Algorithm; kwargs...)
14+
15+
function initialize_state! end
16+
17+
@doc "$(_doc_init_state)"
18+
initialize_state!(::State, ::Problem, ::Algorithm; kwargs...)
19+
20+
@doc """
21+
is_finished(p::Problem, a::Algorithm, s::State)
22+
"""
23+
function is_finished(p::Problem, a::Algorithm, s::State)
24+
sc = get_stopping_criterion(s)
25+
return sc(p, a, s)
26+
end
27+
28+
# has to be defined before used in solve but is documented alphabetically after
1029

1130
@doc """
1231
solve(p::Problem, a::Algorithm; kwargs...)
@@ -19,14 +38,31 @@ returns a state.
1938
By default this method continues to call [`solve!`](@ref).
2039
"""
2140
function solve(p::Problem, a::Algorithm; kwargs...)
22-
s = initialize_state(p, a)
23-
return solve!(p, a, s)
41+
s = initialize_state(p, a; kwargs...)
42+
return solve!(p, a, s; kwargs...)
2443
end
2544

2645
@doc """
27-
solve!(p::Problem, a::Algorithm, s::State)
46+
solve!(p::Problem, a::Algorithm, s::State; kwargs...)
2847
2948
Solve the [`Problem`](@ref) `p` using the [`Algorithm`](@ref) `a`
3049
working on the [`State`](@ref).
50+
51+
All keyword arguments are passed to the [`initialize_state!`](@ref) function.
52+
"""
53+
function solve!(p::Problem, a::Algorithm, s::State; kwargs...)
54+
initialize_state!(p, a, s; kwargs...)
55+
while !is_finished(p, a, s)
56+
increment!(s)
57+
step!(p, a, s)
58+
end
59+
end
60+
61+
function step! end
62+
@doc """
63+
step!(p::Problem, a::Algorithm, s::State)
64+
65+
Perform the current step of an [`Algorithm`](@ref) `a` solving [`Problem`](@ref) `p`
66+
starting from [`State`](@ref) `s`.
3167
"""
32-
solve!(p::Problem, a::Algorithm, s::State)
68+
step!(p::Problem, a::Algorithm, s::State)

src/interface/state.jl

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,51 @@ Usually this should include
1616
These variable names given in this list are the defaults for which the accessors are implemented,
1717
such that if your concrete `MyState <: State` follows this convention, you do not have to reimplement
1818
their accessors.
19+
20+
# Methods
21+
The following methods should be implemented for a state
22+
* [`get_iteration`](@ref)`(s)` to return the current iteration number
23+
* [`increment!](@ref)`(s)`
24+
* [`get_stopping_criterion`](@ref) return the [`StoppingCriterion`](@ref)
25+
* [`get_iterate`](@ref) return the current iterate ``x^{(k)}``.
1926
"""
2027
abstract type State end
2128

29+
"""
30+
get_iterate(s::State)
31+
32+
Return the current iterate ``x^{(k)}`` of a [`State`](@ref) `s`
33+
34+
The default assumes that the current iteration is stored in `s.iterate`.
35+
"""
36+
get_iterate(s::State) = s.iterate
37+
2238
"""
2339
get_iteration(s::State)
2440
25-
Return the current iteration a state either is currently performing or was last performed
41+
Return the current iteration a [`State`](@ref) `s` either is currently performing or was last performed
2642
2743
The default assumes that the current iteration is stored in `s.iteration`.
2844
"""
2945
get_iteration(s::State) = s.iteration
3046

31-
function initialize_state end
32-
@doc """
33-
initialize_state(p::Problem, a::Algorithm, kwargs...)
47+
"""
48+
increment!(s::State)
49+
50+
Return the current iteration a [`State`](@ref) `s` either is currently performing or was last performed
51+
52+
The default assumes that the current iteration is stored in `s.iteration`.
53+
"""
54+
function increment!(s::State)
55+
s.iteration += 1
56+
return s
57+
end
58+
59+
"""
60+
get_stopping_criterion(s::State)
61+
62+
Return the [`StoppingCriterion`](@ref) of the [`State`](@ref) `s`.
3463
35-
Initialise a [`State`](@ref) `s` for a [`Problem`](@ref) `p` and an [`Algorithm`](@ref) `a`,
36-
where the keyword arguments should both uniquely identify the state and provide enough
37-
information to call its constructor.
64+
The default assumes that the criterion is stored in `s.stopping_criterion`.
3865
"""
39-
initialize_state(::Problem, ::Algorithm, kwargs...)
66+
get_stopping_criterion(s::State) = s.stopping_criterion

src/stopping_criterion.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
An abstract type to represent a stopping criterion of an solver.
55
66
Any concrete stopping criterion should be implemented as a functor,
7-
that takes the “usual tuple” `(p, a, s)` of a [`Problem`](@ref),
8-
an [`algorithm`](@ref) and a [`State`](@ref) as input
7+
that takes the “usual tuple” `(p, a, s)` of a [`Problem`](@ref) `p`,
8+
an [`Algorithm`](@ref) and a [`State`](@ref) as input, where this criterion
9+
should usually be part of the [`State`](@ref) itself.
910
1011
## Methods
12+
1113
A concrete `StoppingCriterion` `sc` should provide the following functions
1214
besides the above-mentioned functor is it itself
1315
@@ -57,7 +59,7 @@ For the [`StopAfterIteration`](@ref) criterion, the summary looks like
5759
Max Iterations (15): not reached
5860
```
5961
"""
60-
get_summary(sc::StopAfterIteration)
62+
get_summary(sc::StoppingCriterion)
6163

6264
@doc raw"""
6365
StopAfterIteration <: StoppingCriterion

0 commit comments

Comments
 (0)