Skip to content

Commit 0cc91e8

Browse files
Finish up tests
1 parent ac0f630 commit 0cc91e8

File tree

1 file changed

+89
-85
lines changed

1 file changed

+89
-85
lines changed

lib/OptimizationEvolutionary/test/runtests.jl

Lines changed: 89 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -59,105 +59,109 @@ Random.seed!(1234)
5959
haskey(sol.original.trace[end].metadata, "curr_u")
6060

6161
# Test Suite for Different Multi-Objective Functions
62-
function test_multi_objective(func, initial_guess)
63-
# Define the gradient function using ForwardDiff
64-
function gradient_multi_objective(x, p=nothing)
65-
ForwardDiff.jacobian(func, x)
66-
end
62+
function test_multi_objective(func, initial_guess)
63+
# Define the gradient function using ForwardDiff
64+
function gradient_multi_objective(x, p=nothing)
65+
ForwardDiff.jacobian(func, x)
66+
end
6767

68-
# Create an instance of MultiObjectiveOptimizationFunction
69-
obj_func = MultiObjectiveOptimizationFunction(func, jac=gradient_multi_objective)
68+
# Create an instance of MultiObjectiveOptimizationFunction
69+
obj_func = MultiObjectiveOptimizationFunction(func, jac=gradient_multi_objective)
7070

71-
# Set up the evolutionary algorithm (e.g., NSGA2)
72-
algorithm = OptimizationEvolutionary.NSGA2()
71+
# Set up the evolutionary algorithm (e.g., NSGA2)
72+
algorithm = OptimizationEvolutionary.NSGA2()
7373

74-
# Define the optimization problem
75-
problem = OptimizationProblem(obj_func, initial_guess)
74+
# Define the optimization problem
75+
problem = OptimizationProblem(obj_func, initial_guess)
7676

77-
# Solve the optimization problem
78-
result = solve(problem, algorithm)
79-
80-
return result
81-
end
77+
# Solve the optimization problem
78+
result = solve(problem, algorithm)
8279

83-
@testset "Multi-Objective Optimization Tests" begin
80+
return result
81+
end
8482

85-
# Test 1: Sphere and Rastrigin Functions
86-
@testset "Sphere and Rastrigin Functions" begin
87-
function multi_objective_1(x, p=nothing)::Vector{Float64}
88-
f1 = sum(x .^ 2) # Sphere function
89-
f2 = sum(x .^ 2 .- 10 .* cos.(2π .* x) .+ 10) # Rastrigin function
90-
return [f1, f2]
83+
@testset "Multi-Objective Optimization Tests" begin
84+
85+
# Test 1: Sphere and Rastrigin Functions
86+
@testset "Sphere and Rastrigin Functions" begin
87+
function multi_objective_1(x, p=nothing)::Vector{Float64}
88+
f1 = sum(x .^ 2) # Sphere function
89+
f2 = sum(x .^ 2 .- 10 .* cos.(2π .* x) .+ 10) # Rastrigin function
90+
return [f1, f2]
91+
end
92+
result = test_multi_objective(multi_objective_1, [0.0, 1.0])
93+
@test result nothing
94+
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
9199
end
92-
result = test_multi_objective(multi_objective_1, [0.0, 1.0])
93-
@test result nothing
94-
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-
end
99100

