-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrow_map_k_chain.py
68 lines (60 loc) · 1.75 KB
/
grow_map_k_chain.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
import torch
k = 16
s = 48
m = k * s + 1
l = s + 1
b = k
positions = [0]
states = [(m,l,b)]
active = [True]
depth = [0]
Successors = [[]]
attention_mask = torch.zeros(m,m).long()
parents = [-1]
expand_lists = []
expand_branches = []
num_nodes = 1
while True:
expand = []
expand_branch = []
for i, act in enumerate(active):
if act:
if parents[i] != -1:
attention_mask[i] = attention_mask[parents[i]]
attention_mask[i][i] = 1
expand.append(i)
active[i] = False
(x,y,z) = states[i]
expand_branch.append(z)
positions.extend(list(range(num_nodes, num_nodes + z)))
Successors[i].extend(list(range(num_nodes, num_nodes + z)))
Successors.extend([[] for _ in range(z)])
parents.extend([i for _ in range(z)])
depth.extend([depth[i] + 1 for _ in range(z)])
if z > 1 and s > 1:
states.extend([(s, s, 1) for _ in range(z)])
elif x >= 3 and y >= 3:
states.extend([(x-1, y-1, z) for _ in range(z)])
else:
states.extend([(x-1, y-1, 0) for _ in range(z)])
num_nodes = num_nodes + z
if len(expand) == 0:
break
expand_lists.append(expand)
expand_branches.append(expand_branch)
active.extend([True for _ in range(sum(expand_branch))])
print(expand_lists)
print(expand_branches)
assert num_nodes == m
assert len(positions) == m
assert len(depth) == m
grow_map = {
"roots": expand_lists,
"branches": expand_branches,
"Successors":Successors,
"mask": attention_mask,
"depth": torch.LongTensor(depth),
"size": num_nodes
}
path = "./growmaps/16x48-tree.pt"
torch.save(grow_map, path)