Skip to content

Commit

Permalink
Fix anyoptimization#207: Added DE tests and fixed another selection i…
Browse files Browse the repository at this point in the history
…ssue.
  • Loading branch information
blankjul committed Nov 9, 2021
1 parent b5631e1 commit 54cb597
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 21 deletions.
20 changes: 11 additions & 9 deletions pymoo/algorithms/soo/nonconvex/de.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,28 +138,30 @@ def _do(self, pop, n_select, n_parents, **kwargs):
# create offsprings and add it to the data of the algorithm
P = RandomSelection().do(pop, n_select, n_parents)

n = len(P)

if variant == "best":
P[:, 0] = 0
elif variant == "current-to-best":
P[:, 0] = np.arange(len(pop))
P[:, 0] = np.arange(n)
P[:, 1] = 0
P[:, 2] = np.arange(len(pop))
P[:, 2] = np.arange(n)
elif variant == "current-to-rand":
P[:, 0] = np.arange(len(pop))
P[:, 2] = np.arange(len(pop))
P[:, 0] = np.arange(n)
P[:, 2] = np.arange(n)
elif variant == "rand-to-best":
P[:, 1] = 0
P[:, 2] = np.arange(len(pop))
P[:, 2] = np.arange(n)
elif variant == "current-to-pbest":
# best 10% of the population
n_pbest = int(np.ceil(0.1 * len(pop)))
n_pbest = max(1, int(np.ceil(0.1 * n)))

# the corresponding indices to select from
pbest = np.arange(n_pbest)

P[:, 0] = np.arange(len(pop))
P[:, 1] = np.random.choice(pbest, len(pop))
P[:, 2] = np.arange(len(pop))
P[:, 0] = np.arange(n)
P[:, 1] = np.random.choice(pbest, n)
P[:, 2] = np.arange(n)

return P

Expand Down
25 changes: 25 additions & 0 deletions tests/algorithms/test_de.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import pytest

from pymoo.algorithms.soo.nonconvex.de import DE
from pymoo.factory import get_problem
from pymoo.operators.sampling.lhs import LHS
from pymoo.optimize import minimize


@pytest.mark.parametrize('selection', ["rand", "best", "rand-to-best", "current-to-rand", "current-to-best", "current-to-pbest"])
@pytest.mark.parametrize('crossover', ["bin", "exp"])
def test_de(selection, crossover):
problem = get_problem("ackley", n_var=10)

algorithm = DE(
pop_size=100,
sampling=LHS(),
variant=f"DE/{selection}/1/{crossover}")

ret = minimize(problem,
algorithm,
('n_gen', 20),
seed=1,
verbose=True)

assert len(ret.opt) > 0
25 changes: 13 additions & 12 deletions tests/scratch.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
from pymoo.algorithms.moo.nsga2 import NSGA2
from pymoo.algorithms.soo.nonconvex.de import DE
from pymoo.factory import get_problem
from pymoo.operators.sampling.lhs import LHS
from pymoo.optimize import minimize
from pymoo.visualization.scatter import Scatter

problem = get_problem("zdt1")
problem = get_problem("ackley", n_var=10)

algorithm = NSGA2(pop_size=100)
algorithm = DE(
pop_size=100,
sampling=LHS(),
variant='DE/current-to-pbest/1/bin')

res = minimize(problem,
algorithm,
('n_gen', 200),
seed=1,
verbose=True)
algorithm,
seed=1,
verbose=False)




plot = Scatter()
plot.add(problem.pareto_front(), plot_type="line", color="black", alpha=0.7)
plot.add(res.F, color="red")
plot.show()

0 comments on commit 54cb597

Please sign in to comment.