| Total Complexity | 2 |
| Total Lines | 58 |
| Duplicated Lines | 53.45 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | # Author: Simon Blanke |
||
| 2 | # Email: [email protected] |
||
| 3 | # License: MIT License |
||
| 4 | |||
| 5 | |||
| 6 | from ..local_opt import HillClimbingOptimizer |
||
| 7 | |||
| 8 | |||
| 9 | class RandomAnnealingOptimizer(HillClimbingOptimizer): |
||
| 10 | name = "Random Annealing" |
||
| 11 | _name_ = "random_annealing" |
||
| 12 | __name__ = "RandomAnnealingOptimizer" |
||
| 13 | |||
| 14 | View Code Duplication | def __init__( |
|
|
|
|||
| 15 | self, |
||
| 16 | search_space, |
||
| 17 | initialize={"grid": 4, "random": 2, "vertices": 4}, |
||
| 18 | constraints=[], |
||
| 19 | random_state=None, |
||
| 20 | rand_rest_p=0, |
||
| 21 | nth_process=None, |
||
| 22 | epsilon=0.03, |
||
| 23 | distribution="normal", |
||
| 24 | n_neighbours=3, |
||
| 25 | annealing_rate=0.98, |
||
| 26 | start_temp=10, |
||
| 27 | ): |
||
| 28 | super().__init__( |
||
| 29 | search_space=search_space, |
||
| 30 | initialize=initialize, |
||
| 31 | constraints=constraints, |
||
| 32 | random_state=random_state, |
||
| 33 | rand_rest_p=rand_rest_p, |
||
| 34 | nth_process=nth_process, |
||
| 35 | epsilon=epsilon, |
||
| 36 | distribution=distribution, |
||
| 37 | n_neighbours=n_neighbours, |
||
| 38 | ) |
||
| 39 | self.epsilon = epsilon |
||
| 40 | self.distribution = distribution |
||
| 41 | self.n_neighbours = n_neighbours |
||
| 42 | self.annealing_rate = annealing_rate |
||
| 43 | self.start_temp = start_temp |
||
| 44 | self.temp = start_temp |
||
| 45 | |||
| 46 | @HillClimbingOptimizer.track_new_pos |
||
| 47 | @HillClimbingOptimizer.random_iteration |
||
| 48 | def iterate(self): |
||
| 49 | pos = self.move_climb( |
||
| 50 | self.pos_current, |
||
| 51 | epsilon=self.epsilon, |
||
| 52 | distribution=self.distribution, |
||
| 53 | epsilon_mod=self.temp, |
||
| 54 | ) |
||
| 55 | self.temp = self.temp * self.annealing_rate |
||
| 56 | |||
| 57 | return pos |
||
| 58 |