Conditions | 11 |
Total Lines | 69 |
Code Lines | 49 |
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.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 |
||
108 | def evaluate(self, score_new): |
||
109 | self.score_new = score_new |
||
110 | |||
111 | if self.simplex_step == 1: |
||
112 | print(" self.simplex_step ", self.simplex_step) |
||
113 | if score_new > self.simplex_scores[0]: |
||
114 | print("eval 1 ") |
||
115 | # if r is better than x0 |
||
116 | self.simplex_pos[-1] = self.r_pos |
||
117 | self.simplex_scores[-1] = score_new |
||
118 | self.simplex_step = 2 |
||
119 | elif score_new > self.simplex_scores[-2]: |
||
120 | print("eval 2 ") |
||
121 | # if r is better than x N-1 |
||
122 | self.simplex_pos[-2] = self.r_pos |
||
123 | self.simplex_scores[-2] = score_new |
||
124 | self.simplex_step = 3 |
||
125 | elif score_new > self.simplex_scores[-1]: |
||
126 | print("eval 3 ") |
||
127 | # if r is better than x N |
||
128 | self.h_pos = self.r_pos |
||
129 | c_pos = self.h_pos + self.beta * (self.center_array - self.h_pos) |
||
130 | self.c_pos = self.conv2pos(c_pos) |
||
131 | |||
132 | self.simplex_step = 4 |
||
133 | else: |
||
134 | print("eval 4 ") |
||
135 | # if r is worse than x N |
||
136 | self.h_pos = self.simplex_pos[-1] |
||
137 | c_pos = self.h_pos + self.beta * (self.center_array - self.h_pos) |
||
138 | self.c_pos = self.conv2pos(c_pos) |
||
139 | |||
140 | self.simplex_step = 4 |
||
141 | |||
142 | elif self.simplex_step == 2: |
||
143 | print(" self.simplex_step ", self.simplex_step) |
||
144 | |||
145 | idx_sorted = sort_list_idx(self.scores_valid[-2:]) |
||
146 | self.simplex_pos[-1] = self.simplex_pos[-2:][idx_sorted][0] |
||
147 | self.simplex_scores[-1] = self.simplex_scores[-2:][idx_sorted][0] |
||
148 | |||
149 | self.simplex_step -= 1 |
||
150 | |||
151 | print(" self.simplex_pos[-1] ", self.simplex_pos[-1]) |
||
152 | |||
153 | elif self.simplex_step == 3: |
||
154 | print(" self.simplex_step ", self.simplex_step) |
||
155 | |||
156 | self.simplex_step = 1 |
||
157 | |||
158 | elif self.simplex_step == 4: |
||
159 | print(" self.simplex_step ", self.simplex_step) |
||
160 | |||
161 | if score_new > self.simplex_scores[-1]: |
||
162 | self.simplex_pos[-1] = self.c_pos |
||
163 | self.simplex_scores[-1] = score_new |
||
164 | self.simplex_step = 1 |
||
165 | else: |
||
166 | self.simplex_step = 5 |
||
167 | self.compress_idx = 0 |
||
168 | |||
169 | elif self.simplex_step == 5: |
||
170 | print(" self.simplex_step ", self.simplex_step) |
||
171 | |||
172 | self.simplex_scores[self.compress_idx] = score_new |
||
173 | self.compress_idx += 1 |
||
174 | |||
175 | if self.compress_idx == self.n_simp_positions: |
||
176 | self.simplex_step = 1 |
||
177 |