Skip to content

Commit 3c8295d

Browse files
format
1 parent c6f9e90 commit 3c8295d

File tree

6 files changed

+108
-101
lines changed

6 files changed

+108
-101
lines changed

docs/src/index.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,7 @@ to add the specific wrapper packages.
170170
- Unconstrained: ✅
171171
</details>
172172
🟡 = supported in downstream library but not yet implemented in `Optimization.jl`; PR to add this functionality are welcome
173-
174173
## Citation
175-
176174
```
177175
@software{vaibhav_kumar_dixit_2023_7738525,
178176
author = {Vaibhav Kumar Dixit and Christopher Rackauckas},

docs/src/tutorials/remakecomposition.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ end
2626
2727
optf = OptimizationFunction(f_schwefel, Optimization.AutoReverseDiff(compile = true))
2828
29-
x0 = ones(10).*200.0
30-
prob = OptimizationProblem(optf, x0, [418.9829], lb = fill(-500.0, 10), ub = fill(500.0, 10))
29+
x0 = ones(10) .* 200.0
30+
prob = OptimizationProblem(
31+
optf, x0, [418.9829], lb = fill(-500.0, 10), ub = fill(500.0, 10))
3132
3233
@show f_schwefel(x0)
3334
```
3435

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

3940
```@example polyalg

lib/OptimizationBBO/src/OptimizationBBO.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ for j in string.(BlackBoxOptim.SingleObjectiveMethodNames)
1717
end
1818

1919
Base.@kwdef struct BBO_borg_moea <: BBO
20-
method = :borg_moea
20+
method = :borg_moea
2121
end
2222
export BBO_borg_moea
2323

lib/OptimizationBBO/test/runtests.jl

Lines changed: 59 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -49,68 +49,72 @@ using Test
4949
end
5050

5151
# Define the initial guess and bounds
52-
u0 = [0.25, 0.25]
53-
lb = [0.0, 0.0]
54-
ub = [2.0, 2.0]
52+
u0 = [0.25, 0.25]
53+
lb = [0.0, 0.0]
54+
ub = [2.0, 2.0]
5555

56-
# Define the optimizer
57-
opt = OptimizationBBO.BBO_borg_moea()
56+
# Define the optimizer
57+
opt = OptimizationBBO.BBO_borg_moea()
5858

59-
@testset "Multi-Objective Optimization Tests" begin
59+
@testset "Multi-Objective Optimization Tests" begin
6060

61-
# Test 1: Sphere and Rastrigin Functions
62-
@testset "Sphere and Rastrigin Functions" begin
63-
function multi_obj_func_1(x, p)
64-
f1 = sum(x .^ 2) # Sphere function
65-
f2 = sum(x .^ 2 .- 10 .* cos.(2π .* x) .+ 10) # Rastrigin function
66-
return (f1, f2)
61+
# Test 1: Sphere and Rastrigin Functions
62+
@testset "Sphere and Rastrigin Functions" begin
63+
function multi_obj_func_1(x, p)
64+
f1 = sum(x .^ 2) # Sphere function
65+
f2 = sum(x .^ 2 .- 10 .* cos.(2π .* x) .+ 10) # Rastrigin function
66+
return (f1, f2)
67+
end
68+
69+
mof_1 = MultiObjectiveOptimizationFunction(multi_obj_func_1)
70+
prob_1 = Optimization.OptimizationProblem(mof_1, u0; lb = lb, ub = ub)
71+
sol_1 = solve(prob_1, opt, NumDimensions = 2,
72+
FitnessScheme = ParetoFitnessScheme{2}(is_minimizing = true))
73+
74+
@test sol_1 nothing
75+
println("Solution for Sphere and Rastrigin: ", sol_1)
76+
@test sol_1.objective[1]6.9905986e-18 atol=1e-3
77+
@test sol_1.objective[2]1.7763568e-15 atol=1e-3
6778
end
68-
69-
mof_1 = MultiObjectiveOptimizationFunction(multi_obj_func_1)
70-
prob_1 = Optimization.OptimizationProblem(mof_1, u0; lb=lb, ub=ub)
71-
sol_1 = solve(prob_1, opt, NumDimensions=2, FitnessScheme=ParetoFitnessScheme{2}(is_minimizing=true))
72-
73-
@test sol_1 nothing
74-
println("Solution for Sphere and Rastrigin: ", sol_1)
75-
@test sol_1.objective[1] 6.9905986e-18 atol=1e-3
76-
@test sol_1.objective[2] 1.7763568e-15 atol=1e-3
77-
end
7879

79-
# Test 2: Rosenbrock and Ackley Functions
80-
@testset "Rosenbrock and Ackley Functions" begin
81-
function multi_obj_func_2(x, p)
82-
f1 = (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2 # Rosenbrock function
83-
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
84-
return (f1, f2)
80+
# Test 2: Rosenbrock and Ackley Functions
81+
@testset "Rosenbrock and Ackley Functions" begin
82+
function multi_obj_func_2(x, p)
83+
f1 = (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2 # Rosenbrock function
84+
f2 = -20.0 * exp(-0.2 * sqrt(0.5 * (x[1]^2 + x[2]^2))) -
85+
exp(0.5 * (cos(2π * x[1]) + cos(2π * x[2]))) + exp(1) + 20.0 # Ackley function
86+
return (f1, f2)
87+
end
88+
89+
mof_2 = MultiObjectiveOptimizationFunction(multi_obj_func_2)
90+
prob_2 = Optimization.OptimizationProblem(mof_2, u0; lb = lb, ub = ub)
91+
sol_2 = solve(prob_2, opt, NumDimensions = 2,
92+
FitnessScheme = ParetoFitnessScheme{2}(is_minimizing = true))
93+
94+
@test sol_2 nothing
95+
println("Solution for Rosenbrock and Ackley: ", sol_2)
96+
@test sol_2.objective[1]0.97438 atol=1e-3
97+
@test sol_2.objective[2]0.04088 atol=1e-3
8598
end
86-
87-
mof_2 = MultiObjectiveOptimizationFunction(multi_obj_func_2)
88-
prob_2 = Optimization.OptimizationProblem(mof_2, u0; lb=lb, ub=ub)
89-
sol_2 = solve(prob_2, opt, NumDimensions=2, FitnessScheme=ParetoFitnessScheme{2}(is_minimizing=true))
90-
91-
@test sol_2 nothing
92-
println("Solution for Rosenbrock and Ackley: ", sol_2)
93-
@test sol_2.objective[1] 0.97438 atol=1e-3
94-
@test sol_2.objective[2] 0.04088 atol=1e-3
95-
end
9699

97-
# Test 3: ZDT1 Function
98-
@testset "ZDT1 Function" begin
99-
function multi_obj_func_3(x, p)
100-
f1 = x[1]
101-
g = 1 + 9 * sum(x[2:end]) / (length(x) - 1)
102-
f2 = g * (1 - sqrt(f1 / g))
103-
return (f1, f2)
100+
# Test 3: ZDT1 Function
101+
@testset "ZDT1 Function" begin
102+
function multi_obj_func_3(x, p)
103+
f1 = x[1]
104+
g = 1 + 9 * sum(x[2:end]) / (length(x) - 1)
105+
f2 = g * (1 - sqrt(f1 / g))
106+
return (f1, f2)
107+
end
108+
109+
mof_3 = MultiObjectiveOptimizationFunction(multi_obj_func_3)
110+
prob_3 = Optimization.OptimizationProblem(mof_3, u0; lb = lb, ub = ub)
111+
sol_3 = solve(prob_3, opt, NumDimensions = 2,
112+
FitnessScheme = ParetoFitnessScheme{2}(is_minimizing = true))
113+
114+
@test sol_3 nothing
115+
println("Solution for ZDT1: ", sol_3)
116+
@test sol_3.objective[1]0.273445 atol=1e-3
117+
@test sol_3.objective[2]0.477079 atol=1e-3
104118
end
105-
106-
mof_3 = MultiObjectiveOptimizationFunction(multi_obj_func_3)
107-
prob_3 = Optimization.OptimizationProblem(mof_3, u0; lb=lb, ub=ub)
108-
sol_3 = solve(prob_3, opt, NumDimensions=2, FitnessScheme=ParetoFitnessScheme{2}(is_minimizing=true))
109-
110-
@test sol_3 nothing
111-
println("Solution for ZDT1: ", sol_3)
112-
@test sol_3.objective[1] 0.273445 atol=1e-3
113-
@test sol_3.objective[2] 0.477079 atol=1e-3
114119
end
115120
end
116-
end

lib/OptimizationEvolutionary/src/OptimizationEvolutionary.jl

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,15 @@ function SciMLBase.__solve(cache::OptimizationCache{
146146
cons = WorstFitnessConstraints(Float64[], Float64[], cache.lcons, cache.ucons,
147147
c)
148148
if isa(f, MultiObjectiveOptimizationFunction)
149-
opt_res = Evolutionary.optimize(_loss, _loss(cache.u0), cons, cache.u0, cache.opt, opt_args)
149+
opt_res = Evolutionary.optimize(
150+
_loss, _loss(cache.u0), cons, cache.u0, cache.opt, opt_args)
150151
else
151152
opt_res = Evolutionary.optimize(_loss, cons, cache.u0, cache.opt, opt_args)
152153
end
153154
else
154155
if isa(f, MultiObjectiveOptimizationFunction)
155-
opt_res = Evolutionary.optimize(_loss, _loss(cache.u0), cache.u0, cache.opt, opt_args)
156+
opt_res = Evolutionary.optimize(
157+
_loss, _loss(cache.u0), cache.u0, cache.opt, opt_args)
156158
else
157159
opt_res = Evolutionary.optimize(_loss, cache.u0, cache.opt, opt_args)
158160
end
@@ -165,9 +167,10 @@ function SciMLBase.__solve(cache::OptimizationCache{
165167
cons = BoxConstraints(cache.lb, cache.ub)
166168
end
167169
if isa(f, MultiObjectiveOptimizationFunction)
168-
opt_res = Evolutionary.optimize(_loss, _loss(cache.u0), cons, cache.u0, cache.opt, opt_args)
170+
opt_res = Evolutionary.optimize(
171+
_loss, _loss(cache.u0), cons, cache.u0, cache.opt, opt_args)
169172
else
170-
opt_res = Evolutionary.optimize(_loss, cons, cache.u0, cache.opt, opt_args)
173+
opt_res = Evolutionary.optimize(_loss, cons, cache.u0, cache.opt, opt_args)
171174
end
172175
end
173176
t1 = time()
@@ -176,17 +179,17 @@ function SciMLBase.__solve(cache::OptimizationCache{
176179
time = t1 - t0, fevals = opt_res.f_calls)
177180
if !isa(f, MultiObjectiveOptimizationFunction)
178181
SciMLBase.build_solution(cache, cache.opt,
179-
Evolutionary.minimizer(opt_res),
180-
Evolutionary.minimum(opt_res); original = opt_res,
181-
retcode = opt_ret,
182-
stats = stats)
182+
Evolutionary.minimizer(opt_res),
183+
Evolutionary.minimum(opt_res); original = opt_res,
184+
retcode = opt_ret,
185+
stats = stats)
183186
else
184187
ans = Evolutionary.minimizer(opt_res)
185188
SciMLBase.build_solution(cache, cache.opt,
186-
ans,
187-
_loss(ans[1]); original = opt_res,
188-
retcode = opt_ret,
189-
stats = stats)
189+
ans,
190+
_loss(ans[1]); original = opt_res,
191+
retcode = opt_ret,
192+
stats = stats)
190193
end
191194
end
192195

lib/OptimizationEvolutionary/test/runtests.jl

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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
167168
end

0 commit comments

Comments
 (0)