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

gradient_free_optimizers.optimizers.local_opt.simulated_annealing   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 37
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A SimulatedAnnealingOptimizer._accept_adapt() 0 2 1
A SimulatedAnnealingOptimizer.__init__() 0 11 1
A SimulatedAnnealingOptimizer.evaluate() 0 4 1
A SimulatedAnnealingOptimizer._accept_default() 0 2 1
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