Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaibhavdixit02 committed Aug 19, 2024
1 parent c6f9e90 commit 3c8295d
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 101 deletions.
2 changes: 0 additions & 2 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,7 @@ to add the specific wrapper packages.
- Unconstrained: ✅
</details>
🟡 = supported in downstream library but not yet implemented in `Optimization.jl`; PR to add this functionality are welcome

## Citation

```
@software{vaibhav_kumar_dixit_2023_7738525,
author = {Vaibhav Kumar Dixit and Christopher Rackauckas},
Expand Down
9 changes: 5 additions & 4 deletions docs/src/tutorials/remakecomposition.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ end
optf = OptimizationFunction(f_schwefel, Optimization.AutoReverseDiff(compile = true))
x0 = ones(10).*200.0
prob = OptimizationProblem(optf, x0, [418.9829], lb = fill(-500.0, 10), ub = fill(500.0, 10))
x0 = ones(10) .* 200.0
prob = OptimizationProblem(
optf, x0, [418.9829], lb = fill(-500.0, 10), ub = fill(500.0, 10))
@show f_schwefel(x0)
```

Our polyalgorithm strategy will to use BlackBoxOptim's global optimizers for efficient exploration of the
parameter space followed by a quasi-Newton LBFGS method to (hopefully) converge to the global
Our polyalgorithm strategy will to use BlackBoxOptim's global optimizers for efficient exploration of the
parameter space followed by a quasi-Newton LBFGS method to (hopefully) converge to the global
optimum.

