Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecation warnings and JADE fix #299

Merged
merged 15 commits into from
Mar 30, 2021
2 changes: 1 addition & 1 deletion NiaPy/algorithms/basic/es.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ def changeCount(self, c, cn):
"""
k = 0
for e in cn:
if e not in c: k += 1
if e not in c.tolist(): k += 1
return k

def mutateRand(self, pop, task):
Expand Down
4 changes: 2 additions & 2 deletions NiaPy/algorithms/basic/fa.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ def runIteration(self, task, Fireflies, Intensity, xb, fxb, alpha, **dparams):
"""
alpha = self.alpha_new(task.nFES / self.NP, alpha)
Index = argsort(Intensity)
tmp = asarray([self.move_ffa(i, Fireflies[Index], Intensity[Index], Fireflies, alpha, task) for i in range(self.NP)])
Fireflies, evalF = asarray([tmp[i][0] for i in range(len(tmp))]), asarray([tmp[i][1] for i in range(len(tmp))])
tmp = asarray([self.move_ffa(i, Fireflies[Index], Intensity[Index], Fireflies, alpha, task) for i in range(self.NP)], dtype=object)
Fireflies, evalF = asarray([tmp[i][0] for i in range(len(tmp))], dtype=object), asarray([tmp[i][1] for i in range(len(tmp))])
Intensity[where(evalF)] = apply_along_axis(task.eval, 1, Fireflies[where(evalF)])
xb, fxb = self.getBest(Fireflies, Intensity, xb, fxb)
return Fireflies, Intensity, xb, fxb, {'alpha': alpha}
Expand Down
14 changes: 7 additions & 7 deletions NiaPy/algorithms/modified/jade.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# encoding=utf8
import logging

from numpy import random as rand, concatenate, asarray, argsort
from numpy import random as rand, concatenate, argsort, vstack

from NiaPy.algorithms.basic.de import DifferentialEvolution

Expand All @@ -14,7 +14,7 @@
'CrossRandCurr2Pbest'
]

def CrossRandCurr2Pbest(pop, ic, x_b, f, cr, p=0.2, arc=None, rnd=rand, *args):
def CrossRandCurr2Pbest(pop, ic, fpop, f, cr, p=0.2, arc=None, rnd=rand, *args):
r"""Mutation strategy with crossover.

Mutation strategy uses two different random individuals from population to perform mutation.
Expand All @@ -25,7 +25,7 @@ def CrossRandCurr2Pbest(pop, ic, x_b, f, cr, p=0.2, arc=None, rnd=rand, *args):
Args:
pop (numpy.ndarray): Current population.
ic (int): Index of current individual.
x_b (numpy.ndarray): Global best individual.
fpop (numpy.ndarray): Current population scores.
f (float): Scale factor.
cr (float): Crossover probability.
p (float): Procentage of best individuals to use.
Expand All @@ -40,18 +40,18 @@ def CrossRandCurr2Pbest(pop, ic, x_b, f, cr, p=0.2, arc=None, rnd=rand, *args):
pb = [1.0 / (len(pop) - 1) if i != ic else 0 for i in range(len(pop))] if len(pop) > 1 else None
r = rnd.choice(len(pop), 1, replace=not len(pop) >= 3, p=pb)
# Get pbest index
index, pi = argsort(pop), int(len(pop) * p)
index, pi = argsort(fpop), int(len(fpop) * p)
ppop = pop[index[:pi]]
pb = [1.0 / len(ppop) for i in range(pi)] if len(ppop) > 1 else None
rp = rnd.choice(pi, 1, replace=not len(ppop) >= 1, p=pb)
# Get union population and archive index
apop = concatenate((pop, arc)) if arc is not None else pop
pb = [1.0 / (len(apop) - 1) if i != ic else 0 for i in range(len(apop))] if len(apop) > 1 else None
ra = rnd.choice(len(apop), 1, replace=not len(apop) >= 1, p=pb)
# Generate new positoin
# Generate new position
j = rnd.randint(len(pop[ic]))
x = [pop[ic][i] + f * (ppop[rp[0]][i] - pop[ic][i]) + f * (pop[r[0]][i] - apop[ra[0]][i]) if rnd.rand() < cr or i == j else pop[ic][i] for i in range(len(pop[ic]))]
return asarray(x)
x = [el + f * (ppop[rp[0]][elidx] - el) + f * (pop[r[0]][elidx] - apop[ra[0]][elidx]) if rnd.rand() < cr or elidx == j else el for elidx, el in enumerate(pop[ic])]
return vstack(x)

class AdaptiveArchiveDifferentialEvolution(DifferentialEvolution):
r"""Implementation of Adaptive Differential Evolution With Optional External Archive algorithm.
Expand Down
2 changes: 1 addition & 1 deletion NiaPy/algorithms/other/aso.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ def runIteration(self, task, X, X_f, xb, fxb, Xpb, Xpb_f, alpha, gamma, theta, r
"""
Xin = [self.getBestNeighbors(i, X, X_f, rs) for i in range(len(X))]
MP_c, MP_s, MP_p = asarray([self.FI(X_f[i], Xpb_f[i], fxb, alpha[i]) for i in range(len(X))]), asarray([self.EI(X_f[i], X_f[Xin[i]], gamma[i]) for i in range(len(X))]), asarray([self.II(X_f[i], Xpb_f[i], theta[i]) for i in range(len(X))])
Xtmp = asarray([self.Combination(X[i], Xpb[i], xb, X[self.randint(len(X), skip=[i])], MP_c[i], MP_s[i], MP_p[i], self.F, self.CR, task, self.Rand) for i in range(len(X))])
Xtmp = asarray([self.Combination(X[i], Xpb[i], xb, X[self.randint(len(X), skip=[i])], MP_c[i], MP_s[i], MP_p[i], self.F, self.CR, task, self.Rand) for i in range(len(X))], dtype=object)
X, X_f = asarray([Xtmp[i][0] for i in range(len(X))]), asarray([Xtmp[i][1] for i in range(len(X))])
Xpb, Xpb_f = self.uBestAndPBest(X, X_f, Xpb, Xpb_f)
xb, fxb = self.getBest(X, X_f, xb, fxb)
Expand Down
1 change: 1 addition & 0 deletions NiaPy/tests/test_jade.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def test_function_fine(self):
xb, fxb = pop[ib].copy(), fpop[ib]
for i, x in enumerate(pop):
xn = CrossRandCurr2Pbest(pop, i, xb, self.F, self.CR, self.p, apop)
# Check that position array is updated
self.assertFalse(np.array_equal(x, xn))

class JADETestCase(AlgorithmTestCase):
Expand Down