@@ -34,6 +34,50 @@ using Test
3434 push! (loss_history, fitness)
3535 return false
3636 end
37+
38+ @testset " In-place Multi-Objective Optimization" begin
39+ function inplace_multi_obj! (cost, x, p)
40+ cost[1 ] = sum (x .^ 2 )
41+ cost[2 ] = sum (x .^ 2 .- 10 .* cos .(2 π .* x) .+ 10 )
42+ return nothing
43+ end
44+ u0 = [0.25 , 0.25 ]
45+ lb = [0.0 , 0.0 ]
46+ ub = [2.0 , 2.0 ]
47+ cost_prototype = zeros (2 )
48+ mof_inplace = MultiObjectiveOptimizationFunction (inplace_multi_obj!; cost_prototype= cost_prototype)
49+ prob_inplace = Optimization. OptimizationProblem (mof_inplace, u0; lb= lb, ub= ub)
50+ sol_inplace = solve (prob_inplace, opt, NumDimensions= 2 , FitnessScheme= ParetoFitnessScheme {2} (is_minimizing= true ))
51+ @test sol_inplace ≠ nothing
52+ @test length (sol_inplace. objective) == 2
53+ @test sol_inplace. objective[1 ] ≈ 6.9905986e-18 atol= 1e-3
54+ @test sol_inplace. objective[2 ] ≈ 1.7763568e-15 atol= 1e-3
55+ end
56+
57+ @testset " Custom coalesce for Multi-Objective" begin
58+ function multi_obj_tuple (x, p)
59+ f1 = sum (x .^ 2 )
60+ f2 = sum (x .^ 2 .- 10 .* cos .(2 π .* x) .+ 10 )
61+ return (f1, f2)
62+ end
63+ coalesce_sum (cost, x, p) = sum (cost)
64+ mof_coalesce = MultiObjectiveOptimizationFunction (multi_obj_tuple; coalesce= coalesce_sum)
65+ prob_coalesce = Optimization. OptimizationProblem (mof_coalesce, u0; lb= lb, ub= ub)
66+ sol_coalesce = solve (prob_coalesce, opt, NumDimensions= 2 , FitnessScheme= ParetoFitnessScheme {2} (is_minimizing= true ))
67+ @test sol_coalesce ≠ nothing
68+ @test sol_coalesce. objective[1 ] ≈ 6.9905986e-18 atol= 1e-3
69+ @test sol_coalesce. objective[2 ] ≈ 1.7763568e-15 atol= 1e-3
70+ @test mof_coalesce. coalesce ([1.0 , 2.0 ], [0.0 , 0.0 ], nothing ) == 3.0
71+ end
72+
73+ @testset " Error if in-place MultiObjectiveOptimizationFunction without cost_prototype" begin
74+ function inplace_multi_obj_err! (cost, x, p)
75+ cost[1 ] = sum (x .^ 2 )
76+ cost[2 ] = sum (x .^ 2 .- 10 .* cos .(2 π .* x) .+ 10 )
77+ return nothing
78+ end
79+ @test_throws ArgumentError MultiObjectiveOptimizationFunction (inplace_multi_obj_err!)
80+ end
3781 sol = solve (prob, BBO_adaptive_de_rand_1_bin_radiuslimited (), callback = cb)
3882 # println(fitness_progress_history)
3983 @test ! isempty (fitness_progress_history)
0 commit comments