Skip to content

Commit 3ec120d

Browse files
committed
refactor to use solve_loop!
1 parent cef7482 commit 3ec120d

2 files changed

Lines changed: 31 additions & 18 deletions

File tree

src/AlgorithmsInterface.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export Algorithm, Problem, State
2525
export initialize_state, initialize_state!
2626
export finalize_state!
2727

28-
export step!, solve, solve!
28+
export step!, solve, solve!, solve_loop!
2929

3030
# stopping criteria
3131
export StoppingCriterion, StoppingCriterionState

src/interface/interface.jl

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,19 @@ function solve(problem::Problem, algorithm::Algorithm; kwargs...)
4343
# obtain logger once to minimize overhead from accessing ScopedValue
4444
# additionally handle logging initialization to enable stateful LoggingAction
4545
logger = algorithm_logger()
46-
# initialize_logger(logger, problem, algorithm, state)
4746

4847
# initialize the state and emit message
4948
state = initialize_state(problem, algorithm; kwargs...)
5049
emit_message(logger, problem, algorithm, state, :Start)
5150

52-
return _solve_body!(problem, algorithm, state, logger)
51+
# main loop
52+
state = solve_loop!(problem, algorithm, state)
53+
54+
# emit message about finished state
55+
output = finalize_state!(problem, algorithm, state)
56+
emit_message(logger, problem, algorithm, state, :Stop)
57+
58+
return output
5359
end
5460

5561
@doc """
@@ -64,34 +70,41 @@ function solve!(problem::Problem, algorithm::Algorithm, state::State; kwargs...)
6470
# obtain logger once to minimize overhead from accessing ScopedValue
6571
# additionally handle logging initialization to enable stateful LoggingAction
6672
logger = algorithm_logger()
67-
# initialize_logger(logger, problem, algorithm, state)
6873

6974
# initialize the state and emit message
7075
initialize_state!(problem, algorithm, state; kwargs...)
7176
emit_message(logger, problem, algorithm, state, :Start)
7277

73-
return _solve_body!(problem, algorithm, state, logger)
78+
# main loop
79+
state = solve_loop!(problem, algorithm, state)
80+
81+
# emit message about finished state
82+
output = finalize_state!(problem, algorithm, state)
83+
emit_message(logger, problem, algorithm, state, :Stop)
84+
85+
return output
7486
end
7587

76-
function _solve_body!(problem::Problem, algorithm::Algorithm, state::State, logger)
77-
# main body of the algorithm
88+
"""
89+
solve_loop!(problem::Problem, algorithm::Algorithm, state::State)
90+
91+
Provide the main loop of the iterative `algorithm` for a given `problem` and starting `state`.
92+
93+
This loop consists of:
94+
1. Checking for convergence with [`is_finished!`](@ref)
95+
2. Incrementing the state [`increment!`](@ref)
96+
3. Performing a step [`step!`](@ref)
97+
4. Repeat
98+
"""
99+
function solve_loop!(problem::Problem, algorithm::Algorithm, state::State)
100+
logger = algorithm_logger()
78101
while !is_finished!(problem, algorithm, state)
79-
# logging event between convergence check and algorithm step
80102
emit_message(logger, problem, algorithm, state, :PreStep)
81-
82-
# algorithm step
83103
increment!(state)
84104
step!(problem, algorithm, state)
85-
86-
# logging event between algorithm step and convergence check
87105
emit_message(logger, problem, algorithm, state, :PostStep)
88106
end
89-
90-
# emit message about finished state
91-
output = finalize_state!(problem, algorithm, state)
92-
emit_message(logger, problem, algorithm, state, :Stop)
93-
94-
return output
107+
return state
95108
end
96109

97110
function step! end

0 commit comments

Comments
 (0)