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

HillClimbingOptimizer._move_climb()   A

Complexity

Conditions 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nop 3
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
import numpy as np
6
7
from ..base_optimizer import BaseOptimizer
8
from ...search import Search
9
10
from numpy.random import normal, laplace, logistic, gumbel
11
12
dist_dict = {
13
    "normal": normal,
14
    "laplace": laplace,
15
    "logistic": logistic,
16
    "gumbel": gumbel,
17
}
18
19
20
class HillClimbingOptimizer(BaseOptimizer, Search):
21
    def __init__(
22
        self, search_space, epsilon=0.05, distribution="normal", n_neighbours=1,
23
    ):
24
        super().__init__(search_space)
25
        self.epsilon = epsilon
26
        self.distribution = dist_dict[distribution]
27
        self.n_neighbours = n_neighbours
28
29
    def _move_climb(self, pos, epsilon_mod=1):
30
        sigma = self.space_dim * self.epsilon * epsilon_mod
31
        pos_normal = self.distribution(pos, sigma, pos.shape)
32
        pos_new_int = np.rint(pos_normal)
33
34
        n_zeros = [0] * len(self.space_dim)
35
        pos = np.clip(pos_new_int, n_zeros, self.space_dim)
36
37
        self.pos_new = pos.astype(int)
38
        return self.pos_new
39
40
    def iterate(self):
41
        return self._move_climb(self.pos_current)
42
43