| Total Complexity | 2 |
| Total Lines | 56 |
| 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 gradient_free_optimizers import StochasticHillClimbingOptimizer |
||
| 9 | |||
| 10 | |||
| 11 | n_iter = 1000 |
||
| 12 | |||
| 13 | |||
| 14 | def objective_function(para): |
||
| 15 | score = -para["x1"] * para["x1"] |
||
| 16 | return score |
||
| 17 | |||
| 18 | |||
| 19 | search_space = { |
||
| 20 | "x1": np.arange(0, 10, 1), |
||
| 21 | } |
||
| 22 | |||
| 23 | |||
| 24 | def test_p_accept(): |
||
| 25 | p_accept_low = 0.5 |
||
| 26 | p_accept_high = 1 |
||
| 27 | |||
| 28 | epsilon = 1 / np.inf |
||
| 29 | |||
| 30 | opt = StochasticHillClimbingOptimizer( |
||
| 31 | search_space, |
||
| 32 | p_accept=p_accept_low, |
||
| 33 | epsilon=epsilon, |
||
| 34 | initialize={"random": 1}, |
||
| 35 | ) |
||
| 36 | opt.search(objective_function, n_iter=n_iter) |
||
| 37 | n_transitions_low = opt.n_transitions |
||
| 38 | |||
| 39 | opt = StochasticHillClimbingOptimizer( |
||
| 40 | search_space, |
||
| 41 | p_accept=p_accept_high, |
||
| 42 | epsilon=epsilon, |
||
| 43 | initialize={"random": 1}, |
||
| 44 | ) |
||
| 45 | opt.search(objective_function, n_iter=n_iter) |
||
| 46 | n_transitions_high = opt.n_transitions |
||
| 47 | |||
| 48 | print("\n n_transitions_low", n_transitions_low) |
||
| 49 | print("\n n_transitions_high", n_transitions_high) |
||
| 50 | |||
| 51 | lower_bound = int(n_iter * p_accept_low) |
||
| 52 | lower_bound -= lower_bound * 0.1 |
||
| 53 | higher_bound = n_iter |
||
| 54 | |||
| 55 | assert lower_bound < n_transitions_low < n_transitions_high < higher_bound |
||
| 56 |