Skip to content

Commit c2db111

Browse files
committed
Replace get_ with getproperty
1 parent 8b18564 commit c2db111

4 files changed

Lines changed: 20 additions & 68 deletions

File tree

src/interface/algorithm.jl

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,16 @@ A concrete algorithm contains all static parameters that characterise the algori
77
Together with a [`Problem`](@ref) an `Algorithm` subtype should be able to initialize
88
or reset a [`State`](@ref).
99
10-
## Usual fields
10+
## Properties
1111
12-
Usually this should include the following. If you use this naming scheme, default functions
13-
e.g. as accessors
12+
Algorithms can contain any number of properties that are needed to define the algorithm,
13+
but should additionally contain the following properties to interact with the stopping criteria.
1414
15-
* `stopping_criterion` a [`StoppingCriterion`](@ref)
16-
17-
## Methods
18-
19-
The following methods should be implemented for an [`Algorithm`](@ref)
20-
21-
* [`get_stopping_criterion`](@ref) to return the algorithms stopping criterion.
15+
* `stopping_criterion::StoppingCriterion`
2216
2317
## Example
2418
2519
For a [gradient descent](https://en.wikipedia.org/wiki/Gradient_descent) algorithm
2620
the algorithm would specify which step size selection to use.
2721
"""
2822
abstract type Algorithm end
29-
30-
"""
31-
get_stopping_criterion(algorithm::Algorithm)
32-
33-
Return the [`StoppingCriterion`](@ref) of the [`Algorithm`](@ref).
34-
35-
The default assumes that the criterion is stored in `algorithm.stopping_criterion`.
36-
"""
37-
get_stopping_criterion(algorithm::Algorithm) = algorithm.stopping_criterion

src/interface/problem.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ An abstract type to represent a problem to be solved with all its static propert
55
not change during an algorithm run.
66
77
## Example
8+
89
For a [gradient descent](https://en.wikipedia.org/wiki/Gradient_descent) algorithm the problem consists of
910
1011
* a `cost` function ``f: C → ℝ``

src/interface/state.jl

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,24 @@ An abstract type to represent the state an iterative algorithm is in.
66
The state consists of any information that describes the current step the algorithm is in
77
and keeps all information needed from one step to the next.
88
9-
## Usual fields
9+
## Properties
1010
11-
Usually this should include the following. If you use this naming scheme, default functions
12-
e.g. as accessors
11+
In order to interact with the stopping criteria, the state should contain the following properties,
12+
and provide corresponding `getproperty` and `setproperty!` methods.
1313
1414
* `iteration` – the current iteration step ``k`` that is is currently performed or was last performed
1515
* `stopping_criterion_state` – a [`StoppingCriterionState`](@ref) that indicates whether an [`Algorithm`](@ref)
1616
will stop after this iteration or has stopped.
1717
* `iterate` the current iterate ``x^{(k)}```.
1818
19-
These variable names given in this list are the defaults for which the accessors are implemented,
20-
such that if your concrete `MyState <: State` follows this convention, you do not have to reimplement
21-
their accessors.
22-
2319
## Methods
2420
2521
The following methods should be implemented for a state
2622
27-
* [`get_iteration`](@ref)`(state)` to return the current iteration number
2823
* [`increment!](@ref)`(state)`
29-
* [`get_stopping_criterion_state`](@ref) return the [`StoppingCriterionState`](@ref)
30-
* [`get_iterate`](@ref) return the current iterate ``x^{(k)}``.
3124
"""
3225
abstract type State end
3326

34-
"""
35-
get_iterate(state::State)
36-
37-
Return the current iterate ``x^{(k)}`` of a [`State`](@ref).
38-
39-
The default assumes that the current iterate is stored in `state.iterate`.
40-
"""
41-
get_iterate(state::State) = state.iterate
42-
43-
"""
44-
get_iteration(state::State)
45-
46-
Return the current iteration a [`State`](@ref) either is currently performing or was last performed
47-
48-
The default assumes that the current iteration is stored in `state.iteration`.
49-
"""
50-
get_iteration(state::State) = state.iteration
51-
5227
"""
5328
increment!(state::State)
5429
@@ -60,12 +35,3 @@ function increment!(state::State)
6035
state.iteration += 1
6136
return state
6237
end
63-
64-
"""
65-
get_stopping_criterion_state(state::State)
66-
67-
Return the [`StoppingCriterionState`](@ref) of the given [`State`](@ref).
68-
69-
The default assumes that the criterion is stored in `state.stopping_criterion_state`.
70-
"""
71-
get_stopping_criterion_state(state::State) = state.stopping_criterion_state

src/stopping_criterion.jl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ function is_finished(problem::Problem, algorithm::Algorithm, state::State)
9090
problem,
9191
algorithm,
9292
state,
93-
get_stopping_criterion(algorithm),
94-
get_stopping_criterion_state(state),
93+
algorithm.stopping_criterion,
94+
state.stopping_criterion_state,
9595
)
9696
end
9797

@@ -104,8 +104,8 @@ function is_finished!(problem::Problem, algorithm::Algorithm, state::State)
104104
problem,
105105
algorithm,
106106
state,
107-
get_stopping_criterion(algorithm),
108-
get_stopping_criterion_state(state),
107+
algorithm.stopping_criterion,
108+
state.stopping_criterion_state,
109109
)
110110
end
111111

@@ -303,7 +303,7 @@ function is_finished(
303303
stop_when_all::StopWhenAll,
304304
stopping_criterion_states::GroupStoppingCriterionState,
305305
)
306-
k = get_iteration(state)
306+
k = state.iteration
307307
(k == 0) && (stopping_criterion_states.at_iteration = -1) # reset on init
308308
if all(
309309
st -> is_finished(problem, algorithm, state, st[1], st[2]),
@@ -320,7 +320,7 @@ function is_finished!(
320320
stop_when_all::StopWhenAll,
321321
stopping_criterion_states::GroupStoppingCriterionState,
322322
)
323-
k = get_iteration(state)
323+
k = state.iteration
324324
(k == 0) && (stopping_criterion_states.at_iteration = -1) # reset on init
325325
if all(
326326
st -> is_finished!(problem, algorithm, state, st[1], st[2]),
@@ -339,7 +339,7 @@ function is_finished(
339339
stop_when_any::StopWhenAny,
340340
stopping_criterion_states::GroupStoppingCriterionState,
341341
)
342-
k = get_iteration(state)
342+
k = state.iteration
343343
(k == 0) && (stopping_criterion_states.at_iteration = -1) # reset on init
344344
if any(
345345
st -> is_finished(problem, algorithm, state, st[1], st[2]),
@@ -356,7 +356,7 @@ function is_finished!(
356356
stop_when_any::StopWhenAny,
357357
stopping_criterion_states::GroupStoppingCriterionState,
358358
)
359-
k = get_iteration(state)
359+
k = state.iteration
360360
(k == 0) && (stopping_criterion_states.at_iteration = -1) # reset on init
361361
if all(
362362
st -> is_finished!(problem, algorithm, state, st[1], st[2]),
@@ -460,7 +460,7 @@ function is_finished(
460460
stop_after_iteration::StopAfterIteration,
461461
stopping_criterion_state::DefaultStoppingCriterionState,
462462
)
463-
return get_iteration(state) >= stop_after_iteration.max_iterations
463+
return state.iteration >= stop_after_iteration.max_iterations
464464
end
465465
function is_finished!(
466466
::Problem,
@@ -469,7 +469,7 @@ function is_finished!(
469469
stop_after_iteration::StopAfterIteration,
470470
stopping_criterion_state::DefaultStoppingCriterionState,
471471
)
472-
k = get_iteration(state)
472+
k = state.iteration
473473
(k == 0) && (stopping_criterion_state.at_iteration = -1)
474474
if k >= stop_after_iteration.max_iterations
475475
stopping_criterion_state.at_iteration = k
@@ -570,7 +570,7 @@ function is_finished(
570570
stop_after::StopAfter,
571571
stop_after_state::StopAfterTimePeriodState,
572572
)
573-
k = get_iteration(state)
573+
k = state.iteration
574574
# Just check whether the (last recorded) time is beyond the threshold
575575
return (k > 0 && (stop_after_state.time > Nanosecond(stop_after.threshold)))
576576
end
@@ -581,7 +581,7 @@ function is_finished!(
581581
stop_after::StopAfter,
582582
stop_after_state::StopAfterTimePeriodState,
583583
)
584-
k = get_iteration(state)
584+
k = state.iteration
585585
if value(stop_after_state.start) == 0 || k <= 0 # (re)start timer
586586
stop_after_state.at_iteration = -1
587587
stop_after_state.start = Nanosecond(time_ns())

0 commit comments

Comments
 (0)