| Conditions | 7 |
| Total Lines | 59 |
| Code Lines | 39 |
| 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:
| 1 | # Author: Simon Blanke |
||
| 89 | @HillClimbingOptimizer.track_new_pos |
||
| 90 | def iterate(self): |
||
| 91 | simplex_stale = all( |
||
| 92 | [ |
||
| 93 | np.array_equal(self.simplex_pos[0], array) |
||
| 94 | for array in self.simplex_pos |
||
| 95 | ] |
||
| 96 | ) |
||
| 97 | |||
| 98 | if simplex_stale: |
||
| 99 | idx_sorted = sort_list_idx(self.scores_valid) |
||
| 100 | self.simplex_pos = [self.positions_valid[idx] for idx in idx_sorted] |
||
| 101 | self.simplex_scores = [self.scores_valid[idx] for idx in idx_sorted] |
||
| 102 | |||
| 103 | self.simplex_step = 1 |
||
| 104 | |||
| 105 | if self.simplex_step == 1: |
||
| 106 | idx_sorted = sort_list_idx(self.simplex_scores) |
||
| 107 | self.simplex_pos = [self.simplex_pos[idx] for idx in idx_sorted] |
||
| 108 | self.simplex_scores = [ |
||
| 109 | self.simplex_scores[idx] for idx in idx_sorted |
||
| 110 | ] |
||
| 111 | |||
| 112 | self.center_array = centeroid(self.simplex_pos[:-1]) |
||
| 113 | |||
| 114 | r_pos = self.center_array + self.alpha * ( |
||
| 115 | self.center_array - self.simplex_pos[-1] |
||
| 116 | ) |
||
| 117 | self.r_pos = self.conv2pos(r_pos) |
||
| 118 | pos_new = self.r_pos |
||
| 119 | |||
| 120 | elif self.simplex_step == 2: |
||
| 121 | e_pos = self.center_array + self.gamma * ( |
||
| 122 | self.center_array - self.simplex_pos[-1] |
||
| 123 | ) |
||
| 124 | self.e_pos = self.conv2pos(e_pos) |
||
| 125 | self.simplex_step = 1 |
||
| 126 | |||
| 127 | pos_new = self.e_pos |
||
| 128 | |||
| 129 | elif self.simplex_step == 3: |
||
| 130 | # iter Contraction |
||
| 131 | c_pos = self.h_pos + self.beta * (self.center_array - self.h_pos) |
||
| 132 | c_pos = self.conv2pos(c_pos) |
||
| 133 | |||
| 134 | pos_new = c_pos |
||
| 135 | |||
| 136 | elif self.simplex_step == 4: |
||
| 137 | # iter Shrink |
||
| 138 | pos = self.simplex_pos[self.compress_idx] |
||
| 139 | pos = pos + self.sigma * (self.simplex_pos[0] - pos) |
||
| 140 | |||
| 141 | pos_new = self.conv2pos(pos) |
||
| 142 | |||
| 143 | if self.conv.not_in_constraint(pos_new): |
||
| 144 | return pos_new |
||
| 145 | |||
| 146 | return self.move_climb( |
||
| 147 | pos_new, epsilon=self.epsilon, distribution=self.distribution |
||
| 148 | ) |
||
| 214 |