Skip to content

Commit 6ecd588

Browse files
Merge pull request #805 from ParasPuneetSingh/master
Update __loss function for MOO using OptimizationMetaheuristics.jl
2 parents 9c4f74d + 5d118bd commit 6ecd588

File tree

2 files changed

+149
-3
lines changed

2 files changed

+149
-3
lines changed

lib/OptimizationMetaheuristics/src/OptimizationMetaheuristics.jl

+7-2
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,14 @@ function SciMLBase.__solve(cache::OptimizationCache{
107107
maxiters = Optimization._check_and_convert_maxiters(cache.solver_args.maxiters)
108108
maxtime = Optimization._check_and_convert_maxtime(cache.solver_args.maxtime)
109109

110+
f=cache.f
110111
_loss = function (θ)
111-
x = cache.f(θ, cache.p)
112-
return first(x)
112+
if isa(f,MultiObjectiveOptimizationFunction)
113+
return cache.f(θ, cache.p)
114+
else
115+
x = cache.f(θ, cache.p)
116+
return first(x)
117+
end
113118
end
114119

115120
if !isnothing(cache.lb) & !isnothing(cache.ub)

lib/OptimizationMetaheuristics/test/runtests.jl

+142-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
using OptimizationMetaheuristics, Optimization
1+
using OptimizationMetaheuristics, Optimization, Random
22
using Test
33

4+
Random.seed!(42)
45
@testset "OptimizationMetaheuristics.jl" begin
56
rosenbrock(x, p) = (p[1] - x[1])^2 + p[2] * (x[2] - x[1]^2)^2
67
x0 = zeros(2)
@@ -50,4 +51,144 @@ using Test
5051

5152
sol = solve(prob, WOA(), use_initial = true)
5253
@test 10 * sol.objective < l1
54+
55+
# Define the benchmark functions as multi-objective problems
56+
function sphere(x)
57+
f1 = sum(x .^ 2)
58+
f2 = sum((x .- 2.0) .^ 2)
59+
gx = [0.0]
60+
hx = [0.0]
61+
return [f1, f2], gx, hx
62+
end
63+
64+
function rastrigin(x)
65+
f1 = sum(x .^ 2 .- 10 .* cos.(2 .* π .* x) .+ 10)
66+
f2 = sum((x .- 2.0) .^ 2 .- 10 .* cos.(2 .* π .* (x .- 2.0)) .+ 10)
67+
gx = [0.0]
68+
hx = [0.0]
69+
return [f1, f2], gx, hx
70+
end
71+
72+
function rosenbrock(x)
73+
f1 = sum(100 .* (x[2:end] .- x[1:end-1] .^ 2) .^ 2 .+ (x[1:end-1] .- 1) .^ 2)
74+
f2 = sum(100 .* ((x[2:end] .- 2.0) .- (x[1:end-1] .^ 2)) .^ 2 .+ ((x[1:end-1] .- 1.0) .^ 2))
75+
gx = [0.0]
76+
hx = [0.0]
77+
return [f1, f2], gx, hx
78+
end
79+
80+
function ackley(x)
81+
f1 = -20 * exp(-0.2 * sqrt(sum(x .^ 2) / length(x))) - exp(sum(cos.(2 * π .* x)) / length(x)) + 20 +
82+
f2 = -20 * exp(-0.2 * sqrt(sum((x .- 2.0) .^ 2) / length(x))) - exp(sum(cos.(2 * π .* (x .- 2.0))) / length(x)) + 20 +
83+
gx = [0.0]
84+
hx = [0.0]
85+
return [f1, f2], gx, hx
86+
end
87+
88+
89+
function dtlz2(x)
90+
g = sum((x[3:end] .- 0.5) .^ 2)
91+
f1 = (1 + g) * cos(x[1] * π / 2) * cos(x[2] * π / 2)
92+
f2 = (1 + g) * cos(x[1] * π / 2) * sin(x[2] * π / 2)
93+
gx = [0.0]
94+
hx = [0.0]
95+
return [f1, f2], gx, hx
96+
end
97+
98+
function schaffer_n2(x)
99+
f1 = x[1]^2
100+
f2 = (x[1] - 2.0)^2
101+
gx = [0.0]
102+
hx = [0.0]
103+
return [f1, f2], gx, hx
104+
end
105+
OBJECTIVES = Dict(
106+
"Metaheuristics.Algorithm{NSGA2} for sphere"=> [2.1903011284699687, 3.9825426762781477],
107+
"Metaheuristics.Algorithm{NSGA3} for sphere"=> [0.36916068436590516, 8.256797942777018],
108+
"Metaheuristics.Algorithm{SPEA2} for sphere"=> [0.6866588142724173, 7.18284015333389],
109+
"Metaheuristics.Algorithm{CCMO{NSGA2}} for sphere"=> [1.6659983952552437, 4.731690734657798],
110+
"Metaheuristics.Algorithm{MOEAD_DE} for sphere"=> [1.3118335977331483, 5.478715622895562],
111+
"Metaheuristics.Algorithm{SMS_EMOA} for sphere"=> [0.5003293369817386, 7.837151299208113],
112+
"Metaheuristics.Algorithm{NSGA2} for rastrigin"=> [0.0, 12.0],
113+
"Metaheuristics.Algorithm{NSGA3} for rastrigin"=> [9.754810555001253, 11.123569741993528],
114+
"Metaheuristics.Algorithm{SPEA2} for rastrigin"=> [0.0, 12.0],
115+
"Metaheuristics.Algorithm{CCMO{NSGA2}} for rastrigin"=> [2.600961284360525, 3.4282466721631755],
116+
"Metaheuristics.Algorithm{MOEAD_DE} for rastrigin"=> [2.4963842982482607, 10.377445766099369],
117+
"Metaheuristics.Algorithm{SMS_EMOA} for rastrigin"=> [0.0, 12.0],
118+
"Metaheuristics.Algorithm{NSGA2} for rosenbrock"=> [17.500214034475118, 586.5039366722865],
119+
"Metaheuristics.Algorithm{NSGA3} for rosenbrock"=> [60.58413196101549, 427.34913230512063] ,
120+
"Metaheuristics.Algorithm{SPEA2} for rosenbrock"=> [37.42314302223994, 498.8799375425481],
121+
"Metaheuristics.Algorithm{CCMO{NSGA2}} for rosenbrock"=> [2.600961284360525, 3.4282466721631755],
122+
"Metaheuristics.Algorithm{MOEAD_DE} for rosenbrock"=> [12.969698120217537, 642.4135236259822],
123+
"Metaheuristics.Algorithm{SMS_EMOA} for rosenbrock"=> [61.6898556398449, 450.62433057243777],
124+
"Metaheuristics.Algorithm{NSGA2} for ackley"=> [2.240787163704834, 5.990002878952371],
125+
"Metaheuristics.Algorithm{NSGA3} for ackley"=> [3.408535107623966, 5.459538604033934],
126+
"Metaheuristics.Algorithm{SPEA2} for ackley"=> [4.440892098500626e-16, 6.593599079287213],
127+
"Metaheuristics.Algorithm{CCMO{NSGA2}} for ackley"=> [2.600961284360525, 3.4282466721631755],
128+
"Metaheuristics.Algorithm{MOEAD_DE} for ackley"=> [4.440892098500626e-16, 6.593599079287213],
129+
"Metaheuristics.Algorithm{SMS_EMOA} for ackley"=> [3.370770500897429, 5.510527199861947],
130+
"Metaheuristics.Algorithm{NSGA2} for dtlz2"=> [0.013283104966270814, 0.010808186786590583],
131+
"Metaheuristics.Algorithm{NSGA3} for dtlz2"=> [0.013428265441897881, 0.03589930489326534],
132+
"Metaheuristics.Algorithm{SPEA2} for dtlz2"=> [0.019006068021099495, 0.0009905093731377751],
133+
"Metaheuristics.Algorithm{CCMO{NSGA2}} for dtlz2"=> [2.600961284360525, 3.4282466721631755],
134+
"Metaheuristics.Algorithm{MOEAD_DE} for dtlz2"=> [0.027075258566241527, 0.00973958317460759],
135+
"Metaheuristics.Algorithm{SMS_EMOA} for dtlz2"=> [0.056304481489060705, 0.026075248436234502],
136+
"Metaheuristics.Algorithm{NSGA2} for schaffer_n2"=> [1.4034569322987955, 0.6647534264038837],
137+
"Metaheuristics.Algorithm{NSGA3} for schaffer_n2"=> [2.7987535368174363, 0.10696329884083178],
138+
"Metaheuristics.Algorithm{SPEA2} for schaffer_n2"=> [0.0007534237111212252, 3.8909591643988075],
139+
"Metaheuristics.Algorithm{CCMO{NSGA2}} for schaffer_n2"=> [3.632401400816196e-17, 4.9294679997494206e-17],
140+
"Metaheuristics.Algorithm{MOEAD_DE} for schaffer_n2"=> [2.50317097527324, 0.17460592430221922],
141+
"Metaheuristics.Algorithm{SMS_EMOA} for schaffer_n2"=> [0.4978888767998813, 1.67543922644328],
142+
)
143+
# Define the testset
144+
@testset "Multi-Objective Optimization with Various Functions and Metaheuristics" begin
145+
# Define the problems and their bounds
146+
problems = [
147+
(sphere, [0.0, 0.0, 0.0], [1.0, 1.0, 1.0]),
148+
(rastrigin, [0.0, 0.0, 0.0], [1.0, 1.0, 1.0]),
149+
(rosenbrock, [0.0, 0.0, 0.0], [1.0, 1.0, 1.0]),
150+
(ackley, [0.0, 0.0, 0.0], [1.0, 1.0, 1.0]),
151+
(dtlz2, [0.0, 0.0, 0.0], [1.0, 1.0, 1.0]),
152+
(schaffer_n2, [0.0, 0.0, 0.0], [2.0, 0.0, 0.0])
153+
]
154+
155+
nobjectives = 2
156+
npartitions = 100
157+
158+
# Define the different algorithms
159+
algs = [
160+
NSGA2(),
161+
NSGA3(),
162+
SPEA2(),
163+
CCMO(NSGA2(N=100, p_m=0.001)),
164+
MOEAD_DE(gen_ref_dirs(nobjectives, npartitions), options=Options(debug=false, iterations = 250)),
165+
SMS_EMOA()
166+
]
167+
168+
# Run tests for each problem and algorithm
169+
for (prob_func, lb, ub) in problems
170+
prob_name = string(prob_func)
171+
for alg in algs
172+
alg_name = string(typeof(alg))
173+
@testset "$alg_name on $prob_name" begin
174+
multi_obj_fun = MultiObjectiveOptimizationFunction((x, p) -> prob_func(x))
175+
prob = OptimizationProblem(multi_obj_fun, lb; lb = lb, ub = ub)
176+
if (alg_name=="Metaheuristics.Algorithm{CCMO{NSGA2}}")
177+
sol = solve(prob, alg)
178+
else
179+
sol = solve(prob, alg; maxiters = 100, use_initial = true)
180+
end
181+
182+
# Tests
183+
@test !isempty(sol.minimizer) # Check that a solution was found
184+
185+
# Use sol.objective to get the objective values
186+
key = "$alg_name for $prob_name"
187+
value = OBJECTIVES[key]
188+
objectives = sol.objective
189+
@test value objectives atol=0.95
190+
end
191+
end
192+
end
193+
end
53194
end

0 commit comments

Comments
 (0)