-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathutils.py
More file actions
44 lines (32 loc) · 1.03 KB
/
Copy pathutils.py
File metadata and controls
44 lines (32 loc) · 1.03 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
import numpy as np
import networkx as nx
from torch import Tensor
import torch
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
logging.basicConfig(
format="%(levelname)s: %(message)s"
)
def get_tour_coords(coords, tour):
return coords[torch.arange(len(tour)).view(-1, 1), tour]
def get_tour_len(coords: Tensor, tour: Tensor = None) -> Tensor:
"""Compute the length of a batch of tours.
Args:
tour (Tensor): shape (N, L, D)
Returns:
Tensor: shape (N), contains the length of each tour in the batch.
"""
if tour is not None:
coords = get_tour_coords(coords, tour)
diff = torch.diff(coords, dim=1)
return diff.square().sum(dim=-1).sqrt().sum(dim=-1)
def np2nx(x: np.ndarray):
G = nx.Graph()
for i, node in enumerate(x):
G.add_node(i + 1, pos=node)
for j, node2 in enumerate(x):
if i != j:
d = ((node - node2) ** 2).sum() ** 0.5
G.add_edge(i + 1, j + 1, weight=d.item())
return G