@@ -27,7 +27,7 @@ function Sweep(
2727 return Sweep (regions, Returns (region_kwargs), stopping_criterion)
2828end
2929
30- maxiter (algorithm:: Sweep ) = length (algorithm. regions)
30+ AIE . max_iterations (algorithm:: Sweep ) = length (algorithm. regions)
3131
3232function AI. step! (
3333 problem:: AI.Problem , algorithm:: Sweep , state:: AI.State ; kwargs...
@@ -56,95 +56,3 @@ function insert!(
5656 # Insert step goes here.
5757 return state
5858end
59-
60- #=
61- Sweeping(sweeps::Vector{<:Sweep})
62-
63- The sweeping algorithm, which just stores a list of sweeps defined above.
64- =#
65- @kwdef struct Sweeping{
66- Sweeps <: Vector{<:Sweep} , StoppingCriterion <: AI.StoppingCriterion ,
67- } <: AIE.Algorithm
68- sweeps:: Sweeps
69- stopping_criterion:: StoppingCriterion = AI. StopAfterIteration (length (sweeps))
70- end
71- function Sweeping (f:: Function , nsweeps:: Int ; kwargs... )
72- return Sweeping (; sweeps = f .(1 : nsweeps), kwargs... )
73- end
74-
75- maxiter (algorithm:: Sweeping ) = length (algorithm. sweeps)
76- nregions (algorithm:: Sweeping ) = sum (maxiter, algorithm. sweeps)
77-
78- function AI. step! (
79- problem:: AI.Problem , algorithm:: Sweeping , state:: AI.State ;
80- logging_context_prefix = Symbol ()
81- )
82- # Perform the current sweep.
83- algorithm_sweep = algorithm. sweeps[state. iteration]
84- state_sweep = AI. initialize_state (problem, algorithm_sweep; state. iterate)
85- logging_context_prefix = Symbol (logging_context_prefix, :Sweep_ )
86- AI. solve! (problem, algorithm_sweep, state_sweep; logging_context_prefix)
87- state. iterate = state_sweep. iterate
88- return state
89- end
90-
91- # TODO : Use a proper stopping criterion.
92- function AI. is_finished (
93- problem:: AI.Problem , algorithm:: Sweeping , state:: AI.State
94- )
95- state. iteration == 0 && return false
96- return state. iteration >= length (algorithm. sweeps)
97- end
98-
99- # Sweeping by region.
100- @kwdef struct ByRegion{
101- ParentAlgorithm <: Sweeping , StoppingCriterion <: AI.StoppingCriterion ,
102- } <: AIE.Algorithm
103- sweeping:: ParentAlgorithm
104- stopping_criterion:: StoppingCriterion = AI. StopAfterIteration (nregions (sweeping))
105- end
106-
107- @kwdef mutable struct ByRegionState{
108- Iterate, StoppingCriterionState <: AI.StoppingCriterionState ,
109- } <: AIE.State
110- iterate:: Iterate
111- iteration:: Int = 0
112- sweeping_iteration:: Int = 1
113- sweep_iteration:: Int = 0
114- stopping_criterion_state:: StoppingCriterionState
115- end
116-
117- function AI. initialize_state (
118- problem:: AIE.Problem , algorithm:: ByRegion ; kwargs...
119- )
120- stopping_criterion_state = AI. initialize_state (
121- problem, algorithm, algorithm. stopping_criterion
122- )
123- return ByRegionState (; stopping_criterion_state, kwargs... )
124- end
125- function AI. increment! (problem:: AIE.Problem , algorithm:: AIE.Algorithm , state:: ByRegionState )
126- # Increment the total iteration count.
127- state. iteration += 1
128- if state. sweep_iteration ≥ maxiter (algorithm. sweeping. sweeps[state. sweeping_iteration])
129- # We're on the last region of the sweep, so move to the next sweep.
130- state. sweeping_iteration += 1
131- state. sweep_iteration = 1
132- else
133- # Move to the next region in the current sweep.
134- state. sweep_iteration += 1
135- end
136- return state
137- end
138- function AI. step! (
139- problem:: AI.Problem , algorithm:: ByRegion , state:: ByRegionState ;
140- logging_context_prefix = Symbol ()
141- )
142- algorithm_sweep = algorithm. sweeping. sweeps[state. sweeping_iteration]
143- state_sweep = AI. initialize_state (
144- problem, algorithm_sweep;
145- state. iterate, iteration = state. sweep_iteration
146- )
147- AI. step! (problem, algorithm_sweep, state_sweep; logging_context_prefix)
148- state. iterate = state_sweep. iterate
149- return state
150- end
0 commit comments