104104
105105abstract type AlgorithmIterator end
106106
107- struct DefaultAlgorithmIterator{Problem, Algorithm, State} <: AlgorithmIterator
108- problem:: Problem
109- algorithm:: Algorithm
110- state:: State
111- end
112-
113107function algorithm_iterator (
114108 problem:: Problem , algorithm:: Algorithm , state:: State
115109 )
@@ -135,6 +129,12 @@ function Base.iterate(iterator::AlgorithmIterator, init = nothing)
135129 return iterator. state, nothing
136130end
137131
132+ struct DefaultAlgorithmIterator{Problem, Algorithm, State} <: AlgorithmIterator
133+ problem:: Problem
134+ algorithm:: Algorithm
135+ state:: State
136+ end
137+
138138#= =========================== with_algorithmlogger ========================================#
139139
140140# Allow passing functions, not just CallbackActions.
@@ -147,12 +147,16 @@ end
147147
148148#= =========================== NestedAlgorithm =============================================#
149149
150- abstract type AbstractNestedAlgorithm <: Algorithm end
150+ abstract type NestedAlgorithm <: Algorithm end
151+
152+ function nested_algorithm (f:: Function , nalgorithms:: Int ; kwargs... )
153+ return DefaultNestedAlgorithm (f, nalgorithms; kwargs... )
154+ end
151155
152- max_iterations (algorithm:: AbstractNestedAlgorithm ) = length (algorithm. algorithms)
156+ max_iterations (algorithm:: NestedAlgorithm ) = length (algorithm. algorithms)
153157
154158function AI. step! (
155- problem:: AI.Problem , algorithm:: AbstractNestedAlgorithm , state:: AI.State ;
159+ problem:: AI.Problem , algorithm:: NestedAlgorithm , state:: AI.State ;
156160 logging_context_prefix = Symbol ()
157161 )
158162 # Perform the current sweep.
@@ -167,45 +171,30 @@ function AI.step!(
167171end
168172
169173#=
170- NestedAlgorithm (sweeps::AbstractVector{<:Algorithm})
174+ DefaultNestedAlgorithm (sweeps::AbstractVector{<:Algorithm})
171175
172176An algorithm that consists of running an algorithm at each iteration
173177from a list of stored algorithms.
174178=#
175- @kwdef struct NestedAlgorithm {
179+ @kwdef struct DefaultNestedAlgorithm {
176180 Algorithms <: AbstractVector{<:Algorithm} ,
177181 StoppingCriterion <: AI.StoppingCriterion ,
178- } <: AbstractNestedAlgorithm
182+ } <: NestedAlgorithm
179183 algorithms:: Algorithms
180184 stopping_criterion:: StoppingCriterion = AI. StopAfterIteration (length (algorithms))
181185end
182- function NestedAlgorithm (f:: Function , nalgorithms:: Int ; kwargs... )
183- return NestedAlgorithm (; algorithms = f .(1 : nalgorithms), kwargs... )
186+ function DefaultNestedAlgorithm (f:: Function , nalgorithms:: Int ; kwargs... )
187+ return DefaultNestedAlgorithm (; algorithms = f .(1 : nalgorithms), kwargs... )
184188end
185189
186190#= =========================== FlattenedAlgorithm ==========================================#
187191
188192# Flatten a nested algorithm.
189- @kwdef struct FlattenedAlgorithm{
190- Algorithms <: AbstractVector{<:Algorithm} ,
191- StoppingCriterion <: AI.StoppingCriterion ,
192- } <: Algorithm
193- algorithms:: Algorithms
194- stopping_criterion:: StoppingCriterion =
195- AI. StopAfterIteration (sum (max_iterations, algorithms))
196- end
197- function FlattenedAlgorithm (f:: Function , nalgorithms:: Int ; kwargs... )
198- return FlattenedAlgorithm (; algorithms = f .(1 : nalgorithms), kwargs... )
199- end
193+ abstract type FlattenedAlgorithm <: Algorithm end
194+ abstract type FlattenedAlgorithmState <: State end
200195
201- @kwdef mutable struct FlattenedAlgorithmState{
202- Iterate, StoppingCriterionState <: AI.StoppingCriterionState ,
203- } <: State
204- iterate:: Iterate
205- iteration:: Int = 0
206- parent_iteration:: Int = 1
207- child_iteration:: Int = 0
208- stopping_criterion_state:: StoppingCriterionState
196+ function flattened_algorithm (f:: Function , nalgorithms:: Int ; kwargs... )
197+ return DefaultFlattenedAlgorithm (f, nalgorithms; kwargs... )
209198end
210199
211200function AI. initialize_state (
@@ -214,7 +203,7 @@ function AI.initialize_state(
214203 stopping_criterion_state = AI. initialize_state (
215204 problem, algorithm, algorithm. stopping_criterion
216205 )
217- return FlattenedAlgorithmState (; stopping_criterion_state, kwargs... )
206+ return DefaultFlattenedAlgorithmState (; stopping_criterion_state, kwargs... )
218207end
219208function AI. increment! (
220209 problem:: Problem , algorithm:: Algorithm , state:: FlattenedAlgorithmState
@@ -247,4 +236,26 @@ function AI.step!(
247236 return state
248237end
249238
239+ @kwdef struct DefaultFlattenedAlgorithm{
240+ Algorithms <: AbstractVector{<:Algorithm} ,
241+ StoppingCriterion <: AI.StoppingCriterion ,
242+ } <: FlattenedAlgorithm
243+ algorithms:: Algorithms
244+ stopping_criterion:: StoppingCriterion =
245+ AI. StopAfterIteration (sum (max_iterations, algorithms))
246+ end
247+ function DefaultFlattenedAlgorithm (f:: Function , nalgorithms:: Int ; kwargs... )
248+ return DefaultFlattenedAlgorithm (; algorithms = f .(1 : nalgorithms), kwargs... )
249+ end
250+
251+ @kwdef mutable struct DefaultFlattenedAlgorithmState{
252+ Iterate, StoppingCriterionState <: AI.StoppingCriterionState ,
253+ } <: FlattenedAlgorithmState
254+ iterate:: Iterate
255+ iteration:: Int = 0
256+ parent_iteration:: Int = 1
257+ child_iteration:: Int = 0
258+ stopping_criterion_state:: StoppingCriterionState
259+ end
260+
250261end
0 commit comments