| Total Complexity | 3 |
| Total Lines | 42 |
| Duplicated Lines | 0 % |
| Changes | 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 |