Conditions | 13 |
Total Lines | 63 |
Code Lines | 40 |
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 |
||
121 | def evaluate(self, score_new): |
||
122 | self.score_new = score_new |
||
123 | |||
124 | if self.simplex_step != 0: |
||
125 | self.prev_pos = self.positions_valid[-1] |
||
126 | |||
127 | if self.simplex_step == 1: |
||
128 | # self.r_pos = self.prev_pos |
||
129 | self.r_score = score_new |
||
130 | |||
131 | if self.r_score > self.simplex_scores[0]: |
||
132 | self.simplex_step = 2 |
||
133 | |||
134 | elif self.r_score > self.simplex_scores[-2]: |
||
135 | # if r is better than x N-1 |
||
136 | self.simplex_pos[-1] = self.r_pos |
||
137 | self.simplex_scores[-1] = self.r_score |
||
138 | self.simplex_step = 1 |
||
139 | |||
140 | if self.simplex_scores[-1] > self.r_score: |
||
141 | self.h_pos = self.simplex_pos[-1] |
||
142 | self.h_score = self.simplex_scores[-1] |
||
143 | else: |
||
144 | self.h_pos = self.r_pos |
||
145 | self.h_score = self.r_score |
||
146 | |||
147 | self.simplex_step = 3 |
||
148 | |||
149 | elif self.simplex_step == 2: |
||
150 | self.e_score = score_new |
||
151 | |||
152 | if self.e_score > self.r_score: |
||
153 | self.simplex_scores[-1] = self.e_pos |
||
154 | elif self.r_score > self.e_score: |
||
155 | self.simplex_scores[-1] = self.r_pos |
||
156 | else: |
||
157 | self.simplex_scores[-1] = random.choice([self.e_pos, self.r_pos])[0] |
||
158 | |||
159 | elif self.simplex_step == 3: |
||
160 | # eval Contraction |
||
161 | self.c_pos = self.prev_pos |
||
162 | self.c_score = score_new |
||
163 | |||
164 | if self.c_score > self.simplex_scores[-1]: |
||
165 | self.simplex_scores[-1] = self.c_score |
||
166 | self.simplex_pos[-1] = self.c_pos |
||
167 | |||
168 | self.simplex_step = 1 |
||
169 | |||
170 | else: |
||
171 | # start Shrink |
||
172 | self.simplex_step = 4 |
||
173 | self.compress_idx = 0 |
||
174 | |||
175 | elif self.simplex_step == 4: |
||
176 | # eval Shrink |
||
177 | self.simplex_scores[self.compress_idx] = score_new |
||
178 | self.simplex_pos[self.compress_idx] = self.prev_pos |
||
179 | |||
180 | self.compress_idx += 1 |
||
181 | |||
182 | if self.compress_idx == self.n_simp_positions: |
||
183 | self.simplex_step = 1 |
||
184 |