From 54cb597f7cc20e10918d9fcfd59bb21610f89b57 Mon Sep 17 00:00:00 2001 From: Julian Blank Date: Tue, 9 Nov 2021 08:23:15 -0500 Subject: [PATCH] Fix #207: Added DE tests and fixed another selection issue. --- pymoo/algorithms/soo/nonconvex/de.py | 20 +++++++++++--------- tests/algorithms/test_de.py | 25 +++++++++++++++++++++++++ tests/scratch.py | 25 +++++++++++++------------ 3 files changed, 49 insertions(+), 21 deletions(-) create mode 100644 tests/algorithms/test_de.py diff --git a/pymoo/algorithms/soo/nonconvex/de.py b/pymoo/algorithms/soo/nonconvex/de.py index f18a471e8..ddd26efed 100755 --- a/pymoo/algorithms/soo/nonconvex/de.py +++ b/pymoo/algorithms/soo/nonconvex/de.py @@ -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 diff --git a/tests/algorithms/test_de.py b/tests/algorithms/test_de.py new file mode 100644 index 000000000..f9c878b9a --- /dev/null +++ b/tests/algorithms/test_de.py @@ -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 diff --git a/tests/scratch.py b/tests/scratch.py index 46a495bad..1c14f72e9 100644 --- a/tests/scratch.py +++ b/tests/scratch.py @@ -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()