Code Duplication    Length = 33-33 lines in 2 locations

NiaPy/algorithms/basic/de.py 2 locations

@@ 51-83 (lines=33) @@
48
	x = [pop[r[0]][i] + f * (pop[r[1]][i] - pop[r[2]][i]) if rnd.rand() < cr or i == j else pop[ic][i] for i in range(len(pop[ic]))]
49
	return asarray(x)
50
51
def CrossBest1(pop, ic, x_b, f, cr, rnd=rand, *args):
52
	r"""Mutation strategy with crossover.
53
54
	Mutation strategy uses two different random individuals from population and global best individual.
55
56
	Mutation:
57
		Name: de/best/1
58
59
		:math:`\mathbf{v}_{i, G} = \mathbf{x}_{best, G} + F \cdot (\mathbf{x}_{r_1, G} - \mathbf{x}_{r_2, G})`
60
		where :math:`r_1, r_2` are random indexes representing current population individuals.
61
62
	Crossover:
63
		Name: Binomial crossover
64
65
		:math:`\mathbf{x}_{i, G+1} = \begin{cases} \mathbf{u}_{i, G+1}, & \text{if $f(\mathbf{u}_{i, G+1}) \leq f(\mathbf{x}_{i, G})$}, \\ \mathbf{x}_{i, G}, & \text{otherwise}. \end{cases}`
66
67
	args:
68
		pop (numpy.ndarray[Individual]): Current population.
69
		ic (int): Index of individual being mutated.
70
		x_b (Individual): Current global best individual.
71
		f (float): Scale factor.
72
		cr (float): Crossover probability.
73
		rnd (mtrand.RandomState): Random generator.
74
		args (list): Additional arguments.
75
76
	returns:
77
		numpy.ndarray: Mutated and mixed individual.
78
	"""
79
	j = rnd.randint(len(pop[ic]))
80
	p = [1 / (len(pop) - 1.0) if i != ic else 0 for i in range(len(pop))] if len(pop) > 2 else None
81
	r = rnd.choice(len(pop), 2, replace=not len(pop) >= 2, p=p)
82
	x = [x_b[i] + f * (pop[r[0]][i] - pop[r[1]][i]) if rnd.rand() < cr or i == j else pop[ic][i] for i in range(len(pop[ic]))]
83
	return asarray(x)
84
85
def CrossRand2(pop, ic, x_b, f, cr, rnd=rand, *args):
86
	r"""Mutation strategy with crossover.
@@ 17-49 (lines=33) @@
14
logger = logging.getLogger('NiaPy.algorithms.basic')
15
logger.setLevel('INFO')
16
17
def CrossRand1(pop, ic, x_b, f, cr, rnd=rand, *args):
18
	r"""Mutation strategy with crossover.
19
20
	Mutation strategy uses three different random individuals from population to perform mutation.
21
22
	Mutation:
23
		Name: DE/rand/1
24
25
		:math:`\mathbf{x}_{r_1, G} + F \cdot (\mathbf{x}_{r_2, G} - \mathbf{x}_{r_3, G}`
26
		where :math:`r_1, r_2, r_3` are random indexes representing current population individuals.
27
28
	Crossover:
29
		Name: Binomial crossover
30
31
		:math:`\mathbf{x}_{i, G+1} = \begin{cases} \mathbf{u}_{i, G+1}, & \text{if $f(\mathbf{u}_{i, G+1}) \leq f(\mathbf{x}_{i, G})$}, \\ \mathbf{x}_{i, G}, & \text{otherwise}. \end{cases}`
32
33
	Args:
34
		pop (numpy.ndarray[Individual]): Current population.
35
		ic (int): Index of individual being mutated.
36
		x_b (Individual): Current global best individual.
37
		f (float): Scale factor.
38
		cr (float): Crossover probability.
39
		rnd (mtrand.RandomState): Random generator.
40
		args (list): Additional arguments.
41
42
	Returns:
43
		numpy.ndarray: Mutated and mixed individual.
44
	"""
45
	j = rnd.randint(len(pop[ic]))
46
	p = [1 / (len(pop) - 1.0) if i != ic else 0 for i in range(len(pop))] if len(pop) > 3 else None
47
	r = rnd.choice(len(pop), 3, replace=not len(pop) >= 3, p=p)
48
	x = [pop[r[0]][i] + f * (pop[r[1]][i] - pop[r[2]][i]) if rnd.rand() < cr or i == j else pop[ic][i] for i in range(len(pop[ic]))]
49
	return asarray(x)
50
51
def CrossBest1(pop, ic, x_b, f, cr, rnd=rand, *args):
52
	r"""Mutation strategy with crossover.