Passed
Push — master ( cc0d0d...097d7d )
by Simon
01:34
created

tests.test_RandomSearch._base_test()   A

Complexity

Conditions 3

Size

Total Lines 10
Code Lines 9

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
eloc 9
dl 10
loc 10
rs 9.95
c 0
b 0
f 0
cc 3
nop 2
1
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
import numpy as np
6
7
from gradient_free_optimizers import RandomSearchOptimizer
8
9
n_iter = 100
10
11
12
def get_score(pos_new):
13
    return -(pos_new[0] * pos_new[0])
14
15
16
space_dim = np.array([10])
17
init_positions = [np.array([0]), np.array([1]), np.array([2]), np.array([3])]
18
19
20 View Code Duplication
def _base_test(opt, init_positions):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
21
    for nth_init in range(len(init_positions)):
22
        pos_new = opt.init_pos(nth_init)
23
        score_new = get_score(pos_new)
24
        opt.evaluate(score_new)
25
26
    for nth_iter in range(len(init_positions), n_iter):
27
        pos_new = opt.iterate(nth_iter)
28
        score_new = get_score(pos_new)
29
        opt.evaluate(score_new)
30
31
32
def _test_RandomSearchOptimizer(
33
    init_positions=init_positions, space_dim=space_dim, opt_para={}
34
):
35
    opt = RandomSearchOptimizer(init_positions, space_dim, opt_para)
36
    _base_test(opt, init_positions)
37
38
39
def test_epsilon():
40
    _test_RandomSearchOptimizer()
41