Repository navigation
Expand file tree
/
Copy pathgraph_parser.py
More file actions
66 lines (49 loc) · 1.78 KB
/
Copy pathgraph_parser.py
File metadata and controls
66 lines (49 loc) · 1.78 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
"""A parser that transform data in tspXX.txt file into nx.Graph for TSP problem"""
import sys
from math import sqrt
import networkx as nx
class Point:
def __init__(self, tup):
self.x = tup[0]
self.y = tup[1]
def __repr__(self):
return f"Point({self.x:.1f},{self.y:.1f})"
def distance(a, b):
return sqrt((a.x-b.x)**2+(a.y-b.y)**2)
def file_to_points(filename):
nb_node = 0
points = []
with open(filename) as data_file:
nb = 0
# first line is total number of vertices in file
nb_node = int(data_file.readline())
# print(f"expected number of vertices : {nb_node}")
for line in data_file:
points.append(Point(tuple(float(x) for x in line.split())))
# print(f"reading point {nb}: {points[nb]}")
nb = nb + 1
# print(f"\nnumber of vertices found: {nb} \n")
if nb_node != nb:
AssertionError("Number of vertices found is different from expected")
return points
def file_to_graph(filename):
""""Read and parse a .txt file and return a nx.Graph generated from the data
Args:
filename (str): The path to the .txt file containing the data on the cities
Returns:
(nx.Graph, Points[]): The graph generated from the .txt file, The list of points found in the .txt file
"""
print(f"\nExtracting graph from {filename} ...")
points = file_to_points(filename)
print(f"Found: {len(points)} points")
print(f"Points: \n{points}\n")
# Create graph
g = nx.Graph()
# Compute and add all the edges
n = len(points)
for i in range(n):
for j in range(1,n):
weight = distance(points[i], points[j])
g.add_edge(i,j, weight=weight)
# return g
return (g, points)