Skip to content

Commit

Permalink
Finish up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaibhavdixit02 committed Aug 18, 2024
1 parent ac0f630 commit 0cc91e8
Showing 1 changed file with 89 additions and 85 deletions.
174 changes: 89 additions & 85 deletions lib/OptimizationEvolutionary/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,105 +59,109 @@ Random.seed!(1234)
haskey(sol.original.trace[end].metadata, "curr_u")

# Test Suite for Different Multi-Objective Functions
function test_multi_objective(func, initial_guess)
# Define the gradient function using ForwardDiff
function gradient_multi_objective(x, p=nothing)
ForwardDiff.jacobian(func, x)
end
function test_multi_objective(func, initial_guess)
# Define the gradient function using ForwardDiff
function gradient_multi_objective(x, p=nothing)
ForwardDiff.jacobian(func, x)
end

# Create an instance of MultiObjectiveOptimizationFunction
obj_func = MultiObjectiveOptimizationFunction(func, jac=gradient_multi_objective)
# Create an instance of MultiObjectiveOptimizationFunction
obj_func = MultiObjectiveOptimizationFunction(func, jac=gradient_multi_objective)

# Set up the evolutionary algorithm (e.g., NSGA2)
algorithm = OptimizationEvolutionary.NSGA2()
# Set up the evolutionary algorithm (e.g., NSGA2)
algorithm = OptimizationEvolutionary.NSGA2()

# Define the optimization problem
problem = OptimizationProblem(obj_func, initial_guess)
# Define the optimization problem
problem = OptimizationProblem(obj_func, initial_guess)

# Solve the optimization problem
result = solve(problem, algorithm)

return result
end
# Solve the optimization problem
result = solve(problem, algorithm)

@testset "Multi-Objective Optimization Tests" begin
return result
end

# Test 1: Sphere and Rastrigin Functions
@testset "Sphere and Rastrigin Functions" begin
function multi_objective_1(x, p=nothing)::Vector{Float64}
f1 = sum(x .^ 2) # Sphere function
f2 = sum(x .^ 2 .- 10 .* cos.(2π .* x) .+ 10) # Rastrigin function
return [f1, f2]
@testset "Multi-Objective Optimization Tests" begin

# Test 1: Sphere and Rastrigin Functions
@testset "Sphere and Rastrigin Functions" begin
function multi_objective_1(x, p=nothing)::Vector{Float64}
f1 = sum(x .^ 2) # Sphere function
f2 = sum(x .^ 2 .- 10 .* cos.(2π .* x) .+ 10) # Rastrigin function
return [f1, f2]
end
result = test_multi_objective(multi_objective_1, [0.0, 1.0])
@test result nothing
println("Solution for Sphere and Rastrigin: ", result)
@test result.u[1][1] 7.88866e-5 atol=1e-3
@test result.u[1][2] 4.96471e-5 atol=1e-3
@test result.objective[1] 8.6879e-9 atol=1e-3
@test result.objective[2] 1.48875349381683e-6 atol=1e-3
end
result = test_multi_objective(multi_objective_1, [0.0, 1.0])
@test result nothing
println("Solution for Sphere and Rastrigin: ", result)
@test result.u[1][1] 7.88866e-5 atol=1e-3
@test result.u[1][2] 4.96471e-5 atol=1e-3
@test result.objective[1] 8.6879e-9 atol=1e-3
end

