Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/archive.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ function delta_fitness(a::TopListArchive)
end

function check_stop_condition(a::TopListArchive, p::OptimizationProblem, ctrl)
if ctrl.max_steps_without_progress > 0 &&
ctrl.num_steps_without_progress > ctrl.max_steps_without_progress
return "No progress for more than $(ctrl.max_steps_without_progress) iterations"
end

if delta_fitness(a) < ctrl.min_delta_fitness_tol
return "Delta fitness ($(delta_fitness(a))) below tolerance ($(ctrl.min_delta_fitness_tol))"
end
Expand Down
17 changes: 16 additions & 1 deletion src/opt_controller.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ mutable struct OptRunController{O<:Optimizer, E<:Evaluator}

last_num_fevals::Int # the number of function evals on the previous step
num_steps_without_fevals::Int # the number of steps without the function evals
num_steps_without_progress::Int # the number of steps without improving best fitness

start_time::Float64 # time optimization started, 0 if not running yet
stop_time::Float64 # time optimization stopped, 0 if still running
Expand Down Expand Up @@ -102,7 +103,7 @@ function OptRunController(optimizer::O, evaluator::E, params) where {O<:Optimize
:CallbackFunction, :CallbackInterval,
:MaxSteps, :MaxFuncEvals, :MaxNumStepsWithoutFuncEvals, :MaxStepsWithoutProgress, :MaxTime,
:MinDeltaFitnessTolerance, :FitnessTolerance]]...,
0, 0, 0, 0, 0, 0, 0.0, 0.0, 0.0, -1.0, "", false)
0, 0, 0, 0, 0, 0, 0, 0.0, 0.0, 0.0, -1.0, "", false)
end

# stepping optimizer has it's own evaluator, get a reference
Expand Down Expand Up @@ -311,13 +312,15 @@ function run!(ctrl::OptRunController)

ctrl.start_time = time()
ctrl.num_steps = 0
ctrl.num_steps_without_progress = 0
while isempty(ctrl.stop_reason)
# Report on progress every now and then...
if (time() - ctrl.last_report_time) > ctrl.trace_interval
trace_progress(ctrl)
end

# Take the step and then update the counters
best_fitness_before_step = best_fitness(ctrl)
nstep_better = step!(ctrl)
ctrl.num_better += nstep_better
ctrl.num_better_since_last_report += nstep_better
Expand All @@ -329,6 +332,18 @@ function run!(ctrl::OptRunController)
ctrl.num_steps_without_fevals = 0
end
ctrl.last_num_fevals = num_func_evals(ctrl)
fit_scheme = fitness_scheme(ctrl.evaluator.archive)
best_fitness_after_step = best_fitness(ctrl)
has_progress = if isnafitness(best_fitness_before_step, fit_scheme)
!isnafitness(best_fitness_after_step, fit_scheme)
else
is_better(best_fitness_after_step, best_fitness_before_step, fit_scheme)
end
if has_progress
ctrl.num_steps_without_progress = 0
else
ctrl.num_steps_without_progress += 1
end

# Callback every now and then (if a callback interval has been set)...
if ctrl.callback_interval >= 0.0
Expand Down
18 changes: 17 additions & 1 deletion test/test_max_func_evals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,20 @@ res = bboptimize(myfit(c, rosenbrock2d); SearchRange = (-5.0, 5.0), NumDimension
@test BlackBoxOptim.stop_reason(res) == "Max number of function evaluations ($MFE) reached"
@test MFE <= c.count <= (MFE+20) # Must be at least 10 but can be somewhat higher since might be several evaluations per step of the optimizer

end
end

@testset "MaxStepsWithoutProgress (single objective)" begin
flat_objective(x) = 1.0
max_steps_without_progress = 3

res = bboptimize(flat_objective;
SearchRange = (-5.0, 5.0),
NumDimensions = 2,
Method = :random_search,
MaxSteps = 10_000,
MaxStepsWithoutProgress = max_steps_without_progress,
TraceMode = :silent)

@test BlackBoxOptim.stop_reason(res) == "No progress for more than $(max_steps_without_progress) iterations"
@test BlackBoxOptim.iterations(res) == max_steps_without_progress + 2
end