This repository has been archived by the owner on Jan 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zad1.py
124 lines (96 loc) · 3.37 KB
/
zad1.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
import random as rd
import pandas as pd
import time
start_time = time.time()
GROUPS = 10
POINTS = 201
#rd.seed(65)
# Read distances
distances = pd.read_csv('distances.data', usecols=range(0, POINTS + 1)) # usecols=Omit the index column
distances_cpy = distances.copy()
cols = []
for i in range(POINTS):
col = distances_cpy.sort_values(by=[str(i)])
cols.append(col[str(i)]) # .iloc[1:])
# Points that are still available as endings
not_available_ending_points = []
points = list(range(0, POINTS))
groups = list(range(0, GROUPS))
dictionaries = dict.fromkeys(groups, list())
copy_point = points.copy()
for group in dictionaries.keys():
value = rd.choice(copy_point)
dictionaries[group] = [(value, value)]
# print(copy_point)
copy_point.remove(value)
# print(copy_point)
not_available_ending_points.append(value)
while len(not_available_ending_points) < POINTS:
current_shortest_edge = {
'group': None,
'from_point': None,
'to_point': None,
'distance': None
}
for group in dictionaries.keys():
# print("Group:", group)
for _, point_in_group in dictionaries[group]:
# print("Point_in_group:", point_in_group)
for new_point, dist in cols[point_in_group].sort_values(ascending=True).iteritems():
# print(new_point, dist)
if new_point not in not_available_ending_points:
if current_shortest_edge['distance'] is None or dist < current_shortest_edge['distance']:
current_shortest_edge['group'] = group
current_shortest_edge['from_point'] = point_in_group
current_shortest_edge['to_point'] = new_point
current_shortest_edge['distance'] = dist
if current_shortest_edge['to_point'] is not None:
not_available_ending_points.append(current_shortest_edge['to_point'])
for group in dictionaries.keys():
for _, point_in_group in dictionaries[group]:
# print("Drop", point_in_group, current_shortest_edge['to_point'])
cols[point_in_group].drop(current_shortest_edge['to_point'], inplace=True)
# print(type(cols[point_in_group]))
print("Group:", current_shortest_edge['group'],
"From point:", current_shortest_edge['from_point'],
"To point:", current_shortest_edge['to_point'],
"Distance", current_shortest_edge['distance'])
dictionaries[current_shortest_edge['group']].append((current_shortest_edge['from_point'], current_shortest_edge['to_point']))
current_shortest_edge['group'] = None
current_shortest_edge['from_point'] = None
current_shortest_edge['to_point'] = None
current_shortest_edge['distance'] = None
print("dicti", dictionaries)
elapsed_time = time.time() - start_time
print(elapsed_time)
import matplotlib.pyplot as plt
import pandas as pd
points = pd.read_csv('objects.data', sep=" ", header=None, usecols=[0, 1])
points.columns = ['X', 'Y']
x = []
y = []
clrs = []
colors = ['tab:blue', 'tab:orange', 'tab:green', 'tab:red', 'tab:purple', 'tab:brown', 'tab:pink', 'tab:gray',
'tab:olive', 'tab:cyan']
edges = dictionaries
for i in range(len(points)):
x.append(points.iloc[i][0])
y.append(points.iloc[i][1])
clrs.append(colors[0])
clr_nr = 0
for grp in edges.values():
for case in grp:
pointa_x = x[case[0]]
pointa_y = y[case[0]]
pointb_x = x[case[1]]
pointb_y = y[case[1]]
plt.plot([pointa_x, pointb_x], [pointa_y, pointb_y], c=colors[clr_nr])#, marker='o')
clr_nr += 1
# plt.scatter(
# x,
# y,
# c=clrs
# )
plt.xlabel('X')
plt.ylabel('Y')
plt.show()