| Conditions | 1 |
| Total Lines | 54 |
| Code Lines | 51 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | # encoding=utf8 |
||
| 78 | |||
| 79 | Arguments: |
||
| 80 | x (numpy.ndarray): Solution to check and repair if needed. |
||
| 81 | Lower (numpy.ndarray): Lower bounds of search space. |
||
| 82 | Upper (numpy.ndarray): Upper bounds of search space. |
||
| 83 | kwargs (Dict[str, Any]): Additional arguments. |
||
| 84 | |||
| 85 | Returns: |
||
| 86 | numpy.ndarray: Solution in search space. |
||
| 87 | |||
| 88 | """ |
||
| 89 | |||
| 90 | ir = where(x < Lower) |
||
| 91 | x[ir] = amin([Upper[ir], 2 * Lower[ir] - x[ir]], axis=0) |
||
| 92 | ir = where(x > Upper) |
||
| 93 | x[ir] = amax([Lower[ir], 2 * Upper[ir] - x[ir]], axis=0) |
||
| 94 | return x |
||
| 95 | |||
| 96 | |||
| 97 | def randRepair(x, Lower, Upper, rnd=rand, **kwargs): |
||
| 98 | r"""Repair solution and put the solution in the random position inside of the bounds of problem. |
||
| 99 | |||
| 100 | Arguments: |
||
| 101 | x (numpy.ndarray): Solution to check and repair if needed. |
||
| 102 | Lower (numpy.ndarray): Lower bounds of search space. |
||
| 103 | Upper (numpy.ndarray): Upper bounds of search space. |
||
| 104 | rnd (mtrand.RandomState): Random generator. |
||
| 105 | kwargs (Dict[str, Any]): Additional arguments. |
||
| 106 | |||
| 107 | Returns: |
||
| 108 | numpy.ndarray: Fixed solution. |
||
| 109 | |||
| 110 | """ |
||
| 111 | |||
| 112 | ir = where(x < Lower) |
||
| 113 | x[ir] = rnd.uniform(Lower[ir], Upper[ir]) |
||
| 114 | ir = where(x > Upper) |
||
| 115 | x[ir] = rnd.uniform(Lower[ir], Upper[ir]) |
||
| 116 | return x |
||
| 117 | |||
| 118 | |||
| 119 | def reflectRepair(x, Lower, Upper, **kwargs): |
||
| 120 | r"""Repair solution and put the solution in search space with reflection of how much the solution violates a bound. |
||
| 121 | |||
| 122 | Args: |
||
| 123 | x (numpy.ndarray): Solution to be fixed. |
||
| 124 | Lower (numpy.ndarray): Lower bounds of search space. |
||
| 125 | Upper (numpy.ndarray): Upper bounds of search space. |
||
| 126 | kwargs (Dict[str, Any]): Additional arguments. |
||
| 127 | |||
| 128 | Returns: |
||
| 129 | numpy.ndarray: Fix solution. |
||
| 130 | |||
| 131 | """ |
||
| 132 | |||
| 183 |