@@ -17,6 +17,16 @@ function initialize_state! end
1717@doc " $(_doc_init_state) "
1818initialize_state! (:: Problem , :: Algorithm , :: State ; kwargs... )
1919
20+
21+ """
22+ output = finalize_state!(problem::Problem, algorithm::Algorithm, state::State)
23+
24+ Finalize the solver and decide what values get returned from the [`solve!`](@ref) call.
25+ By default, this is a no-op and returns the `state.iterate`, but this allows for further
26+ customization in other cases, for example to clean up used resources or output other data.
27+ """
28+ finalize_state! (problem:: Problem , algorithm:: Algorithm , state:: State ) = state. iterate
29+
2030# has to be defined before used in solve but is documented alphabetically after
2131
2232@doc """
@@ -30,8 +40,21 @@ returns a state.
3040By default this method continues to call [`solve!`](@ref).
3141"""
3242function solve (problem:: Problem , algorithm:: Algorithm ; kwargs... )
43+ # obtain logger once to minimize overhead from accessing ScopedValue
44+ # additionally handle logging initialization to enable stateful LoggingAction
45+ logger = algorithm_logger ()
46+
47+ # initialize the state and emit message
3348 state = initialize_state (problem, algorithm; kwargs... )
34- return solve! (problem, algorithm, state; kwargs... )
49+ emit_message (logger, problem, algorithm, state, :Start )
50+
51+ # main loop
52+ state = solve_loop! (problem, algorithm, state)
53+
54+ # emit message about finished state
55+ emit_message (logger, problem, algorithm, state, :Stop )
56+
57+ return finalize_state! (problem, algorithm, state)
3558end
3659
3760@doc """
@@ -46,28 +69,39 @@ function solve!(problem::Problem, algorithm::Algorithm, state::State; kwargs...)
4669 # obtain logger once to minimize overhead from accessing ScopedValue
4770 # additionally handle logging initialization to enable stateful LoggingAction
4871 logger = algorithm_logger ()
49- # initialize_logger(logger, problem, algorithm, state)
5072
5173 # initialize the state and emit message
5274 initialize_state! (problem, algorithm, state; kwargs... )
5375 emit_message (logger, problem, algorithm, state, :Start )
5476
55- # main body of the algorithm
77+ # main loop
78+ state = solve_loop! (problem, algorithm, state)
79+
80+ # emit message about finished state
81+ emit_message (logger, problem, algorithm, state, :Stop )
82+
83+ return finalize_state! (problem, algorithm, state)
84+ end
85+
86+ """
87+ solve_loop!(problem::Problem, algorithm::Algorithm, state::State)
88+
89+ Provide the main loop of the iterative `algorithm` for a given `problem` and starting `state`.
90+
91+ This loop consists of:
92+ 1. Checking for convergence with [`is_finished!`](@ref)
93+ 2. Incrementing the state [`increment!`](@ref)
94+ 3. Performing a step [`step!`](@ref)
95+ 4. Repeat
96+ """
97+ function solve_loop! (problem:: Problem , algorithm:: Algorithm , state:: State )
98+ logger = algorithm_logger ()
5699 while ! is_finished! (problem, algorithm, state)
57- # logging event between convergence check and algorithm step
58100 emit_message (logger, problem, algorithm, state, :PreStep )
59-
60- # algorithm step
61101 increment! (state)
62102 step! (problem, algorithm, state)
63-
64- # logging event between algorithm step and convergence check
65103 emit_message (logger, problem, algorithm, state, :PostStep )
66104 end
67-
68- # emit message about finished state
69- emit_message (logger, problem, algorithm, state, :Stop )
70-
71105 return state
72106end
73107
0 commit comments