-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreatedata.py
134 lines (97 loc) · 3.52 KB
/
createdata.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import os
import sys
import re
import random
def generator():
file = open("instances\\generator.txt", 'w')
while True:
vertices = input('Type number of vertices: ')
try:
vertices = int(vertices)
if vertices > 1:
break
else:
print('Number of vertices should be at least one!')
except ValueError:
print('Number of vertices should be an integer!')
file.write(str(vertices) + "\n")
pairs = []
i = 0
while len(pairs) < vertices:
x = random.randint(0, 200)
y = random.randint(0, 200)
if (x, y) not in pairs:
pairs.append((x, y))
file.write(str(i + 1) + " " + str(x) + " " + str(y) + "\n")
i += 1
file.close()
return file.name
def choose_file(dir_path=None):
if dir_path is None:
dir_path = os.getcwd()
dir_content = os.listdir(dir_path)
dir_txts = list(filter(lambda x: x[-4:] == '.txt', dir_content))
dir_txts.append('generator')
print('\nYou can use data from: ')
print(*dir_txts, sep='\n')
while True:
answer = input('Type which one would you like to use: ')
if answer == 'generator':
return generator()
elif answer in dir_txts:
print('xd')
return dir_path + '\\' + answer
else:
print('Please, enter a correct name.')
def check_file(filename):
with open(filename, encoding='utf-8-sig') as file:
lines = file.readlines()
try:
number_of_lines = int(lines[0])
for index, line in enumerate(lines[1:]):
if index == number_of_lines:
print('File has too many lines!')
return False
# wyrazenie regularne postaci: 'numer linii''spacja''liczba calkowita''spacja''liczba calkowita'
# result = re.search('^' + str(index + 1) + ' -?\\d+ -?\\d+$', line)
result = re.search(r'^\d+\s+(-?)\d+\s+(-?)\d+(\s+|$)', line)
if result is None:
print(f'Invalid data in line {index + 2}')
return False
if index + 1 == number_of_lines:
file.close()
return True
print('File has not enough lines!')
return False
except ValueError:
print('First line should be a single integer value!')
return False
def distance(x1, y1, x2, y2):
return round(((x2 - x1) ** 2 + (y2 - y1) ** 2) ** (1 / 2), 3)
def create_matrix(filename):
with open(filename, encoding='utf-8-sig') as file:
content = file.read().splitlines()
number_of_vertices = int(content.pop(0))
a = []
for number in range(number_of_vertices):
tmp = content[number].replace(' ', ' ').split(' ')
a.append((int(tmp[1]), int(tmp[2])))
matrix = []
for vertex_index in range(number_of_vertices):
distances = []
for index in range(number_of_vertices):
if index > vertex_index:
distances.append(distance(a[vertex_index][0], a[vertex_index][1], a[index][0], a[index][1]))
elif index < vertex_index:
distances.append(matrix[index][vertex_index])
else:
distances.append(0)
matrix.append(distances)
return matrix
if __name__ == '__main__':
f = choose_file()
if check_file(f):
print('Robie macierza')
create_matrix(f)
else:
sys.exit(0)