# Test 2: Rosenbrock and Ackley Functions
@testset "Rosenbrock and Ackley Functions" begin
function multi_objective_2(x, p=nothing)::Vector{Float64}
f1 = (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2 # Rosenbrock function
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
return [f1, f2]
# Test 2: Rosenbrock and Ackley Functions
@testset "Rosenbrock and Ackley Functions" begin
function multi_objective_2(x, p=nothing)::Vector{Float64}
f1 = (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2 # Rosenbrock function
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
return [f1, f2]
end
result = test_multi_objective(multi_objective_2, [0.1, 1.0])
@test result nothing
println("Solution for Rosenbrock and Ackley: ", result)
@test result.u[1][1] 0.003993274873103834 atol=1e-3
@test result.u[1][2] 0.001433311246712721 atol=1e-3
@test result.objective[1] 0.9922302888530358 atol=1e-3
@test result.objective[2] 0.012479470703588902 atol=1e-3
end
result = test_multi_objective(multi_objective_2, [1.0, 1.0])
@test result nothing
println("Solution for Rosenbrock and Ackley: ", result)
@test result.u[10][1] 1.0 atol=1e-3
@test result.u[10][2] 0.999739 atol=1e-3
@test result.objective[2] 3.625384 atol=1e-3
end

# Test 3: ZDT1 Function
@testset "ZDT1 Function" begin
function multi_objective_3(x, p=nothing)::Vector{Float64}
f1 = x[1]
g = 1 + 9 * sum(x[2:end]) / (length(x) - 1)
sqrt_arg = f1 / g
f2 = g * (1 - (sqrt_arg >= 0 ? sqrt(sqrt_arg) : NaN))
return [f1, f2]
# Test 3: ZDT1 Function
@testset "ZDT1 Function" begin
function multi_objective_3(x, p=nothing)::Vector{Float64}
f1 = x[1]
g = 1 + 9 * sum(x[2:end]) / (length(x) - 1)
sqrt_arg = f1 / g
f2 = g * (1 - (sqrt_arg >= 0 ? sqrt(sqrt_arg) : NaN))
return [f1, f2]
end
result = test_multi_objective(multi_objective_3, [0.25, 1.5])
@test result nothing
println("Solution for ZDT1: ", result)
@test result.u[1][1] -0.365434 atol=1e-3
@test result.u[1][2] 1.22128 atol=1e-3
@test result.objective[1] -0.365434 atol=1e-3
@test isnan(result.objective[2])
end
result = test_multi_objective(multi_objective_3, [0.25, 1.5])
@test result nothing
println("Solution for ZDT1: ", result)
@test result.u[1][1] -0.365434 atol=1e-3
@test result.u[1][2] 1.22128 atol=1e-3
@test result.objective[1] -0.365434 atol=1e-3
end

# Test 4: DTLZ2 Function
@testset "DTLZ2 Function" begin
function multi_objective_4(x, p=nothing)::Vector{Float64}
f1 = (1 + sum(x[2:end] .^ 2)) * cos(x[1] * π / 2)
f2 = (1 + sum(x[2:end] .^ 2)) * sin(x[1] * π / 2)
return [f1, f2]
# Test 4: DTLZ2 Function
@testset "DTLZ2 Function" begin
function multi_objective_4(x, p=nothing)::Vector{Float64}
f1 = (1 + sum(x[2:end] .^ 2)) * cos(x[1] * π / 2)
f2 = (1 + sum(x[2:end] .^ 2)) * sin(x[1] * π / 2)
return [f1, f2]
end
result = test_multi_objective(multi_objective_4, [0.25, 0.75])
@test result nothing
println("Solution for DTLZ2: ", result)
@test result.u[1][1] 0.899183 atol=1e-3
@test result.u[2][1] 0.713992 atol=1e-3
@test result.objective[1] 0.1599915 atol=1e-3
@test result.objective[2] 1.001824893932647 atol=1e-3
end
result = test_multi_objective(multi_objective_4, [0.25, 0.75])
@test result nothing
println("Solution for DTLZ2: ", result)
@test result.u[1][1] 0.899183 atol=1e-3
@test result.u[2][1] 0.713992 atol=1e-3
@test result.objective[1] 0.1599915 atol=1e-3
end

# Test 5: Schaffer Function N.2
@testset "Schaffer Function N.2" begin
function multi_objective_5(x, p=nothing)::Vector{Float64}
f1 = x[1]^2
f2 = (x[1] - 2)^2
return [f1, f2]
# Test 5: Schaffer Function N.2
@testset "Schaffer Function N.2" begin
function multi_objective_5(x, p=nothing)::Vector{Float64}
f1 = x[1]^2
f2 = (x[1] - 2)^2
return [f1, f2]
end
result = test_multi_objective(multi_objective_5, [1.0])
@test result nothing
println("Solution for Schaffer N.2: ", result)
@test result.u[19][1] 0.252635 atol=1e-3
@test result.u[9][1] 1.0 atol=1e-3
@test result.objective[1] 1.0 atol=1e-3
@test result.objective[2] 1.0 atol=1e-3
end
result = test_multi_objective(multi_objective_5, [1.0])
@test result nothing
println("Solution for Schaffer N.2: ", result)
@test result.u[19][1] 0.252635 atol=1e-3
@test result.u[9][1] 1.0 atol=1e-3
@test result.objective[1] 1.0 atol=1e-3
end

end
end

0 comments on commit 0cc91e8

Please sign in to comment.