-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_tool_ordering.py
More file actions
114 lines (75 loc) · 2.84 KB
/
test_tool_ordering.py
File metadata and controls
114 lines (75 loc) · 2.84 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
114
import unittest
from tool_ordering import sa_solve, cost_mapping, make_nodes_edges
def is_rotation(seq, target):
"""
Return True if seq is a rotation of target.
"""
if len(seq) != len(target):
return False
# print("foo")
doubled = target + target
# look for seq as a contiguous slice in doubled
for i in range(len(target)):
if doubled[i:i+len(seq)] == seq:
return True
return False
def is_circular_equivalent(seq, target):
"""
Return True if seq is either
- a rotation of target, or
- a rotation of the reverse of target.
"""
return is_rotation(seq, target) or is_rotation(seq, target[::-1])
class TestToolOrdering(unittest.TestCase):
def test_cost_mapping(self):
seq = [1,2,1,3,1,4,1,2]
edges = [(1, 2, 4), (1, 3, 2), (1, 4, 2)]
#################################
# Case 1 -- optimal for M=4
#################################
order = [1, 2, 3, 4]
mapping = {T:i for (i,T) in enumerate(order)}
M = 4 # no empty positions
expected_cost = sum([1,1,2,2,1,1,1,1])
cost = cost_mapping(mapping, edges, 4)
M = 5 # 1 empty position
expected_cost = sum([1,1,2,2,2,2,1,1])
cost = cost_mapping(mapping, edges, 5)
#################################
# Case 2 -- not optimal for M=4
#################################
self.assertEqual(cost, expected_cost)
M = 4 # no empty positions
order = [1, 4, 2, 3]
expected_cost = sum([2,2,1,1,1,1,2,2])
mapping = {T:i for (i,T) in enumerate(order)}
cost = cost_mapping(mapping, edges, M)
self.assertEqual(cost, expected_cost)
M = 10 # 6 empty positions
order = [1, 4, 2, 3]
expected_cost = sum([2,2,3,3,1,1,2,2]) # should not circle around the long way!
mapping = {T:i for (i,T) in enumerate(order)}
cost = cost_mapping(mapping, edges, M)
self.assertEqual(cost, expected_cost)
def test_full_turret(self):
M = 5
seq = [1,2,1,4]
cost, order = sa_solve(seq, M, verbose=False)
expected_order = [4,1,2]
self.assertEqual(cost, 4)
self.assertTrue(is_circular_equivalent(order, expected_order))
M = 4
cost, order = sa_solve(seq, M, verbose=False)
expected_order = [4,1,2]
expected_cost = sum([1,1,1,1])
self.assertEqual(cost, 10)
self.assertTrue(is_circular_equivalent(order, expected_order))
M = 10
seq = [1,2,1,3,1,2,4,1]
cost, order = sa_solve(seq, M, verbose=False)
expected_order = [4,1,2]
expected_cost = sum([1,1,1,1])
self.assertEqual(cost, 10)
self.assertTrue(is_circular_equivalent(order, expected_order))
if __name__ == "__main__":
unittest.main()