Passed
Pull Request — master (#233)
by Grega
01:22
created

AdaptiveArchiveDifferentialEvolution.setParameters()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 2
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 2
dl 2
loc 2
rs 10
c 0
b 0
f 0
1
# encoding=utf8
2
# pylint: disable=mixed-indentation, multiple-statements, logging-not-lazy, attribute-defined-outside-init, line-too-long, arguments-differ, singleton-comparison, bad-continuation, dangerous-default-value, consider-using-enumerate, unused-argument, keyword-arg-before-vararg
3
import logging
4
5
from numpy import random as rand, concatenate, asarray, argsort
6
7
from NiaPy.algorithms.basic.de import DifferentialEvolution
8
9
logging.basicConfig()
10
logger = logging.getLogger('NiaPy.algorithms.modified')
11
logger.setLevel('INFO')
12
13
__all__ = [
14
	'AdaptiveArchiveDifferentialEvolution',
15
	'CrossRandCurr2Pbest'
16
]
17
18
def CrossRandCurr2Pbest(pop, ic, x_b, f, cr, p=0.2, arc=None, rnd=rand, *args):
19
	r"""Mutation strategy with crossover.
20
21
	Mutation strategy uses two different random individuals from population to perform mutation.
22
23
	Mutation:
24
		Name: DE/curr2pbest/1
25
26
	Args:
27
		pop (numpy.ndarray): Current population.
28
		ic (int): Index of current individual.
29
		x_b (numpy.ndarray): Global best individual.
30
		f (float): Scale factor.
31
		cr (float): Crossover probability.
32
		p (float): Procentage of best individuals to use.
33
		arc (numpy.ndarray): Achived individuals.
34
		rnd (mtrand.RandomState): Random generator.
35
		*args (Dict[str, Any]): Additional argumets.
36
37
	Returns:
38
		numpy.ndarray: New position.
39
	"""
40
	# Get random index from current population
41
	pb = [1 / (len(pop) - 1) if i != ic else 0 for i in range(len(pop))] if len(pop) > 1 else None
42
	r = rnd.choice(len(pop), 1, replace=not len(pop) >= 3, p=pb)
43
	# Get pbest index
44
	index, pi = argsort(pop), int(len(pop) * p)
45
	ppop = pop[index[:pi]]
46
	pb = [1 / len(ppop) for i in range(pi)] if len(ppop) > 1 else None
47
	rp = rnd.choice(pi, 1, replace=not len(ppop) >= 1, p=pb)
48
	# Get union population and archive index
49
	apop = concatenate((pop, arc)) if arc is not None else pop
50
	pb = [1 / (len(apop) - 1) if i != ic else 0 for i in range(len(apop))] if len(apop) > 1 else None
51
	ra = rnd.choice(len(apop), 1, replace=not len(apop) >= 1, p=pb)
52
	# Generate new positoin
53
	j = rnd.randint(len(pop[ic]))
54
	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]))]
55
	return asarray(x)
56
57 View Code Duplication
class AdaptiveArchiveDifferentialEvolution(DifferentialEvolution):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
58
	r"""Implementation of Adaptive Differential Evolution With Optional External Archive algorithm.
59
60
	Algorithm:
61
		Adaptive Differential Evolution With Optional External Archive
62
63
	Date:
64
		2019
65
66
	Author:
67
		Klemen Berkovič
68
69
	License:
70
		MIT
71
72
	Reference URL:
73
		https://ieeexplore.ieee.org/document/5208221
74
75
	Reference paper:
76
		Zhang, Jingqiao, and Arthur C. Sanderson. "JADE: adaptive differential evolution with optional external archive." IEEE Transactions on evolutionary computation 13.5 (2009): 945-958.
77
78
	Attributes:
79
		Name (List[str]): List of strings representing algorithm name.
80
81
	See Also:
82
		:class:`NiaPy.algorithms.basic.DifferentialEvolution`
83
	"""
84
	Name = ['AdaptiveArchiveDifferentialEvolution', 'JADE']
85
86
	@staticmethod
87
	def algorithmInfo():
88
		r"""Get algorithm information.
89
90
		Returns:
91
			str: Alogrithm information.
92
93
		See Also:
94
			:func:`NiaPy.algorithms.algorithm.Algorithm.algorithmInfo`
95
		"""
96
		return r"""Zhang, Jingqiao, and Arthur C. Sanderson. "JADE: adaptive differential evolution with optional external archive." IEEE Transactions on evolutionary computation 13.5 (2009): 945-958."""
97
98
	def setParameters(self, **kwargs):
99
		DifferentialEvolution.setParameters(self, **kwargs)
100
		# TODO add parameters of the algorithm
101
102
	def getParameters(self):
103
		d = DifferentialEvolution.getParameters(self)
104
		# TODO add paramters values
105
		return d
106
107
	def runIteration(self, task, pop, fpop, xb, fxb, **dparams):
108
		# TODO Implement algorithm
109
		return pop, fpop, xb, fxb, dparams
110
111
# vim: tabstop=3 noexpandtab shiftwidth=3 softtabstop=3
112