Skip to content

Commit 2995c3d

Browse files
Merge pull request #236 from prbzrg/iss-224
Add end of iteration check in callback of Optim
2 parents bee4c50 + 471e6da commit 2995c3d

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

lib/GalacticOptimJL/src/GalacticOptimJL.jl

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ function ___solve(prob::OptimizationProblem, opt::Optim.AbstractOptimizer,
6565
reltol::Union{Number,Nothing}=nothing,
6666
progress=false,
6767
kwargs...)
68+
6869
local x, cur, state
6970

7071
if data != GalacticOptim.DEFAULT_DATA
@@ -78,8 +79,13 @@ function ___solve(prob::OptimizationProblem, opt::Optim.AbstractOptimizer,
7879
if !(typeof(cb_call) <: Bool)
7980
error("The callback should return a boolean `halt` for whether to stop the optimization process.")
8081
end
81-
cur, state = iterate(data, state)
82-
cb_call
82+
nx_itr = iterate(data, state)
83+
if isnothing(nx_itr)
84+
true
85+
else
86+
cur, state = nx_itr
87+
cb_call
88+
end
8389
end
8490

8591
maxiters = GalacticOptim._check_and_convert_maxiters(maxiters)
@@ -163,8 +169,13 @@ function ___solve(prob::OptimizationProblem, opt::Union{Optim.Fminbox,Optim.SAMI
163169
if !(typeof(cb_call) <: Bool)
164170
error("The callback should return a boolean `halt` for whether to stop the optimization process.")
165171
end
166-
cur, state = iterate(data, state)
167-
cb_call
172+
nx_itr = iterate(data, state)
173+
if isnothing(nx_itr)
174+
true
175+
else
176+
cur, state = nx_itr
177+
cb_call
178+
end
168179
end
169180

170181
maxiters = GalacticOptim._check_and_convert_maxiters(maxiters)
@@ -231,8 +242,13 @@ function ___solve(prob::OptimizationProblem, opt::Optim.ConstrainedOptimizer,
231242
if !(typeof(cb_call) <: Bool)
232243
error("The callback should return a boolean `halt` for whether to stop the optimization process.")
233244
end
234-
cur, state = iterate(data, state)
235-
cb_call
245+
nx_itr = iterate(data, state)
246+
if isnothing(nx_itr)
247+
true
248+
else
249+
cur, state = nx_itr
250+
cb_call
251+
end
236252
end
237253

238254
maxiters = GalacticOptim._check_and_convert_maxiters(maxiters)

0 commit comments

Comments
 (0)