| Conditions | 12 |
| Total Lines | 64 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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:
Complex classes like GreyWolfOptimizer.move() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | """Grey wolf optimizer. |
||
| 63 | def move(self): |
||
| 64 | |||
| 65 | self.initialization() |
||
| 66 | |||
| 67 | while True: |
||
| 68 | if self.evaluations == self.nFES: |
||
| 69 | break |
||
| 70 | |||
| 71 | for i in range(self.NP): |
||
| 72 | self.Positions[i] = self.bounds(self.Positions[i]) |
||
| 73 | |||
| 74 | Fit = self.Fun(self.D, self.Positions[i]) |
||
| 75 | self.evaluations = self.evaluations + 1 |
||
| 76 | |||
| 77 | if Fit < self.Alpha_score: |
||
| 78 | self.Alpha_score = Fit |
||
| 79 | self.Alpha_pos = self.Positions[i] |
||
| 80 | |||
| 81 | if ((Fit > self.Alpha_score) and (Fit < self.Beta_score)): |
||
| 82 | self.Beta_score = Fit |
||
| 83 | self.Beta_pos = self.Positions[i] |
||
| 84 | |||
| 85 | if ((Fit > self.Alpha_score) and (Fit > self.Beta_score) and |
||
| 86 | (Fit < self.Delta_score)): |
||
| 87 | self.Delta_score = Fit |
||
| 88 | self.Delta_pos = self.Positions[i] |
||
| 89 | |||
| 90 | a = 2 - self.evaluations * ((2) / self.nFES) |
||
| 91 | |||
| 92 | for i in range(self.NP): |
||
| 93 | for j in range(self.D): |
||
| 94 | |||
| 95 | r1 = random.random() |
||
| 96 | r2 = random.random() |
||
| 97 | |||
| 98 | A1 = 2 * a * r1 - a |
||
| 99 | C1 = 2 * r2 |
||
| 100 | |||
| 101 | D_alpha = abs( |
||
| 102 | C1 * self.Alpha_pos[j] - self.Positions[i][j]) |
||
| 103 | X1 = self.Alpha_pos[j] - A1 * D_alpha |
||
| 104 | |||
| 105 | r1 = random.random() |
||
| 106 | r2 = random.random() |
||
| 107 | |||
| 108 | A2 = 2 * a * r1 - a |
||
| 109 | C2 = 2 * r2 |
||
| 110 | |||
| 111 | D_beta = abs(C2 * self.Beta_pos[j] - self.Positions[i][j]) |
||
| 112 | X2 = self.Beta_pos[j] - A2 * D_beta |
||
| 113 | |||
| 114 | r1 = random.random() |
||
| 115 | r2 = random.random() |
||
| 116 | |||
| 117 | A3 = 2 * a * r1 - a |
||
| 118 | C3 = 2 * r2 |
||
| 119 | |||
| 120 | D_delta = abs( |
||
| 121 | C3 * self.Delta_pos[j] - self.Positions[i][j]) |
||
| 122 | X3 = self.Delta_pos[j] - A3 * D_delta |
||
| 123 | |||
| 124 | self.Positions[i][j] = (X1 + X2 + X3) / 3 |
||
| 125 | |||
| 126 | return self.Alpha_score |
||
| 127 |