|
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 ...base_positioner import BasePositioner |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
class HillClimbingOptimizer(BaseOptimizer): |
|
12
|
|
|
def __init__(self, _opt_args_): |
|
13
|
|
|
super().__init__(_opt_args_) |
|
14
|
|
|
self.n_positioners = 1 |
|
15
|
|
|
|
|
16
|
|
|
def _hill_climb_iter(self, i, _cand_): |
|
17
|
|
|
score_new = -np.inf |
|
18
|
|
|
pos_new = None |
|
19
|
|
|
|
|
20
|
|
|
self.p_list[0].move_climb(_cand_, self.p_list[0].pos_current) |
|
21
|
|
|
self._optimizer_eval(_cand_, self.p_list[0]) |
|
22
|
|
|
|
|
23
|
|
|
if self.p_list[0].score_new > score_new: |
|
24
|
|
|
score_new = self.p_list[0].score_new |
|
25
|
|
|
pos_new = self.p_list[0].pos_new |
|
26
|
|
|
|
|
27
|
|
|
if i % self._opt_args_.n_neighbours == 0: |
|
28
|
|
|
self.p_list[0].pos_new = pos_new |
|
29
|
|
|
self.p_list[0].score_new = score_new |
|
30
|
|
|
|
|
31
|
|
|
self._update_pos(_cand_, self.p_list[0]) |
|
32
|
|
|
|
|
33
|
|
|
def _iterate(self, i, _cand_): |
|
34
|
|
|
self._hill_climb_iter(i, _cand_) |
|
35
|
|
|
|
|
36
|
|
|
def _init_iteration(self, _cand_): |
|
37
|
|
|
p = super()._init_base_positioner(_cand_, positioner=HillClimbingPositioner) |
|
38
|
|
|
|
|
39
|
|
|
self._optimizer_eval(_cand_, p) |
|
40
|
|
|
self._update_pos(_cand_, p) |
|
41
|
|
|
|
|
42
|
|
|
return p |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
class HillClimbingPositioner(BasePositioner): |
|
46
|
|
|
def __init__(self, *args, **kwargs): |
|
47
|
|
|
super().__init__(*args, **kwargs) |
|
48
|
|
|
|
|
49
|
|
|
self.epsilon = kwargs["epsilon"] |
|
50
|
|
|
self.climb_dist = kwargs["climb_dist"] |
|
51
|
|
|
|