Single-objective

Throughout this section, we will solve the classic Rastrigin problem imported from pymoo. Three strategies of how to instantiate it from scratch are also presented in the complete tutorial.

Besides the original DE [3], in this example the PSO [13] implementation from pymoo will also be used.

Rastrigin

\[\begin{split}\begin{align} \text{min} \; \; & f(\boldsymbol{x}) = An + \sum_{i=1}^{n}[x_i^2 - A \, \mathrm{cos}(2 \pi x_i)]\\ \text{s.t.} \; \; & -5.12 \leq x_i \leq 5.12 & \forall i \in \{ 1, 2 \} \end{align}\end{split}\]
[1]:
from pymoo.algorithms.soo.nonconvex.pso import PSO
from pymoo.optimize import minimize
from pymoo.problems import get_problem
from pymoode.algorithms import GDE3
from pymoode.algorithms import DE
[2]:
problem = get_problem("rastrigin")
[3]:
NGEN = 100
POPSIZE = 20
SEED = 3
[4]:
# DE Parameters
CR = 0.5
F = (0.3, 1.0)
[5]:
de = DE(pop_size=POPSIZE, variant="DE/rand/1/bin", CR=CR, F=F)

res_de = minimize(
    problem,
    de,
    ('n_gen', NGEN),
    seed=SEED,
    save_history=False,
    verbose=False,
)

print(res_de.F)
print(res_de.X)
[1.42108547e-14]
[-9.85467210e-11  8.38205833e-09]
[6]:
pso = PSO(pop_size=POPSIZE)

res_pso = minimize(
    problem,
    pso,
    ('n_gen', NGEN),
    seed=SEED,
    save_history=False,
    verbose=False,
)

print(res_pso.F)
print(res_pso.X)
[2.00159889e-11]
[-1.28686492e-07  2.90416106e-07]