| 1 |  |  | # encoding=utf8 | 
            
                                                                        
                            
            
                                    
            
            
                | 2 |  |  | import logging | 
            
                                                                        
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 4 |  |  | from numpy import full | 
            
                                                                        
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 6 |  |  | from NiaPy.algorithms.algorithm import Algorithm | 
            
                                                                        
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 8 |  |  | logging.basicConfig() | 
            
                                                                        
                            
            
                                    
            
            
                | 9 |  |  | logger = logging.getLogger('NiaPy.algorithms.modified') | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  | logger.setLevel('INFO') | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  | __all__ = ['ParameterFreeBatAlgorithm'] | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  | class ParameterFreeBatAlgorithm(Algorithm): | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  | 	r"""Implementation of Parameter-free Bat algorithm. | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  | 	Algorithm: | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  | 		Parameter-free Bat algorithm | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  | 	Date: | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  | 		2020 | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  | 	Authors: | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  | 		Iztok Fister Jr. | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  | 		This implementation is based on the implementation of basic BA from NiaPy | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  | 	License: | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  | 		MIT | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  | 	Reference paper: | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  | 		Iztok Fister Jr., Iztok Fister, Xin-She Yang. Towards the development of a parameter-free bat algorithm . In: FISTER Jr., Iztok (Ed.), BRODNIK, Andrej (Ed.). StuCoSReC : proceedings of the 2015 2nd Student Computer Science Research Conference. Koper: University of Primorska, 2015, pp. 31-34. | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  | 	Attributes: | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  | 		Name (List[str]): List of strings representing algorithm name. | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  | 	See Also: | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  | 		* :class:`NiaPy.algorithms.Algorithm` | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  | 	""" | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  | 	Name = ['ParameterFreeBatAlgorithm', 'PLBA'] | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  | 	@staticmethod | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  | 	def algorithmInfo(): | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  | 		r"""Get algorithms information. | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  | 		Returns: | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  | 			str: Algorithm information. | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  | 		See Also: | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  | 			* :func:`NiaPy.algorithms.Algorithm.algorithmInfo` | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  | 		""" | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  | 		return r"""Iztok Fister Jr., Iztok Fister, Xin-She Yang. Towards the development of a parameter-free bat algorithm . In: FISTER, Iztok (Ed.), BRODNIK, Andrej (Ed.). StuCoSReC : proceedings of the 2015 2nd Student Computer Science Research Conference. Koper: University of Primorska, 2015, pp. 31-34.""" | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  | 	def setParameters(self, **ukwargs): | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  | 		r"""Set the parameters of the algorithm. | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  | 		Args: | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  | 			A (Optional[float]): Loudness. | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  | 			r (Optional[float]): Pulse rate. | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  | 		See Also: | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  | 			* :func:`NiaPy.algorithms.Algorithm.setParameters` | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  | 		""" | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  | 		Algorithm.setParameters(self, NP=80, **ukwargs) | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  | 		self.A, self.r = 0.9, 0.1 | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 65 |  | View Code Duplication | 	def initPopulation(self, task): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  | 		r"""Initialize the initial population. | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  | 		Parameters: | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  | 			task (Task): Optimization task | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  | 		Returns: | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  | 			Tuple[numpy.ndarray, numpy.ndarray[float], Dict[str, Any]]: | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  | 				1. New population. | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  | 				2. New population fitness/function values. | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  | 				3. Additional arguments: | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  | 					* S (numpy.ndarray): Solutions | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  | 					* Q (numpy.ndarray[float]): Frequencies | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  | 					* v (numpy.ndarray[float]): Velocities | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  | 		See Also: | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  | 			* :func:`NiaPy.algorithms.Algorithm.initPopulation` | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  | 		""" | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  | 		Sol, Fitness, d = Algorithm.initPopulation(self, task) | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  | 		S, Q, v = full([self.NP, task.D], 0.0), full(self.NP, 0.0), full([self.NP, task.D], 0.0) | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  | 		d.update({'S': S, 'Q': Q, 'v': v}) | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  | 		return Sol, Fitness, d | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  | 	def localSearch(self, best, task, **kwargs): | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  | 		r"""Improve the best solution according to the Yang (2010). | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  | 		Args: | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  | 			best (numpy.ndarray): Global best individual. | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  | 			task (Task): Optimization task. | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  | 			**kwargs (Dict[str, Any]): Additional arguments. | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  | 		Returns: | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  | 			numpy.ndarray: New solution based on global best individual. | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  | 		""" | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  | 		return task.repair(best + 0.001 * self.normal(0, 1, task.D)) | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  | 	def runIteration(self, task, Sol, Fitness, xb, fxb, S, Q, v, **dparams): | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  | 		r"""Core function of Parameter-free Bat Algorithm. | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  | 		Parameters: | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  | 			task (Task): Optimization task. | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  | 			Sol (numpy.ndarray): Current population | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  | 			Fitness (numpy.ndarray[float]): Current population fitness/funciton values | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  | 			best (numpy.ndarray): Current best individual | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  | 			f_min (float): Current best individual function/fitness value | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  | 			S (numpy.ndarray): Solutions | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  | 			Q (numpy.ndarray): Frequencies | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  | 			v (numpy.ndarray): Velocities | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  | 			best (numpy.ndarray): Global best used by the algorithm | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  | 			f_min (float): Global best fitness value used by the algorithm | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  | 			dparams (Dict[str, Any]): Additional algorithm arguments | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  | 		Returns: | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  | 			Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray, float, Dict[str, Any]]: | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  | 				1. New population | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  | 				2. New population fitness/function vlues | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  | 				3. New global best solution | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  | 				4. New global best fitness/objective value | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  | 				5. Additional arguments: | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  | 					* S (numpy.ndarray): Solutions | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  | 					* Q (numpy.ndarray): Frequencies | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  | 					* v (numpy.ndarray): Velocities | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  | 					* best (numpy.ndarray): Global best | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  | 					* f_min (float): Global best fitness | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  | 		""" | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  | 		upper, lower = task.bcUpper(), task.bcLower() | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  | 		for i in range(self.NP): | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  | 			Q[i] = ((upper[0] - lower[0]) / float(self.NP)) * self.normal(0, 1) | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  | 			v[i] += (Sol[i] - xb) * Q[i] | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  | 			if self.rand() > self.r: S[i] = self.localSearch(best=xb, task=task, i=i, Sol=Sol) | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  | 			else: S[i] = task.repair(Sol[i] + v[i], rnd=self.Rand) | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  | 			Fnew = task.eval(S[i]) | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  | 			if (Fnew <= Fitness[i]) and (self.rand() < self.A): Sol[i], Fitness[i] = S[i], Fnew | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  | 			if Fnew <= fxb: xb, fxb = S[i].copy(), Fnew | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  | 		return Sol, Fitness, xb, fxb, {'S': S, 'Q': Q, 'v': v} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 141 |  |  | # vim: tabstop=3 noexpandtab shiftwidth=3 softtabstop=3 | 
            
                                                        
            
                                    
            
            
                | 142 |  |  |  |