1
|
|
|
# Author: Simon Blanke |
2
|
|
|
# Email: [email protected] |
3
|
|
|
# License: MIT License |
4
|
|
|
|
5
|
|
|
import pytest |
6
|
|
|
import numpy as np |
7
|
|
|
|
8
|
|
|
from surfaces.test_functions.mathematical import SphereFunction |
9
|
|
|
|
10
|
|
|
from gradient_free_optimizers import StochasticHillClimbingOptimizer |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
sphere_function = SphereFunction(n_dim=2) |
14
|
|
|
objective_function = sphere_function.objective_function |
15
|
|
|
search_space = sphere_function.search_space() |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
n_iter_para = ( |
19
|
|
|
"n_iter", |
20
|
|
|
[ |
21
|
|
|
(300), |
22
|
|
|
(500), |
23
|
|
|
(1000), |
24
|
|
|
], |
25
|
|
|
) |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
@pytest.mark.parametrize(*n_iter_para) |
29
|
|
|
def test_p_accept(n_iter): |
30
|
|
|
p_accept_low = 0.5 |
31
|
|
|
p_accept_mid = 0.75 |
32
|
|
|
p_accept_high = 1 |
33
|
|
|
|
34
|
|
|
epsilon = 1 / np.inf |
35
|
|
|
|
36
|
|
|
opt = StochasticHillClimbingOptimizer( |
37
|
|
|
search_space, |
38
|
|
|
p_accept=p_accept_low, |
39
|
|
|
epsilon=epsilon, |
40
|
|
|
initialize={"random": 1}, |
41
|
|
|
) |
42
|
|
|
opt.search(objective_function, n_iter=n_iter) |
43
|
|
|
n_transitions_low = opt.n_transitions |
44
|
|
|
|
45
|
|
|
opt = StochasticHillClimbingOptimizer( |
46
|
|
|
search_space, |
47
|
|
|
p_accept=p_accept_mid, |
48
|
|
|
epsilon=epsilon, |
49
|
|
|
initialize={"random": 1}, |
50
|
|
|
) |
51
|
|
|
opt.search(objective_function, n_iter=n_iter) |
52
|
|
|
n_transitions_mid = opt.n_transitions |
53
|
|
|
|
54
|
|
|
opt = StochasticHillClimbingOptimizer( |
55
|
|
|
search_space, |
56
|
|
|
p_accept=p_accept_high, |
57
|
|
|
epsilon=epsilon, |
58
|
|
|
initialize={"random": 1}, |
59
|
|
|
) |
60
|
|
|
opt.search(objective_function, n_iter=n_iter) |
61
|
|
|
n_transitions_high = opt.n_transitions |
62
|
|
|
|
63
|
|
|
print("\n n_transitions_low", n_transitions_low) |
64
|
|
|
print("\n n_transitions_mid", n_transitions_mid) |
65
|
|
|
print("\n n_transitions_high", n_transitions_high) |
66
|
|
|
|
67
|
|
|
lower_bound = int(n_iter * p_accept_low) |
68
|
|
|
lower_bound -= lower_bound * 0.1 |
69
|
|
|
higher_bound = n_iter |
70
|
|
|
|
71
|
|
|
assert ( |
72
|
|
|
lower_bound |
73
|
|
|
< n_transitions_low |
74
|
|
|
< n_transitions_mid |
75
|
|
|
< n_transitions_high |
76
|
|
|
< higher_bound |
77
|
|
|
) |
78
|
|
|
|