| 1 |  |  | # encoding=utf8 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | import logging | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | import numpy as np | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | from NiaPy.algorithms.algorithm import Algorithm | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | logging.basicConfig() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | logger = logging.getLogger('NiaPy.algorithms.other') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | logger.setLevel('INFO') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | __all__ = ['RandomSearch'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class RandomSearch(Algorithm): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 	r"""Implementation of a simple Random Algorithm. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	Algorithm: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 		Random Search | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	Date: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 		11.10.2020 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	Authors: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 		Iztok Fister Jr., Grega Vrbančič | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	License: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 		MIT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	Reference URL: https://en.wikipedia.org/wiki/Random_search | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	Attributes: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 		Name (List[str]): List of strings representing algorithm name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	See Also: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 		* :class:`NiaPy.algorithms.Algorithm` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	Name = ['RandomSearch', 'RS'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	@staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	def algorithmInfo(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 		r"""Get basic information of algorithm. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 		Returns: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 			str: Basic information of algorithm. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 		See Also: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 			* :func:`NiaPy.algorithms.Algorithm.algorithmInfo` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 		""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 		return r"""None""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	def setParameters(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		r"""Set the algorithm parameters/arguments. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		Arguments: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 		See Also | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 			* :func:`NiaPy.algorithms.Algorithm.setParameters` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 		Algorithm.setParameters(self, NP=1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	def getParameters(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 		r"""Get algorithms parametes values. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 		Returns: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 			Dict[str, Any]: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 		See Also | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 			* :func:`NiaPy.algorithms.Algorithm.getParameters` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		d = Algorithm.getParameters(self) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		return d | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	def initPopulation(self, task): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 		r"""Initialize the starting population. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 		Args: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 			task (Task): Optimization task. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 		Returns: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 			Tuple[numpy.ndarray, float, dict]: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 			1. Initial solution | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 			2. Initial solutions fitness/objective value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 			3. Additional arguments | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 		""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 		total_candidates = 0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 		if task.nGEN or task.nFES: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 			total_candidates = task.nGEN if task.nGEN else task.nFES | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		self.candidates = [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 		for i in range(total_candidates): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 			while True: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 				x = task.Lower + task.bcRange() * self.rand(task.D) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 				if not np.any([np.all(a == x) for a in self.candidates]): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 					self.candidates.append(x) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 					break | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 		xfit = task.eval(self.candidates[0]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		return x, xfit, {} | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 97 |  |  | 	def runIteration(self, task, x, xfit, xb, fxb, **dparams): | 
            
                                                        
            
                                    
            
            
                | 98 |  |  | 		r"""Core function of the algorithm. | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 100 |  |  | 		Args: | 
            
                                                        
            
                                    
            
            
                | 101 |  |  | 			task (Task): | 
            
                                                        
            
                                    
            
            
                | 102 |  |  | 			x (numpy.ndarray): | 
            
                                                        
            
                                    
            
            
                | 103 |  |  | 			xfit (float): | 
            
                                                        
            
                                    
            
            
                | 104 |  |  | 			xb (numpy.ndarray): | 
            
                                                        
            
                                    
            
            
                | 105 |  |  | 			fxb (float): | 
            
                                                        
            
                                    
            
            
                | 106 |  |  | 			**dparams (dict): Additional arguments. | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 108 |  |  | 		Returns: | 
            
                                                        
            
                                    
            
            
                | 109 |  |  | 			Tuple[numpy.ndarray, float, numpy.ndarray, float, dict]: | 
            
                                                        
            
                                    
            
            
                | 110 |  |  | 			1. New solution | 
            
                                                        
            
                                    
            
            
                | 111 |  |  | 			2. New solutions fitness/objective value | 
            
                                                        
            
                                    
            
            
                | 112 |  |  | 			3. New global best solution | 
            
                                                        
            
                                    
            
            
                | 113 |  |  | 			4. New global best solutions fitness/objective value | 
            
                                                        
            
                                    
            
            
                | 114 |  |  | 			5. Additional arguments | 
            
                                                        
            
                                    
            
            
                | 115 |  |  | 		""" | 
            
                                                        
            
                                    
            
            
                | 116 |  |  | 		current_candidate = task.Evals if task.Evals else task.Iters | 
            
                                                        
            
                                    
            
            
                | 117 |  |  | 		x = self.candidates[current_candidate] | 
            
                                                        
            
                                    
            
            
                | 118 |  |  | 		xfit = task.eval(x) | 
            
                                                        
            
                                    
            
            
                | 119 |  |  | 		xb, fxb = self.getBest(x, xfit, xb, fxb) | 
            
                                                        
            
                                    
            
            
                | 120 |  |  | 		return x, xfit, xb, fxb, {} | 
            
                                                        
            
                                    
            
            
                | 121 |  |  |  |