Passed
Push — master ( 626f23...be3b1e )
by Simon
01:48
created

SimulatedAnnealingOptimizer._accept_adapt()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 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