100-
# Test 2: Rosenbrock and Ackley Functions
101-
@testset "Rosenbrock and Ackley Functions" begin
102-
function multi_objective_2(x, p=nothing)::Vector{Float64}
103-
f1 = (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2 # Rosenbrock function
104-
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-
return [f1, f2]
101+
# Test 2: Rosenbrock and Ackley Functions
102+
@testset "Rosenbrock and Ackley Functions" begin
103+
function multi_objective_2(x, p=nothing)::Vector{Float64}
104+
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
106+
return [f1, f2]
107+
end
108+
result = test_multi_objective(multi_objective_2, [0.1, 1.0])
109+
@test result nothing
110+
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
106115
end
107-
result = test_multi_objective(multi_objective_2, [1.0, 1.0])
108-
@test result nothing
109-
println("Solution for Rosenbrock and Ackley: ", result)
110-
@test result.u[10][1] 1.0 atol=1e-3
111-
@test result.u[10][2] 0.999739 atol=1e-3
112-
@test result.objective[2] 3.625384 atol=1e-3
113-
end
114116

115-
# Test 3: ZDT1 Function
116-
@testset "ZDT1 Function" begin
117-
function multi_objective_3(x, p=nothing)::Vector{Float64}
118-
f1 = x[1]
119-
g = 1 + 9 * sum(x[2:end]) / (length(x) - 1)
120-
sqrt_arg = f1 / g
121-
f2 = g * (1 - (sqrt_arg >= 0 ? sqrt(sqrt_arg) : NaN))
122-
return [f1, f2]
117+
# Test 3: ZDT1 Function
118+
@testset "ZDT1 Function" begin
119+
function multi_objective_3(x, p=nothing)::Vector{Float64}
120+
f1 = x[1]
121+
g = 1 + 9 * sum(x[2:end]) / (length(x) - 1)
122+
sqrt_arg = f1 / g
123+
f2 = g * (1 - (sqrt_arg >= 0 ? sqrt(sqrt_arg) : NaN))
124+
return [f1, f2]
125+
end
126+
result = test_multi_objective(multi_objective_3, [0.25, 1.5])
127+
@test result nothing
128+
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
132+
@test isnan(result.objective[2])
123133
end
124-
result = test_multi_objective(multi_objective_3, [0.25, 1.5])
125-
@test result nothing
126-
println("Solution for ZDT1: ", result)
127-
@test result.u[1][1] -0.365434 atol=1e-3
128-
@test result.u[1][2] 1.22128 atol=1e-3
129-
@test result.objective[1] -0.365434 atol=1e-3
130-
end
131134

132-
# Test 4: DTLZ2 Function
133-
@testset "DTLZ2 Function" begin
134-
function multi_objective_4(x, p=nothing)::Vector{Float64}
135-
f1 = (1 + sum(x[2:end] .^ 2)) * cos(x[1] * π / 2)
136-
f2 = (1 + sum(x[2:end] .^ 2)) * sin(x[1] * π / 2)
137-
return [f1, f2]
135+
# Test 4: DTLZ2 Function
136+
@testset "DTLZ2 Function" begin
137+
function multi_objective_4(x, p=nothing)::Vector{Float64}
138+
f1 = (1 + sum(x[2:end] .^ 2)) * cos(x[1] * π / 2)
139+
f2 = (1 + sum(x[2:end] .^ 2)) * sin(x[1] * π / 2)
140+
return [f1, f2]
141+
end
142+
result = test_multi_objective(multi_objective_4, [0.25, 0.75])
143+
@test result nothing
144+
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
138149
end
139-
result = test_multi_objective(multi_objective_4, [0.25, 0.75])
140-
@test result nothing
141-
println("Solution for DTLZ2: ", result)
142-
@test result.u[1][1] 0.899183 atol=1e-3
143-
@test result.u[2][1] 0.713992 atol=1e-3
144-
@test result.objective[1] 0.1599915 atol=1e-3
145-
end
146150

147-
# Test 5: Schaffer Function N.2
148-
@testset "Schaffer Function N.2" begin
149-
function multi_objective_5(x, p=nothing)::Vector{Float64}
150-
f1 = x[1]^2
151-
f2 = (x[1] - 2)^2
152-
return [f1, f2]
151+
# Test 5: Schaffer Function N.2
152+
@testset "Schaffer Function N.2" begin
153+
function multi_objective_5(x, p=nothing)::Vector{Float64}
154+
f1 = x[1]^2
155+
f2 = (x[1] - 2)^2
156+
return [f1, f2]
157+
end
158+
result = test_multi_objective(multi_objective_5, [1.0])
159+
@test result nothing
160+
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
153165
end
154-
result = test_multi_objective(multi_objective_5, [1.0])
155-
@test result nothing
156-
println("Solution for Schaffer N.2: ", result)
157-
@test result.u[19][1] 0.252635 atol=1e-3
158-
@test result.u[9][1] 1.0 atol=1e-3
159-
@test result.objective[1] 1.0 atol=1e-3
160166
end
161-
162-
end
163167
end

0 commit comments

Comments
 (0)