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