Conditions | 13 |
Total Lines | 64 |
Code Lines | 42 |
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 gradient_free_optimizers.optimizers.local_opt.downhill_simplex.DownhillSimplexOptimizer.evaluate() 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 | # Author: Simon Blanke |
||
150 | @HillClimbingOptimizer.track_new_score |
||
151 | def evaluate(self, score_new): |
||
152 | if self.simplex_step != 0: |
||
153 | self.prev_pos = self.positions_valid[-1] |
||
154 | |||
155 | if self.simplex_step == 1: |
||
156 | # self.r_pos = self.prev_pos |
||
157 | self.r_score = score_new |
||
158 | |||
159 | if self.r_score > self.simplex_scores[0]: |
||
160 | self.simplex_step = 2 |
||
161 | |||
162 | elif self.r_score > self.simplex_scores[-2]: |
||
163 | # if r is better than x N-1 |
||
164 | self.simplex_pos[-1] = self.r_pos |
||
165 | self.simplex_scores[-1] = self.r_score |
||
166 | self.simplex_step = 1 |
||
167 | |||
168 | if self.simplex_scores[-1] > self.r_score: |
||
169 | self.h_pos = self.simplex_pos[-1] |
||
170 | self.h_score = self.simplex_scores[-1] |
||
171 | else: |
||
172 | self.h_pos = self.r_pos |
||
173 | self.h_score = self.r_score |
||
174 | |||
175 | self.simplex_step = 3 |
||
176 | |||
177 | elif self.simplex_step == 2: |
||
178 | self.e_score = score_new |
||
179 | |||
180 | if self.e_score > self.r_score: |
||
181 | self.simplex_scores[-1] = self.e_pos |
||
182 | elif self.r_score > self.e_score: |
||
183 | self.simplex_scores[-1] = self.r_pos |
||
184 | else: |
||
185 | self.simplex_scores[-1] = random.choice( |
||
186 | [self.e_pos, self.r_pos] |
||
187 | )[0] |
||
188 | |||
189 | elif self.simplex_step == 3: |
||
190 | # eval Contraction |
||
191 | self.c_pos = self.prev_pos |
||
192 | self.c_score = score_new |
||
193 | |||
194 | if self.c_score > self.simplex_scores[-1]: |
||
195 | self.simplex_scores[-1] = self.c_score |
||
196 | self.simplex_pos[-1] = self.c_pos |
||
197 | |||
198 | self.simplex_step = 1 |
||
199 | |||
200 | else: |
||
201 | # start Shrink |
||
202 | self.simplex_step = 4 |
||
203 | self.compress_idx = 0 |
||
204 | |||
205 | elif self.simplex_step == 4: |
||
206 | # eval Shrink |
||
207 | self.simplex_scores[self.compress_idx] = score_new |
||
208 | self.simplex_pos[self.compress_idx] = self.prev_pos |
||
209 | |||
210 | self.compress_idx += 1 |
||
211 | |||
212 | if self.compress_idx == self.n_simp_positions: |
||
213 | self.simplex_step = 1 |
||
214 |