| 1 |  |  | # encoding=utf8 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | """Implementation of various utility functions.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | import logging | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | from numpy import ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |     ndarray, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     asarray, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     full, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     empty, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     where, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     random as rand, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     ceil, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     amin, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     amax | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | logging.basicConfig() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | logger = logging.getLogger("NiaPy.util.utility") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | logger.setLevel("INFO") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | __all__ = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     "limit_repair", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     "limitInversRepair", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     "objects2array", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     "wangRepair", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     "randRepair", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     "fullArray", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     "reflectRepair" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | def limit_repair(x, Lower, Upper, **kwargs): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     r"""Repair solution and put the solution in the random position inside of the bounds of problem. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     Arguments: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             x (numpy.ndarray): Solution to check and repair if needed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             Lower (numpy.ndarray): Lower bounds of search space. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             Upper (numpy.ndarray): Upper bounds of search space. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             kwargs (Dict[str, Any]): Additional arguments. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     Returns: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             numpy.ndarray: Solution in search space. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     # TODO: Add one-liner np.clip approach | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     ir = where(x < Lower) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     x[ir] = Lower[ir] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     ir = where(x > Upper) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     x[ir] = Upper[ir] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     return x | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | def limitInversRepair(x, Lower, Upper, **kwargs): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     r"""Repair solution and put the solution in the random position inside of the bounds of problem. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     Arguments: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             x (numpy.ndarray): Solution to check and repair if needed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             Lower (numpy.ndarray): Lower bounds of search space. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             Upper (numpy.ndarray): Upper bounds of search space. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             kwargs (Dict[str, Any]): Additional arguments. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     Returns: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             numpy.ndarray: Solution in search space. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     ir = where(x < Lower) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     x[ir] = Upper[ir] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     ir = where(x > Upper) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     x[ir] = Lower[ir] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     return x | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | def wangRepair(x, Lower, Upper, **kwargs): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     r"""Repair solution and put the solution in the random position inside of the bounds of problem. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     Arguments: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             x (numpy.ndarray): Solution to check and repair if needed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             Lower (numpy.ndarray): Lower bounds of search space. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             Upper (numpy.ndarray): Upper bounds of search space. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             kwargs (Dict[str, Any]): Additional arguments. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     Returns: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             numpy.ndarray: Solution in search space. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     ir = where(x < Lower) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     x[ir] = amin([Upper[ir], 2 * Lower[ir] - x[ir]], axis=0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     ir = where(x > Upper) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     x[ir] = amax([Lower[ir], 2 * Upper[ir] - x[ir]], axis=0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     return x | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  | def randRepair(x, Lower, Upper, rnd=rand, **kwargs): | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |     r"""Repair solution and put the solution in the random position inside of the bounds of problem. | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |     Arguments: | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |             x (numpy.ndarray): Solution to check and repair if needed. | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |             Lower (numpy.ndarray): Lower bounds of search space. | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |             Upper (numpy.ndarray): Upper bounds of search space. | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |             rnd (mtrand.RandomState): Random generator. | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |             kwargs (Dict[str, Any]): Additional arguments. | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |     Returns: | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |             numpy.ndarray: Fixed solution. | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |     """ | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |     ir = where(x < Lower) | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |     x[ir] = rnd.uniform(Lower[ir], Upper[ir]) | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |     ir = where(x > Upper) | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |     x[ir] = rnd.uniform(Lower[ir], Upper[ir]) | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |     return x | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | def reflectRepair(x, Lower, Upper, **kwargs): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     r"""Repair solution and put the solution in search space with reflection of how much the solution violates a bound. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     Args: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             x (numpy.ndarray): Solution to be fixed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             Lower (numpy.ndarray): Lower bounds of search space. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             Upper (numpy.ndarray): Upper bounds of search space. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             kwargs (Dict[str, Any]): Additional arguments. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     Returns: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             numpy.ndarray: Fix solution. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     ir = where(x > Upper) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     x[ir] = Lower[ir] + x[ir] % (Upper[ir] - Lower[ir]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     ir = where(x < Lower) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     x[ir] = Lower[ir] + x[ir] % (Upper[ir] - Lower[ir]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     return x | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | def fullArray(a, D): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     r"""Fill or create array of length D, from value or value form a. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     Arguments: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         a (Union[int, float, numpy.ndarray], Iterable[Any]): Input values for fill. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         D (int): Length of new array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     Returns: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         numpy.ndarray: Array filled with passed values or value. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     A = [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     if isinstance(a, (int, float)): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         A = full(D, a) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     elif isinstance(a, (ndarray, list, tuple)): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         if len(a) == D: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |             A = a if isinstance(a, ndarray) else asarray(a) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         elif len(a) > D: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |             A = a[:D] if isinstance(a, ndarray) else asarray(a[:D]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |             for i in range(int(ceil(float(D) / len(a)))): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |                 A.extend(a[:D if (D - i * len(a)) >= len(a) else D - i * len(a)]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |             A = asarray(A) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     return A | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  | def objects2array(objs): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |     r"""Convert `Iterable` array or list to `NumPy` array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |     Args: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |         objs (Iterable[Any]): Array or list to convert. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |     Returns: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |         numpy.ndarray: Array of objects. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |     a = empty(len(objs), dtype=object) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |     for i, e in enumerate(objs): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |         a[i] = e | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 184 |  |  |     return a | 
            
                                                        
            
                                    
            
            
                | 185 |  |  |  |