Passed
Push — master ( d39371...69bf6f )
by Simon
03:38
created

gradient_free_optimizers.optimizers.local.simulated_annealing   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 30
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A SimulatedAnnealingOptimizer.__init__() 0 4 1
A SimulatedAnnealingOptimizer._accept_default() 0 2 1
A SimulatedAnnealingOptimizer._accept_adapt() 0 2 1
A SimulatedAnnealingOptimizer.evaluate() 0 4 1
1
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
6
import numpy as np
7
8
from ..local import StochasticHillClimbingOptimizer
9
from ...search import Search
10
11
12
class SimulatedAnnealingOptimizer(StochasticHillClimbingOptimizer, Search):
13
    def __init__(self, search_space, annealing_rate=0.99, start_temp=100):
14
        super().__init__(search_space)
15
        self.annealing_rate = annealing_rate
16
        self.temp = start_temp
17
18
    # use _consider from StochasticHillClimbingOptimizer
19
20
    def _accept_default(self):
21
        return np.exp(-self._score_norm_default() / self.temp)
22
23
    def _accept_adapt(self):
24
        return self._score_norm_adapt() * self.temp
25
26
    def evaluate(self, score_new):
27
        super().evaluate(score_new)
28
29
        self.temp = self.temp * self.annealing_rate
30