Passed
Push — master ( f321c8...7f7475 )
by Simon
01:34
created

test_p_accept()   A

Complexity

Conditions 1

Size

Total Lines 32
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 24
nop 0
dl 0
loc 32
rs 9.304
c 0
b 0
f 0
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 = 1000
19
20
21
def test_p_accept():
22
    p_accept_low = 0.5
23
    p_accept_high = 1
24
25
    epsilon = 1 / np.inf
26
27
    opt = StochasticHillClimbingOptimizer(
28
        search_space,
29
        p_accept=p_accept_low,
30
        epsilon=epsilon,
31
        initialize={"random": 1},
32
    )
33
    opt.search(objective_function, n_iter=n_iter)
34
    n_transitions_low = opt.n_transitions
35
36
    opt = StochasticHillClimbingOptimizer(
37
        search_space,
38
        p_accept=p_accept_high,
39
        epsilon=epsilon,
40
        initialize={"random": 1},
41
    )
42
    opt.search(objective_function, n_iter=n_iter)
43
    n_transitions_high = opt.n_transitions
44
45
    print("\n n_transitions_low", n_transitions_low)
46
    print("\n n_transitions_high", n_transitions_high)
47
48
    lower_bound = int(n_iter * p_accept_low)
49
    lower_bound -= lower_bound * 0.1
50
    higher_bound = n_iter
51
52
    assert lower_bound < n_transitions_low < n_transitions_high < higher_bound
53