Skip to content

Commit 6dcf336

Browse files
Update tests for MOO runtests.jl
Added tests for MOO algorithms.
1 parent 0a01e9d commit 6dcf336

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed

lib/OptimizationMetaheuristics/test/runtests.jl

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,144 @@ using Test
5050

5151
sol = solve(prob, WOA(), use_initial = true)
5252
@test 10 * sol.objective < l1
53+
54+
# Define the benchmark functions as multi-objective problems
55+
function sphere(x)
56+
f1 = sum(x .^ 2)
57+
f2 = sum((x .- 2.0) .^ 2)
58+
gx = [0.0]
59+
hx = [0.0]
60+
return [f1, f2], gx, hx
61+
end
62+
63+
function rastrigin(x)
64+
f1 = sum(x .^ 2 .- 10 .* cos.(2 .* π .* x) .+ 10)
65+
f2 = sum((x .- 2.0) .^ 2 .- 10 .* cos.(2 .* π .* (x .- 2.0)) .+ 10)
66+
gx = [0.0]
67+
hx = [0.0]
68+
return [f1, f2], gx, hx
69+
end
70+
71+
function rosenbrock(x)
72+
f1 = sum(100 .* (x[2:end] .- x[1:end-1] .^ 2) .^ 2 .+ (x[1:end-1] .- 1) .^ 2)
73+
f2 = sum(100 .* ((x[2:end] .- 2.0) .- (x[1:end-1] .^ 2)) .^ 2 .+ ((x[1:end-1] .- 1.0) .^ 2))
74+
gx = [0.0]
75+
hx = [0.0]
76+
return [f1, f2], gx, hx
77+
end
78+
79+
function ackley(x)
80+
f1 = -20 * exp(-0.2 * sqrt(sum(x .^ 2) / length(x))) - exp(sum(cos.(2 * π .* x)) / length(x)) + 20 +
81+
f2 = -20 * exp(-0.2 * sqrt(sum((x .- 2.0) .^ 2) / length(x))) - exp(sum(cos.(2 * π .* (x .- 2.0))) / length(x)) + 20 +
82+
gx = [0.0]
83+
hx = [0.0]
84+
return [f1, f2], gx, hx
85+
end
86+
87+
88+
function dtlz2(x)
89+
g = sum((x[3:end] .- 0.5) .^ 2)
90+
f1 = (1 + g) * cos(x[1] * π / 2) * cos(x[2] * π / 2)
91+
f2 = (1 + g) * cos(x[1] * π / 2) * sin(x[2] * π / 2)
92+
gx = [0.0]
93+
hx = [0.0]
94+
return [f1, f2], gx, hx
95+
end
96+
97+
function schaffer_n2(x)
98+
f1 = x[1]^2
99+
f2 = (x[1] - 2.0)^2
100+
gx = [0.0]
101+
hx = [0.0]
102+
return [f1, f2], gx, hx
103+
end
104+
OBJECTIVES = Dict(
105+
"Metaheuristics.Algorithm{NSGA2} for sphere"=> [0.4761274648673104, 7.888859360956367],
106+
"Metaheuristics.Algorithm{NSGA3} for sphere"=> [1.1245011962315388, 5.9084439601220105],
107+
"Metaheuristics.Algorithm{SPEA2} for sphere"=> [0.45500157273715425, 8.060476156495577],
108+
"Metaheuristics.Algorithm{CCMO{NSGA2}} for sphere"=> [0.8537159192703154, 6.721186217733861],
109+
"Metaheuristics.Algorithm{MOEAD_DE} for sphere"=> [1.7135443166012259, 4.818225194026158],
110+
"Metaheuristics.Algorithm{SMS_EMOA} for sphere"=> [1.1376191314229631, 5.935092118744685],
111+
"Metaheuristics.Algorithm{NSGA2} for rastrigin"=> [3.914962881168682, 11.552205533592897],
112+
"Metaheuristics.Algorithm{NSGA3} for rastrigin"=> [4.842031386209626, 5.542348181529025],
113+
"Metaheuristics.Algorithm{SPEA2} for rastrigin"=> [2.9692594618763835, 10.596356482458171],
114+
"Metaheuristics.Algorithm{CCMO{NSGA2}} for rastrigin"=> [0.4152393951206974, 7.953188854042798],
115+
"Metaheuristics.Algorithm{MOEAD_DE} for rastrigin"=> [0.0, 12.0],
116+
"Metaheuristics.Algorithm{SMS_EMOA} for rastrigin"=> [10.668382998122013, 11.672554721420616],
117+
"Metaheuristics.Algorithm{NSGA2} for rosenbrock"=> [13.564144823755003, 608.7768632268896],
118+
"Metaheuristics.Algorithm{NSGA3} for rosenbrock"=> [41.32512246661068, 479.9472092328193],
119+
"Metaheuristics.Algorithm{SPEA2} for rosenbrock"=> [20.921291737001457, 566.887198567844],
120+
"Metaheuristics.Algorithm{CCMO{NSGA2}} for rosenbrock"=> [0.4152393951206974, 7.953188854042798],
121+
"Metaheuristics.Algorithm{MOEAD_DE} for rosenbrock"=> [2.215363988408552, 723.1454508385998],
122+
"Metaheuristics.Algorithm{SMS_EMOA} for rosenbrock"=> [20.27041333432111, 575.7366151959259],
123+
"Metaheuristics.Algorithm{NSGA2} for ackley"=> [3.4438643047130992, 5.9371415671384895],
124+
"Metaheuristics.Algorithm{NSGA3} for ackley"=> [3.4659156540969573, 5.287995047899489],
125+
"Metaheuristics.Algorithm{SPEA2} for ackley"=> [2.3209460118197716, 5.918573168574383],
126+
"Metaheuristics.Algorithm{CCMO{NSGA2}} for ackley"=> [0.4152393951206974, 7.953188854042798],
127+
"Metaheuristics.Algorithm{MOEAD_DE} for ackley"=> [4.440892098500626e-16, 6.593599079287213],
128+
"Metaheuristics.Algorithm{SMS_EMOA} for ackley"=> [2.4079028491253074, 6.085847745455787],
129+
"Metaheuristics.Algorithm{NSGA2} for dtlz2"=> [0.0008621981163705847, 0.016776532222616037],
130+
"Metaheuristics.Algorithm{NSGA3} for dtlz2"=> [0.00530717096691627, 0.006810762449448562],
131+
"Metaheuristics.Algorithm{SPEA2} for dtlz2"=> [0.0022573638805422967, 0.0012875185095928014],
132+
"Metaheuristics.Algorithm{CCMO{NSGA2}} for dtlz2"=> [2.9276186095638996, 3.0744092709040185],
133+
"Metaheuristics.Algorithm{MOEAD_DE} for dtlz2"=> [0.0009460864848779976, 0.015153151632789923],
134+
"Metaheuristics.Algorithm{SMS_EMOA} for dtlz2"=> [0.006063356611750317, 0.014614126585905095],
135+
"Metaheuristics.Algorithm{NSGA2} for schaffer_n2"=> [1.0978202866371685, 0.9067435054036517],
136+
"Metaheuristics.Algorithm{NSGA3} for schaffer_n2"=> [2.755035084049435, 0.11571574056316469],
137+
"Metaheuristics.Algorithm{SPEA2} for schaffer_n2"=> [2.2990190172651723, 0.23401248171694122],
138+
"Metaheuristics.Algorithm{CCMO{NSGA2}} for schaffer_n2"=> [0.0, 800.0],
139+
"Metaheuristics.Algorithm{MOEAD_DE} for schaffer_n2"=> [0.0017365039124724727, 3.8350509838468123],
140+
"Metaheuristics.Algorithm{SMS_EMOA} for schaffer_n2"=> [0.7559493982502018, 1.278135376195079],
141+
)
142+
# Define the testset
143+
@testset "Multi-Objective Optimization with Various Functions and Metaheuristics" begin
144+
# Define the problems and their bounds
145+
problems = [
146+
(sphere, [0.0, 0.0, 0.0], [1.0, 1.0, 1.0]),
147+
(rastrigin, [0.0, 0.0, 0.0], [1.0, 1.0, 1.0]),
148+
(rosenbrock, [0.0, 0.0, 0.0], [1.0, 1.0, 1.0]),
149+
(ackley, [0.0, 0.0, 0.0], [1.0, 1.0, 1.0]),
150+
(dtlz2, [0.0, 0.0, 0.0], [1.0, 1.0, 1.0]),
151+
(schaffer_n2, [0.0, 0.0, 0.0], [2.0, 0.0, 0.0])
152+
]
153+
154+
nobjectives = 2
155+
npartitions = 100
156+
157+
# Define the different algorithms
158+
algs = [
159+
NSGA2(),
160+
NSGA3(),
161+
SPEA2(),
162+
CCMO(NSGA2(N=100, p_m=0.001)),
163+
MOEAD_DE(gen_ref_dirs(nobjectives, npartitions), options=Options(debug=false, iterations = 250)),
164+
SMS_EMOA()
165+
]
166+
167+
# Run tests for each problem and algorithm
168+
for (prob_func, lb, ub) in problems
169+
prob_name = string(prob_func)
170+
for alg in algs
171+
alg_name = string(typeof(alg))
172+
@testset "$alg_name on $prob_name" begin
173+
multi_obj_fun = MultiObjectiveOptimizationFunction((x, p) -> prob_func(x))
174+
prob = OptimizationProblem(multi_obj_fun, lb; lb = lb, ub = ub)
175+
if (alg_name=="Metaheuristics.Algorithm{CCMO{NSGA2}}")
176+
sol = solve(prob, alg)
177+
else
178+
sol = solve(prob, alg; maxiters = 100, use_initial = true)
179+
end
180+
181+
# Tests
182+
@test !isempty(sol.minimizer) # Check that a solution was found
183+
184+
# Use sol.objective to get the objective values
185+
key = "$alg_name for $prob_name"
186+
value = OBJECTIVES[key]
187+
objectives = sol.objective
188+
@test value==objectives
189+
end
190+
end
191+
end
192+
end
53193
end

0 commit comments

Comments
 (0)