Skip to content

Commit 53b6278

Browse files
committed
test(OptimizationBBO): test that the callback reports the correct objective
1 parent 26eb743 commit 53b6278

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

lib/OptimizationBBO/test/runtests.jl

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ using Test
2727

2828
fitness_progress_history = []
2929
function cb(state, fitness)
30-
push!(fitness_progress_history, [state.u, fitness])
30+
push!(fitness_progress_history, [deepcopy(state), fitness])
3131
return false
3232
end
3333
sol = solve(prob, BBO_adaptive_de_rand_1_bin_radiuslimited(), callback = cb)
3434
# println(fitness_progress_history)
3535
@test !isempty(fitness_progress_history)
36+
fp1 = fitness_progress_history[1]
37+
@test BlackBoxOptim.best_fitness(fp1[1].original) == fp1[1].objective == fp1[2]
3638

3739
@test_logs begin
3840
(Base.LogLevel(-1), "loss: 0.0")
@@ -77,6 +79,34 @@ using Test
7779
@test sol_1.objective[2]1.7763568e-15 atol=1e-3
7880
end
7981

82+
@testset "Sphere and Rastrigin Functions with callback" begin
83+
function multi_obj_func_1(x, p)
84+
f1 = sum(x .^ 2) # Sphere function
85+
f2 = sum(x .^ 2 .- 10 .* cos.(2π .* x) .+ 10) # Rastrigin function
86+
return (f1, f2)
87+
end
88+
89+
fitness_progress_history = []
90+
function cb(state, fitness)
91+
push!(fitness_progress_history, deepcopy(state))
92+
return false
93+
end
94+
95+
mof_1 = MultiObjectiveOptimizationFunction(multi_obj_func_1)
96+
prob_1 = Optimization.OptimizationProblem(mof_1, u0; lb = lb, ub = ub)
97+
sol_1 = solve(prob_1, opt, NumDimensions = 2,
98+
FitnessScheme = ParetoFitnessScheme{2}(is_minimizing = true), callback=cb)
99+
100+
fp1 = fitness_progress_history[1]
101+
@test BlackBoxOptim.best_fitness(fp1.original).orig == fp1.objective
102+
@test length(fp1.objective) == 2
103+
104+
@test sol_1 nothing
105+
println("Solution for Sphere and Rastrigin: ", sol_1)
106+
@test sol_1.objective[1]6.9905986e-18 atol=1e-3
107+
@test sol_1.objective[2]1.7763568e-15 atol=1e-3
108+
end
109+
80110
# Test 2: Rosenbrock and Ackley Functions
81111
@testset "Rosenbrock and Ackley Functions" begin
82112
function multi_obj_func_2(x, p)

0 commit comments

Comments
 (0)