| Conditions | 19 |
| Total Lines | 90 |
| Code Lines | 61 |
| 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:
Complex classes like NiaPy.algorithms.basic.hho.HarrisHawksOptimization.runIteration() 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 | # encoding=utf8 |
||
| 132 | def runIteration(self, task, Sol, Fitness, xb, fxb, **dparams): |
||
| 133 | r"""Core function of Harris Hawks Optimization. |
||
| 134 | |||
| 135 | Parameters: |
||
| 136 | task (Task): Optimization task. |
||
| 137 | Sol (numpy.ndarray): Current population |
||
| 138 | Fitness (numpy.ndarray[float]): Current population fitness/funciton values |
||
| 139 | xb (numpy.ndarray): Current best individual |
||
| 140 | fxb (float): Current best individual function/fitness value |
||
| 141 | dparams (Dict[str, Any]): Additional algorithm arguments |
||
| 142 | |||
| 143 | Returns: |
||
| 144 | Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray, float, Dict[str, Any]]: |
||
| 145 | 1. New population |
||
| 146 | 2. New population fitness/function vlues |
||
| 147 | 3. New global best solution |
||
| 148 | 4. New global best fitness/objective value |
||
| 149 | """ |
||
| 150 | # Decreasing energy factor |
||
| 151 | # DEBUG |
||
| 152 | rnd = self.Rand |
||
| 153 | decreasing_energy_factor = 2 * (1 - task.iters() / task.nGEN) |
||
| 154 | mean_sol = np.mean(Sol) |
||
| 155 | # Update population |
||
| 156 | for i in range(self.NP): |
||
| 157 | jumping_energy = rnd.uniform(0, 2) |
||
| 158 | decreasing_energy_random = rnd.uniform(-1, 1) |
||
| 159 | escaping_energy = decreasing_energy_factor * decreasing_energy_random |
||
| 160 | escaping_energy_abs = np.abs(escaping_energy) |
||
| 161 | random_number = rnd.rand() |
||
| 162 | fx = Fitness[i] |
||
| 163 | if escaping_energy >= 1 and random_number >= 0.5: |
||
| 164 | # 0. Exploration: Random tall tree |
||
| 165 | rhi = rnd.randint(0, self.NP) |
||
| 166 | random_agent = Sol[rhi] |
||
| 167 | Sol[i] = random_agent - rnd.rand() * \ |
||
| 168 | np.abs(random_agent - 2 * rnd.rand() * \ |
||
| 169 | Sol[i]) |
||
| 170 | elif escaping_energy_abs >= 1 and random_number < 0.5: |
||
| 171 | # 1. Exploration: Family members mean |
||
| 172 | Sol[i] = \ |
||
| 173 | (xb - mean_sol) - \ |
||
| 174 | rnd.rand() * \ |
||
| 175 | rnd.uniform(task.Lower, task.Upper) |
||
| 176 | elif escaping_energy_abs >= 0.5 and random_number >= 0.5: |
||
| 177 | # 2. Exploitation: Soft besiege |
||
| 178 | Sol[i] = \ |
||
| 179 | (xb - Sol[i]) - \ |
||
| 180 | escaping_energy * \ |
||
| 181 | np.abs(jumping_energy * xb - Sol[i]) |
||
| 182 | elif escaping_energy_abs < 0.5 and random_number >= 0.5: |
||
| 183 | # 3. Exploitation: Hard besiege |
||
| 184 | Sol[i] = \ |
||
| 185 | xb - \ |
||
| 186 | escaping_energy * \ |
||
| 187 | np.abs(xb - Sol[i]) |
||
| 188 | elif escaping_energy_abs >= 0.5 and random_number < 0.5: |
||
| 189 | # 4. Exploitation: Soft besiege with pprogressive rapid dives |
||
| 190 | cand1 = task.repair(xb - escaping_energy * \ |
||
| 191 | np.abs(jumping_energy * xb - Sol[i]), rnd=self.Rand) |
||
| 192 | random_vector = rnd.rand(task.D) |
||
| 193 | cand2 = task.repair(cand1 + random_vector * \ |
||
| 194 | levy_function(task.D, self.levy), rnd=self.Rand) |
||
| 195 | if task.eval(cand1) < Fitness[i]: |
||
| 196 | Sol[i] = cand1 |
||
| 197 | elif task.eval(cand2) < Fitness[i]: |
||
| 198 | Sol[i] = cand2 |
||
| 199 | elif escaping_energy_abs < 0.5 and random_number < 0.5: |
||
| 200 | # 5. Exploitation: Hard besiege with pprogressive rapid dives |
||
| 201 | cand1 = task.repair(xb - escaping_energy * \ |
||
| 202 | np.abs(jumping_energy * xb - mean_sol), rnd=self.Rand) |
||
| 203 | random_vector = rnd.rand(task.D) |
||
| 204 | cand2 = task.repair(cand1 + random_vector * \ |
||
| 205 | levy_function(task.D, self.levy), rnd=self.Rand) |
||
| 206 | if task.eval(cand1) < Fitness[i]: |
||
| 207 | Sol[i] = cand1 |
||
| 208 | elif task.eval(cand2) < Fitness[i]: |
||
| 209 | Sol[i] = cand2 |
||
| 210 | # Repair agent (from population) values |
||
| 211 | Sol[i] = task.repair(Sol[i], rnd=self.Rand) |
||
| 212 | # Eval population |
||
| 213 | Fitness[i] = task.eval(Sol[i]) |
||
| 214 | # Get best of population |
||
| 215 | best_index = np.argmin(Fitness) |
||
| 216 | xb_cand = Sol[best_index].copy() |
||
| 217 | fxb_cand = Fitness[best_index].copy() |
||
| 218 | if fxb_cand < fxb: |
||
| 219 | fxb = fxb_cand |
||
| 220 | xb = xb_cand.copy() |
||
| 221 | return Sol, Fitness, xb, fxb, {} |
||
| 222 | |||
| 224 |