-
Notifications
You must be signed in to change notification settings - Fork 0
/
opt_test.py
83 lines (68 loc) · 1.98 KB
/
opt_test.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
import numpy as np
import opt
def test_argmin():
cases = [
{ "in": ([1,2,3], lambda x: x**2), "out": 1 },
]
for case in cases:
got = opt.argmin(*case["in"])
want = case["out"]
if not (got == want):
raise Exception("argmin test failure")
def test_sample_controls():
cases = [
{
"in": (1, 3),
"out": np.array([
[np.cos(0), np.sin(0)],
[np.cos(np.pi), np.sin(np.pi)],
[np.cos(2*np.pi), np.sin(2*np.pi)],
]),
},
]
for case in cases:
got = opt.sample_controls(*case["in"])
want = case["out"]
if not np.alltrue((np.isclose(got, want))):
raise Exception("sample_controls test failure")
def test_q_value():
cases = [
{
"in": (np.array([0, 0]), np.array([1, 0])),
"u": np.array([.1, 0.0]),
"out": 1.0,
},
{
"in": (np.array([0, 0]), np.array([1, 0])),
"u": np.array([-.1, 0.0]),
"out": 1.2,
},
]
for case in cases:
got = opt.q_value(*case["in"])(case["u"])
want = case["out"]
if not np.allclose(got, want):
raise Exception("test_q_value: got " + repr(got) + " want " + repr(want))
def test_expected_q_value():
cases = [
{
"in": (np.array([0, 0]),
np.array([
[1, 0],
[2, 0],
]),
np.array([0.5, 0.5])),
"u": np.array([.1, 0.0]),
"out": 1.5,
},
]
for case in cases:
got = opt.expected_q_value(*case["in"])(case["u"])
want = case["out"]
if not np.allclose(got, want):
raise Exception("test_expected_q_value: got " + repr(got) + " want " + repr(want))
if __name__ == '__main__':
test_argmin()
test_sample_controls()
test_q_value()
test_expected_q_value()