-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
124 lines (95 loc) · 2.77 KB
/
main.py
File metadata and controls
124 lines (95 loc) · 2.77 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
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
import numpy as np
import random as rn
from numpy import version
import greedy as gd
import ploter as pl
from numpy.core.multiarray import result_type
def Generate(size=False, matrix=None, filename="input"):
vertices = []
while True and type(size) != int:
size = input("Please type how many cities to generate: ")
try:
size = int(size)
except:
print("Please type a number!")
finally:
break
if matrix is None:
matrix = size*10
for i in range(size):
while True:
x = rn.randint(0, matrix)
y = rn.randint(0, matrix)
if [x,y] not in vertices:
vertices.append([x,y])
break
Save(size=size, data=vertices, filename="./data/"+filename+".txt")
vertices = np.array(vertices)
return vertices
def Save(filename='./data/input.txt', size=0, data=[]):
file = open(filename, 'w')
file.write(str(size) + "\n")
i = 1
for city in data:
file.write("{} {} {}\n".format(i, city[0], city[1]))
i += 1
file.close()
def Load(filename='./data/input.txt'):
file = open(filename, 'r')
size = int(file.readline())
vertices = []
for line in file:
tmp = line.split()
vertices.append([int(tmp[1]),int(tmp[2])])
vertices = np.array(vertices)
return vertices
def PathToFile(path):
file = open("output.txt", 'w')
for city in path:
file.write(str(city)+"\n")
file.close()
if __name__ == '__main__':
opt = -1
while opt != 0:
print("\n\n0. Exit.")
print("1. Generate random cities.")
print("2. Load cities form file.")
opt = input("Choose option: ")
try:
opt = int(opt)
except:
print("Please type a number!")
else:
if opt == 1:
cities = Generate()
elif opt == 2:
cities = Load()
elif opt == 0:
exit()
else:
print("Wrong option!")
continue
break
print(cities)
opt = -1
while opt != 0:
print("\n\n0. Exit.")
print("1. Greedy algorithm.")
opt = input("Choose option: ")
try:
opt = int(opt)
except:
print("Please type a number!")
continue
else:
if opt == 1:
distance, result, coordinates = gd.main(cities)
elif opt == 0:
exit()
else:
print("Wrong option!")
continue
break
print("Result:\n", result, "\nDistance:\n", distance)
PathToFile(result)
pl.generateInteractiveGraph(x=coordinates[0], y=coordinates[1])