-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathutils.py
57 lines (52 loc) · 2.07 KB
/
utils.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
import torch
from torch_geometric.data import Data
def gen_distance_matrix(tsp_coordinates):
'''
Args:
tsp_coordinates: torch tensor [n_nodes, 2] for node coordinates
Returns:
distance_matrix: torch tensor [n_nodes, n_nodes] for EUC distances
'''
n_nodes = len(tsp_coordinates)
distances = torch.norm(tsp_coordinates[:, None] - tsp_coordinates, dim=2, p=2)
distances[torch.arange(n_nodes), torch.arange(n_nodes)] = 1e9 # note here
return distances
def gen_pyg_data(tsp_coordinates, k_sparse):
'''
Args:
tsp_coordinates: torch tensor [n_nodes, 2] for node coordinates
Returns:
pyg_data: pyg Data instance
distances: distance matrix
'''
n_nodes = len(tsp_coordinates)
distances = gen_distance_matrix(tsp_coordinates)
topk_values, topk_indices = torch.topk(distances,
k=k_sparse,
dim=1, largest=False)
edge_index = torch.stack([
torch.repeat_interleave(torch.arange(n_nodes).to(topk_indices.device),
repeats=k_sparse),
torch.flatten(topk_indices)
])
edge_attr = topk_values.reshape(-1, 1)
pyg_data = Data(x=tsp_coordinates, edge_index=edge_index, edge_attr=edge_attr)
return pyg_data, distances
def load_val_dataset(n_node, k_sparse, device):
val_list = []
val_tensor = torch.load(f'../data/tsp/valDataset-{n_node}.pt')
for instance in val_tensor:
instance = instance.to(device)
data, distances = gen_pyg_data(instance, k_sparse=k_sparse)
val_list.append((data, distances))
return val_list
def load_test_dataset(n_node, k_sparse, device):
val_list = []
val_tensor = torch.load(f'../data/tsp/testDataset-{n_node}.pt')
for instance in val_tensor:
instance = instance.to(device)
data, distances = gen_pyg_data(instance, k_sparse=k_sparse)
val_list.append((data, distances))
return val_list
if __name__ == "__main__":
pass