-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest.py
More file actions
113 lines (90 loc) · 4.67 KB
/
test.py
File metadata and controls
113 lines (90 loc) · 4.67 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import unittest
import numpy as np
from utils import Representation, Interval
class ComplexTest(unittest.TestCase):
def setUp(self):
dimensions = [3, 3] # 2D grid 3x3
self.rep = Representation(dimensions)
self.rep.create_vecs((0, 0), 0) # (position, vector space's dimension)
self.rep.create_vecs((0, 1), 1)
self.rep.create_vecs((0, 2), 1)
self.rep.create_vecs((1, 0), 2)
self.rep.create_vecs((1, 1), 2)
self.rep.create_vecs((1, 2), 2)
self.rep.create_vecs((2, 0), 2)
self.rep.create_vecs((2, 1), 2)
self.rep.create_vecs((2, 2), 2)
# add linear maps u: x -> y
self.rep.create_matrix((0, 0), (1, 0), None) # (x, y, matrix)
self.rep.create_matrix((0, 0), (0, 1), None) # for matrices equal to 0 you can directly write None
self.rep.create_matrix((0, 1), (0, 2), np.array([[0]]))
self.rep.create_matrix((0, 1), (1, 1), np.array([[0], [1]]))
self.rep.create_matrix((0, 2), (1, 2), np.array([[0], [1]]))
self.rep.create_matrix((1, 0), (2, 0), np.array([[0, 0], [0, 0]]))
self.rep.create_matrix((1, 0), (1, 1), np.array([[0, 0], [0, 0]]))
self.rep.create_matrix((1, 1), (1, 2), np.array([[1, 0], [0, 0]]))
self.rep.create_matrix((1, 1), (2, 1), np.array([[1, 0], [0, 1]]))
self.rep.create_matrix((1, 2), (2, 2), np.array([[1, 0], [0, 1]]))
self.rep.create_matrix((2, 0), (2, 1), np.array([[1, 0], [0, 1]]))
self.rep.create_matrix((2, 1), (2, 2), np.array([[1, 0], [0, 0]]))
self.intervals = self.rep.list_int(conv=False)
def test_evaluation_from_01_to_21(self):
assert np.array_equal(self.rep.evaluation((0, 1), (2, 1)), np.array([[0], [1]]))
def test_evaluation_from_10_to_12(self):
assert np.array_equal(self.rep.evaluation((1, 0), (1, 2)), np.array([[0, 0], [0, 0]]))
def test_evaluation_from_11_to_22(self):
assert np.array_equal(self.rep.evaluation((1, 1), (2, 2)), np.array([[1, 0], [0, 0]]))
def test_number_of_generated_intervals(self):
assert len(self.intervals) == 83
def test_interval_hull_type_3_1(self):
interval = Interval([(2, 0), (1, 1), (0, 2)], [(2, 2)])
assert set(self.rep.int_hull(interval)) == {(1, 2), (2, 1), (1, 1), (2, 0), (0, 2), (2, 2)}
def test_matrix_M_type_3_1(self):
interval = Interval([(2, 0), (1, 1), (0, 2)], [(2, 2)])
matrix = self.rep.construct_matrix_M_tot(interval)
expected = np.array([[1., 0., -1., -0., 0.],
[0., 1., -0., -1., 0.],
[1., 0., 0., 0., -0.],
[0., 0., 0., 0., -1.],
[0., 0., 1., 0., -0.],
[0., 0., 0., 0., -1.]])
matrix_np = np.array(matrix).astype(float)
assert np.array_equal(matrix_np, expected)
def test_matrix_N_type_3_1(self):
interval = Interval([(2, 0), (1, 1), (0, 2)], [(2, 2)])
matrix = self.rep.construct_matrix_N_tot(interval)
assert np.array(matrix).size == 0
def test_matrix_M_type_2_2(self):
interval = Interval([(0, 1), (1, 0)], [(2, 1), (1, 2)])
matrix = self.rep.construct_matrix_M_tot(interval)
expected = np.array([[0., -0., -0.],
[1., -0., -0.]])
matrix_np = np.array(matrix).astype(float)
assert np.array_equal(matrix_np, expected)
def test_matrix_N_type_2_2(self):
interval = Interval([(0, 1), (1, 0)], [(2, 1), (1, 2)])
matrix = self.rep.construct_matrix_N_tot(interval)
expected = np.array([[1., 0.],
[0., 1.],
[-1., -0.],
[-0., -0.]])
matrix_np = np.array(matrix).astype(float)
assert np.array_equal(matrix_np, expected)
def test_interval_rank_type_3_1(self):
interval = Interval([(2, 0), (1, 1), (0, 2)], [(2, 2)])
assert self.rep.int_rank(interval) == 0
def test_interval_replacement_type_3_1(self):
interval = Interval([(2, 0), (1, 1), (0, 2)], [(2, 2)])
assert self.rep.int_replacement(interval) == 0
def test_interval_replacement_all(self):
interval_replacement = {}
for itv in self.intervals:
repl = self.rep.int_replacement(itv)
if repl != 0:
interval_replacement[(tuple(sorted(itv.src)), tuple(sorted(itv.snk)))] = repl
assert interval_replacement == {(((1, 0),), ((1, 0),)): 2,
(((0, 2),), ((2, 2),)): 1,
(((0, 1), (2, 0)), ((2, 1),)): 1,
(((1, 1), (2, 0)), ((2, 2),)): 1}
if __name__ == "__main__":
unittest.main()