| Total Complexity | 4 |
| Total Lines | 36 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | import numpy as np |
||
| 2 | from gradient_free_optimizers import RandomSearchOptimizer |
||
| 3 | |||
| 4 | |||
| 5 | """ --- test search spaces with mixed int/float types --- """ |
||
| 6 | |||
| 7 | |||
| 8 | def test_mixed_type_search_space(): |
||
| 9 | def objective_function(para): |
||
| 10 | nonlocal para_types |
||
| 11 | for v, t in zip(para.values(), para_types): |
||
| 12 | assert isinstance(v, t) |
||
| 13 | score = 0 |
||
| 14 | for x1 in range(para["x1"]): |
||
| 15 | score += -(x1 ** 2) + para["x2"] + 100 |
||
| 16 | return score |
||
| 17 | |||
| 18 | search_space = { |
||
| 19 | "x1": range(10, 20), |
||
| 20 | "x2": np.arange(1, 2, 0.1), |
||
| 21 | } |
||
| 22 | para_types = [int, float] |
||
| 23 | expected_pos = [1, 9] |
||
| 24 | |||
| 25 | opt = RandomSearchOptimizer(search_space) |
||
| 26 | opt.search(objective_function, n_iter=10000) |
||
| 27 | |||
| 28 | for best_para_val, expected_p, dim_space, p_type in zip( |
||
| 29 | opt.best_para.values(), expected_pos, search_space.values(), para_types |
||
| 30 | ): |
||
| 31 | print("p_type", p_type) |
||
| 32 | print("dim_space", dim_space) |
||
| 33 | |||
| 34 | assert best_para_val == dim_space[expected_p] |
||
| 35 | assert isinstance(best_para_val, p_type) |
||
| 36 |