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