| Total Complexity | 4 |
| Total Lines | 37 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | # Author: Simon Blanke |
||
| 2 | # Email: [email protected] |
||
| 3 | # License: MIT License |
||
| 4 | |||
| 5 | |||
| 6 | import numpy as np |
||
| 7 | |||
| 8 | from ..local_opt import StochasticHillClimbingOptimizer |
||
| 9 | from ...search import Search |
||
| 10 | |||
| 11 | |||
| 12 | class SimulatedAnnealingOptimizer(StochasticHillClimbingOptimizer, Search): |
||
| 13 | name = "Simulated Annealing" |
||
| 14 | |||
| 15 | def __init__( |
||
| 16 | self, |
||
| 17 | *args, |
||
| 18 | annealing_rate=0.97, |
||
| 19 | start_temp=1, |
||
| 20 | **kwargs, |
||
| 21 | ): |
||
| 22 | super().__init__(*args, **kwargs) |
||
| 23 | self.annealing_rate = annealing_rate |
||
| 24 | self.start_temp = start_temp |
||
| 25 | self.temp = start_temp |
||
| 26 | |||
| 27 | def _accept_default(self): |
||
| 28 | return np.exp(-self._score_norm_default() / self.temp) |
||
| 29 | |||
| 30 | def _accept_adapt(self): |
||
| 31 | return self._score_norm_adapt() / self.temp |
||
| 32 | |||
| 33 | def evaluate(self, score_new): |
||
| 34 | StochasticHillClimbingOptimizer.evaluate(self, score_new) |
||
| 35 | |||
| 36 | self.temp = self.temp * self.annealing_rate |
||
| 37 |