Skip to content

Commit 1a3ddb6

Browse files
committed
test(OptimizationBBO): avoid deepcopy in callback
SciML#899 (comment)
1 parent fa88f0a commit 1a3ddb6

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

lib/OptimizationBBO/test/runtests.jl

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,20 @@ using Test
2626
@test 10 * sol.objective < l1
2727

2828
fitness_progress_history = []
29+
fitness_progress_history_orig = []
30+
loss_history = []
2931
function cb(state, fitness)
30-
push!(fitness_progress_history, [deepcopy(state), fitness])
32+
push!(fitness_progress_history, state.objective)
33+
push!(fitness_progress_history_orig, BlackBoxOptim.best_fitness(state.original))
34+
push!(loss_history, fitness)
3135
return false
3236
end
3337
sol = solve(prob, BBO_adaptive_de_rand_1_bin_radiuslimited(), callback = cb)
3438
# println(fitness_progress_history)
3539
@test !isempty(fitness_progress_history)
3640
fp1 = fitness_progress_history[1]
37-
@test BlackBoxOptim.best_fitness(fp1[1].original) == fp1[1].objective == fp1[2]
41+
fp2 = fitness_progress_history_orig[1]
42+
@test fp2 == fp1 == loss_history[1]
3843

3944
@test_logs begin
4045
(Base.LogLevel(-1), "loss: 0.0")
@@ -87,19 +92,23 @@ using Test
8792
end
8893

8994
fitness_progress_history = []
95+
fitness_progress_history_orig = []
9096
function cb(state, fitness)
91-
push!(fitness_progress_history, deepcopy(state))
97+
push!(fitness_progress_history, state.objective)
98+
push!(fitness_progress_history_orig, BlackBoxOptim.best_fitness(state.original))
9299
return false
93100
end
94101

95102
mof_1 = MultiObjectiveOptimizationFunction(multi_obj_func_1)
96103
prob_1 = Optimization.OptimizationProblem(mof_1, u0; lb = lb, ub = ub)
97104
sol_1 = solve(prob_1, opt, NumDimensions = 2,
98-
FitnessScheme = ParetoFitnessScheme{2}(is_minimizing = true), callback=cb)
105+
FitnessScheme = ParetoFitnessScheme{2}(is_minimizing = true),
106+
callback=cb)
99107

100108
fp1 = fitness_progress_history[1]
101-
@test BlackBoxOptim.best_fitness(fp1.original).orig == fp1.objective
102-
@test length(fp1.objective) == 2
109+
fp2 = fitness_progress_history_orig[1]
110+
@test fp2.orig == fp1
111+
@test length(fp1) == 2
103112

104113
@test sol_1 nothing
105114
println("Solution for Sphere and Rastrigin: ", sol_1)

0 commit comments

Comments
 (0)