Total Complexity | 1 |
Total Lines | 53 |
Duplicated Lines | 0 % |
Changes | 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 |