@@ -61,12 +61,12 @@ Random.seed!(1234)
6161 # Test Suite for Different Multi-Objective Functions
6262 function test_multi_objective (func, initial_guess)
6363 # Define the gradient function using ForwardDiff
64- function gradient_multi_objective (x, p= nothing )
64+ function gradient_multi_objective (x, p = nothing )
6565 ForwardDiff. jacobian (func, x)
6666 end
6767
6868 # Create an instance of MultiObjectiveOptimizationFunction
69- obj_func = MultiObjectiveOptimizationFunction (func, jac= gradient_multi_objective)
69+ obj_func = MultiObjectiveOptimizationFunction (func, jac = gradient_multi_objective)
7070
7171 # Set up the evolutionary algorithm (e.g., NSGA2)
7272 algorithm = OptimizationEvolutionary. NSGA2 ()
@@ -84,39 +84,40 @@ Random.seed!(1234)
8484
8585 # Test 1: Sphere and Rastrigin Functions
8686 @testset " Sphere and Rastrigin Functions" begin
87- function multi_objective_1 (x, p= nothing ):: Vector{Float64}
87+ function multi_objective_1 (x, p = nothing ):: Vector{Float64}
8888 f1 = sum (x .^ 2 ) # Sphere function
8989 f2 = sum (x .^ 2 .- 10 .* cos .(2 π .* x) .+ 10 ) # Rastrigin function
9090 return [f1, f2]
9191 end
9292 result = test_multi_objective (multi_objective_1, [0.0 , 1.0 ])
9393 @test result ≠ nothing
9494 println (" Solution for Sphere and Rastrigin: " , result)
95- @test result. u[1 ][1 ] ≈ 7.88866e-5 atol= 1e-3
96- @test result. u[1 ][2 ] ≈ 4.96471e-5 atol= 1e-3
97- @test result. objective[1 ] ≈ 8.6879e-9 atol= 1e-3
98- @test result. objective[2 ] ≈ 1.48875349381683e-6 atol= 1e-3
95+ @test result. u[1 ][1 ]≈ 7.88866e-5 atol= 1e-3
96+ @test result. u[1 ][2 ]≈ 4.96471e-5 atol= 1e-3
97+ @test result. objective[1 ]≈ 8.6879e-9 atol= 1e-3
98+ @test result. objective[2 ]≈ 1.48875349381683e-6 atol= 1e-3
9999 end
100100
101101 # Test 2: Rosenbrock and Ackley Functions
102102 @testset " Rosenbrock and Ackley Functions" begin
103- function multi_objective_2 (x, p= nothing ):: Vector{Float64}
103+ function multi_objective_2 (x, p = nothing ):: Vector{Float64}
104104 f1 = (1.0 - x[1 ])^ 2 + 100.0 * (x[2 ] - x[1 ]^ 2 )^ 2 # Rosenbrock function
105- f2 = - 20.0 * exp (- 0.2 * sqrt (0.5 * (x[1 ]^ 2 + x[2 ]^ 2 ))) - exp (0.5 * (cos (2 π * x[1 ]) + cos (2 π * x[2 ]))) + exp (1 ) + 20.0 # Ackley function
105+ f2 = - 20.0 * exp (- 0.2 * sqrt (0.5 * (x[1 ]^ 2 + x[2 ]^ 2 ))) -
106+ exp (0.5 * (cos (2 π * x[1 ]) + cos (2 π * x[2 ]))) + exp (1 ) + 20.0 # Ackley function
106107 return [f1, f2]
107108 end
108109 result = test_multi_objective (multi_objective_2, [0.1 , 1.0 ])
109110 @test result ≠ nothing
110111 println (" Solution for Rosenbrock and Ackley: " , result)
111- @test result. u[1 ][1 ] ≈ 0.003993274873103834 atol= 1e-3
112- @test result. u[1 ][2 ] ≈ 0.001433311246712721 atol= 1e-3
113- @test result. objective[1 ] ≈ 0.9922302888530358 atol= 1e-3
114- @test result. objective[2 ] ≈ 0.012479470703588902 atol= 1e-3
112+ @test result. u[1 ][1 ]≈ 0.003993274873103834 atol= 1e-3
113+ @test result. u[1 ][2 ]≈ 0.001433311246712721 atol= 1e-3
114+ @test result. objective[1 ]≈ 0.9922302888530358 atol= 1e-3
115+ @test result. objective[2 ]≈ 0.012479470703588902 atol= 1e-3
115116 end
116117
117118 # Test 3: ZDT1 Function
118119 @testset " ZDT1 Function" begin
119- function multi_objective_3 (x, p= nothing ):: Vector{Float64}
120+ function multi_objective_3 (x, p = nothing ):: Vector{Float64}
120121 f1 = x[1 ]
121122 g = 1 + 9 * sum (x[2 : end ]) / (length (x) - 1 )
122123 sqrt_arg = f1 / g
@@ -126,42 +127,42 @@ Random.seed!(1234)
126127 result = test_multi_objective (multi_objective_3, [0.25 , 1.5 ])
127128 @test result ≠ nothing
128129 println (" Solution for ZDT1: " , result)
129- @test result. u[1 ][1 ] ≈ - 0.365434 atol= 1e-3
130- @test result. u[1 ][2 ] ≈ 1.22128 atol= 1e-3
131- @test result. objective[1 ] ≈ - 0.365434 atol= 1e-3
130+ @test result. u[1 ][1 ]≈ - 0.365434 atol= 1e-3
131+ @test result. u[1 ][2 ]≈ 1.22128 atol= 1e-3
132+ @test result. objective[1 ]≈ - 0.365434 atol= 1e-3
132133 @test isnan (result. objective[2 ])
133134 end
134135
135136 # Test 4: DTLZ2 Function
136137 @testset " DTLZ2 Function" begin
137- function multi_objective_4 (x, p= nothing ):: Vector{Float64}
138+ function multi_objective_4 (x, p = nothing ):: Vector{Float64}
138139 f1 = (1 + sum (x[2 : end ] .^ 2 )) * cos (x[1 ] * π / 2 )
139140 f2 = (1 + sum (x[2 : end ] .^ 2 )) * sin (x[1 ] * π / 2 )
140141 return [f1, f2]
141142 end
142143 result = test_multi_objective (multi_objective_4, [0.25 , 0.75 ])
143144 @test result ≠ nothing
144145 println (" Solution for DTLZ2: " , result)
145- @test result. u[1 ][1 ] ≈ 0.899183 atol= 1e-3
146- @test result. u[2 ][1 ] ≈ 0.713992 atol= 1e-3
147- @test result. objective[1 ] ≈ 0.1599915 atol= 1e-3
148- @test result. objective[2 ] ≈ 1.001824893932647 atol= 1e-3
146+ @test result. u[1 ][1 ]≈ 0.899183 atol= 1e-3
147+ @test result. u[2 ][1 ]≈ 0.713992 atol= 1e-3
148+ @test result. objective[1 ]≈ 0.1599915 atol= 1e-3
149+ @test result. objective[2 ]≈ 1.001824893932647 atol= 1e-3
149150 end
150151
151152 # Test 5: Schaffer Function N.2
152153 @testset " Schaffer Function N.2" begin
153- function multi_objective_5 (x, p= nothing ):: Vector{Float64}
154+ function multi_objective_5 (x, p = nothing ):: Vector{Float64}
154155 f1 = x[1 ]^ 2
155156 f2 = (x[1 ] - 2 )^ 2
156157 return [f1, f2]
157158 end
158159 result = test_multi_objective (multi_objective_5, [1.0 ])
159160 @test result ≠ nothing
160161 println (" Solution for Schaffer N.2: " , result)
161- @test result. u[19 ][1 ] ≈ 0.252635 atol= 1e-3
162- @test result. u[9 ][1 ] ≈ 1.0 atol= 1e-3
163- @test result. objective[1 ] ≈ 1.0 atol= 1e-3
164- @test result. objective[2 ] ≈ 1.0 atol= 1e-3
162+ @test result. u[19 ][1 ]≈ 0.252635 atol= 1e-3
163+ @test result. u[9 ][1 ]≈ 1.0 atol= 1e-3
164+ @test result. objective[1 ]≈ 1.0 atol= 1e-3
165+ @test result. objective[2 ]≈ 1.0 atol= 1e-3
165166 end
166167 end
167168end
0 commit comments