-
Notifications
You must be signed in to change notification settings - Fork 29
/
test_optimization.py
91 lines (85 loc) · 4.04 KB
/
test_optimization.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import numpy as np
import psyneulink.core.llvm as pnlvm
import psyneulink.core.components.functions.function as Function
import psyneulink.core.components.functions.nonstateful.objectivefunctions as Functions
import psyneulink.core.components.functions.nonstateful.optimizationfunctions as OPTFunctions
import psyneulink.core.globals.keywords as kw
from psyneulink.core.globals.sampleiterator import SampleIterator, SampleSpec
import pytest
SIZE=5
# Some metrics (CROSS_ENTROPY) don't like 0s
test_var = np.random.rand(SIZE) + Function.EPSILON
EPS = float(Function.EPSILON)
search_space = [SampleIterator([EPS, 1.0] if i % 2 == 0 else SampleSpec(start=EPS, stop=1.0, num=2)) for i in range(SIZE)]
results = {
Functions.Stability: {
kw.ENERGY: {
True: {
OPTFunctions.MINIMIZE: {
'FIRST': ((1.0, 1.0, 1.0, 1.0, 1.0), -0.4, [], []),
'RANDOM': ((1.0, 1.0, 1.0, 1.0, 1.0), -0.4, [], []),
},
OPTFunctions.MAXIMIZE: {
'FIRST': ((EPS, EPS, EPS, EPS, EPS), -1.9721522630525296e-32, [], []),
'RANDOM': ((1.0, EPS, EPS, EPS, EPS), -1.9721522630525296e-32, [], []),
},
},
False: {
OPTFunctions.MINIMIZE: {
'FIRST': ((1.0, 1.0, 1.0, 1.0, 1.0), -10.0, [], []),
'RANDOM': ((1.0, 1.0, 1.0, 1.0, 1.0), -10.0, [], []),
},
OPTFunctions.MAXIMIZE: {
'FIRST': ((EPS, EPS, EPS, EPS, EPS), -4.930380657631324e-31, [], []),
'RANDOM': ((1.0, EPS, EPS, EPS, EPS), -4.930380657631324e-31, [], []),
},
},
},
kw.ENTROPY: {
True: {
OPTFunctions.MINIMIZE: {
'FIRST': ((1.0, 1.0, 1.0, 1.0, 1.0), -1.3862943611198906, [], []),
'RANDOM': ((1.0, 1.0, 1.0, 1.0, 1.0), -1.3862943611198906, [], []),
},
OPTFunctions.MAXIMIZE: {
'FIRST': ((EPS, EPS, EPS, EPS, 1.0), 6.931471805599453, [], []),
'RANDOM': ((EPS, EPS, 1.0, EPS, EPS), 6.931471805599453, [], []),
},
},
False: {
OPTFunctions.MINIMIZE: {
'FIRST': ((1.0, 1.0, 1.0, 1.0, 1.0), -6.931471805599453, [], []),
'RANDOM': ((1.0, 1.0, 1.0, 1.0, 1.0), -6.931471805599453, [], []),
},
OPTFunctions.MAXIMIZE: {
'FIRST': ((EPS, EPS, EPS, EPS, 1.0), 34.657359027997266, [], []),
'RANDOM': ((EPS, EPS, 1.0, EPS, EPS), 34.657359027997266, [], []),
},
},
},
},
}
@pytest.mark.function
@pytest.mark.benchmark
@pytest.mark.optimization_function
@pytest.mark.parametrize("selection", ['FIRST', 'RANDOM'])
@pytest.mark.parametrize("direction", [OPTFunctions.MINIMIZE, OPTFunctions.MAXIMIZE])
@pytest.mark.parametrize("normalize", [True, False])
@pytest.mark.parametrize("metric", [kw.ENERGY, kw.ENTROPY])
@pytest.mark.parametrize("obj_func", [Functions.Stability])
def test_grid_search(obj_func, metric, normalize, direction, selection, benchmark, func_mode):
variable = test_var
result = results[obj_func][metric][normalize][direction][selection]
benchmark.group = "OptimizationFunction " + str(obj_func) + " " + metric
of = obj_func(default_variable=variable, metric=metric, normalize=normalize)
f = OPTFunctions.GridSearch(objective_function=of, default_variable=variable,
search_space=search_space, direction=direction,
select_randomly_from_optimal_values=(selection=='RANDOM'),
seed=0, save_values=False)
EX = pytest.helpers.get_func_execution(f, func_mode)
res = benchmark(EX, variable)
assert np.allclose(res[0], result[0])
assert np.allclose(res[1], result[1])
if func_mode == 'Python':
assert np.allclose(res[2], result[2])
assert np.allclose(res[3], result[3])