```@example polyalg
Expand Down
2 changes: 1 addition & 1 deletion lib/OptimizationBBO/src/OptimizationBBO.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ for j in string.(BlackBoxOptim.SingleObjectiveMethodNames)
end

Base.@kwdef struct BBO_borg_moea <: BBO
method = :borg_moea
method = :borg_moea
end
export BBO_borg_moea

Expand Down
114 changes: 59 additions & 55 deletions lib/OptimizationBBO/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,68 +49,72 @@ using Test
end

# Define the initial guess and bounds
u0 = [0.25, 0.25]
lb = [0.0, 0.0]
ub = [2.0, 2.0]
u0 = [0.25, 0.25]
lb = [0.0, 0.0]
ub = [2.0, 2.0]

# Define the optimizer
opt = OptimizationBBO.BBO_borg_moea()
# Define the optimizer
opt = OptimizationBBO.BBO_borg_moea()

@testset "Multi-Objective Optimization Tests" begin
@testset "Multi-Objective Optimization Tests" begin

# Test 1: Sphere and Rastrigin Functions
@testset "Sphere and Rastrigin Functions" begin
function multi_obj_func_1(x, p)
f1 = sum(x .^ 2) # Sphere function
f2 = sum(x .^ 2 .- 10 .* cos.(2π .* x) .+ 10) # Rastrigin function
return (f1, f2)
# Test 1: Sphere and Rastrigin Functions
@testset "Sphere and Rastrigin Functions" begin
function multi_obj_func_1(x, p)
f1 = sum(x .^ 2) # Sphere function
f2 = sum(x .^ 2 .- 10 .* cos.(2π .* x) .+ 10) # Rastrigin function
return (f1, f2)
end

mof_1 = MultiObjectiveOptimizationFunction(multi_obj_func_1)
prob_1 = Optimization.OptimizationProblem(mof_1, u0; lb = lb, ub = ub)
sol_1 = solve(prob_1, opt, NumDimensions = 2,
FitnessScheme = ParetoFitnessScheme{2}(is_minimizing = true))

@test sol_1 nothing
println("Solution for Sphere and Rastrigin: ", sol_1)
@test sol_1.objective[1]6.9905986e-18 atol=1e-3
@test sol_1.objective[2]1.7763568e-15 atol=1e-3
end

mof_1 = MultiObjectiveOptimizationFunction(multi_obj_func_1)
prob_1 = Optimization.OptimizationProblem(mof_1, u0; lb=lb, ub=ub)
sol_1 = solve(prob_1, opt, NumDimensions=2, FitnessScheme=ParetoFitnessScheme{2}(is_minimizing=true))

@test sol_1 nothing
println("Solution for Sphere and Rastrigin: ", sol_1)
@test sol_1.objective[1] 6.9905986e-18 atol=1e-3
@test sol_1.objective[2] 1.7763568e-15 atol=1e-3
end

# Test 2: Rosenbrock and Ackley Functions
@testset "Rosenbrock and Ackley Functions" begin
function multi_obj_func_2(x, p)
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_obj_func_2(x, p)
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

mof_2 = MultiObjectiveOptimizationFunction(multi_obj_func_2)
prob_2 = Optimization.OptimizationProblem(mof_2, u0; lb = lb, ub = ub)
sol_2 = solve(prob_2, opt, NumDimensions = 2,
FitnessScheme = ParetoFitnessScheme{2}(is_minimizing = true))

@test sol_2 nothing
println("Solution for Rosenbrock and Ackley: ", sol_2)
@test sol_2.objective[1]0.97438 atol=1e-3
@test sol_2.objective[2]0.04088 atol=1e-3
end

mof_2 = MultiObjectiveOptimizationFunction(multi_obj_func_2)
prob_2 = Optimization.OptimizationProblem(mof_2, u0; lb=lb, ub=ub)
sol_2 = solve(prob_2, opt, NumDimensions=2, FitnessScheme=ParetoFitnessScheme{2}(is_minimizing=true))

@test sol_2 nothing
println("Solution for Rosenbrock and Ackley: ", sol_2)
@test sol_2.objective[1] 0.97438 atol=1e-3
@test sol_2.objective[2] 0.04088 atol=1e-3
end

# Test 3: ZDT1 Function
@testset "ZDT1 Function" begin
function multi_obj_func_3(x, p)
f1 = x[1]
g = 1 + 9 * sum(x[2:end]) / (length(x) - 1)
f2 = g * (1 - sqrt(f1 / g))
return (f1, f2)
# Test 3: ZDT1 Function
@testset "ZDT1 Function" begin
function multi_obj_func_3(x, p)
f1 = x[1]
g = 1 + 9 * sum(x[2:end]) / (length(x) - 1)
f2 = g * (1 - sqrt(f1 / g))
return (f1, f2)
end

mof_3 = MultiObjectiveOptimizationFunction(multi_obj_func_3)
prob_3 = Optimization.OptimizationProblem(mof_3, u0; lb = lb, ub = ub)
sol_3 = solve(prob_3, opt, NumDimensions = 2,
FitnessScheme = ParetoFitnessScheme{2}(is_minimizing = true))

@test sol_3 nothing
println("Solution for ZDT1: ", sol_3)
@test sol_3.objective[1]0.273445 atol=1e-3
@test sol_3.objective[2]0.477079 atol=1e-3
end

mof_3 = MultiObjectiveOptimizationFunction(multi_obj_func_3)
prob_3 = Optimization.OptimizationProblem(mof_3, u0; lb=lb, ub=ub)
sol_3 = solve(prob_3, opt, NumDimensions=2, FitnessScheme=ParetoFitnessScheme{2}(is_minimizing=true))

@test sol_3 nothing
println("Solution for ZDT1: ", sol_3)
@test sol_3.objective[1] 0.273445 atol=1e-3
@test sol_3.objective[2] 0.477079 atol=1e-3
end
end
end
27 changes: 15 additions & 12 deletions lib/OptimizationEvolutionary/src/OptimizationEvolutionary.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,15 @@ function SciMLBase.__solve(cache::OptimizationCache{
cons = WorstFitnessConstraints(Float64[], Float64[], cache.lcons, cache.ucons,
c)
if isa(f, MultiObjectiveOptimizationFunction)
opt_res = Evolutionary.optimize(_loss, _loss(cache.u0), cons, cache.u0, cache.opt, opt_args)
opt_res = Evolutionary.optimize(
_loss, _loss(cache.u0), cons, cache.u0, cache.opt, opt_args)
else
opt_res = Evolutionary.optimize(_loss, cons, cache.u0, cache.opt, opt_args)
end
else
if isa(f, MultiObjectiveOptimizationFunction)
opt_res = Evolutionary.optimize(_loss, _loss(cache.u0), cache.u0, cache.opt, opt_args)
opt_res = Evolutionary.optimize(
_loss, _loss(cache.u0), cache.u0, cache.opt, opt_args)
else
opt_res = Evolutionary.optimize(_loss, cache.u0, cache.opt, opt_args)
end
Expand All @@ -165,9 +167,10 @@ function SciMLBase.__solve(cache::OptimizationCache{
cons = BoxConstraints(cache.lb, cache.ub)
end
if isa(f, MultiObjectiveOptimizationFunction)
opt_res = Evolutionary.optimize(_loss, _loss(cache.u0), cons, cache.u0, cache.opt, opt_args)
opt_res = Evolutionary.optimize(
_loss, _loss(cache.u0), cons, cache.u0, cache.opt, opt_args)
else
opt_res = Evolutionary.optimize(_loss, cons, cache.u0, cache.opt, opt_args)
opt_res = Evolutionary.optimize(_loss, cons, cache.u0, cache.opt, opt_args)
end
end
t1 = time()
Expand All @@ -176,17 +179,17 @@ function SciMLBase.__solve(cache::OptimizationCache{
time = t1 - t0, fevals = opt_res.f_calls)
if !isa(f, MultiObjectiveOptimizationFunction)
SciMLBase.build_solution(cache, cache.opt,
Evolutionary.minimizer(opt_res),
Evolutionary.minimum(opt_res); original = opt_res,
retcode = opt_ret,
stats = stats)
Evolutionary.minimizer(opt_res),
Evolutionary.minimum(opt_res); original = opt_res,
retcode = opt_ret,
stats = stats)
else
ans = Evolutionary.minimizer(opt_res)
SciMLBase.build_solution(cache, cache.opt,
ans,
_loss(ans[1]); original = opt_res,
retcode = opt_ret,
stats = stats)
ans,
_loss(ans[1]); original = opt_res,
retcode = opt_ret,
stats = stats)
end
end

Expand Down
55 changes: 28 additions & 27 deletions lib/OptimizationEvolutionary/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ Random.seed!(1234)
# 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)
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)
obj_func = MultiObjectiveOptimizationFunction(func, jac = gradient_multi_objective)

# Set up the evolutionary algorithm (e.g., NSGA2)
algorithm = OptimizationEvolutionary.NSGA2()
Expand All @@ -84,39 +84,40 @@ Random.seed!(1234)

# Test 1: Sphere and Rastrigin Functions
@testset "Sphere and Rastrigin Functions" begin
function multi_objective_1(x, p=nothing)::Vector{Float64}
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
@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

# Test 2: Rosenbrock and Ackley Functions
@testset "Rosenbrock and Ackley Functions" begin
function multi_objective_2(x, p=nothing)::Vector{Float64}
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
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
@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

# Test 3: ZDT1 Function
@testset "ZDT1 Function" begin
function multi_objective_3(x, p=nothing)::Vector{Float64}
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
Expand All @@ -126,42 +127,42 @@ Random.seed!(1234)
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 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

# Test 4: DTLZ2 Function
@testset "DTLZ2 Function" begin
function multi_objective_4(x, p=nothing)::Vector{Float64}
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
@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

# Test 5: Schaffer Function N.2
@testset "Schaffer Function N.2" begin
function multi_objective_5(x, p=nothing)::Vector{Float64}
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
@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
end
end

0 comments on commit 3c8295d

Please sign in to comment.