-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaseline.py
132 lines (113 loc) · 3.59 KB
/
baseline.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
"""
writer: Yongjun Shin
date: 2017.11.30
USAGE: change directory path of inputs
"""
import os
import time
from graph_partition import *
def main():
data_dir = "./data/" #terminate path with one slash
out_dir = "./heuristic_out/"
if not os.path.isdir(out_dir):
os.mkdir(out_dir)
f_r = open(out_dir + "result_random.csv", 'w')
f_K = open(out_dir + "result_Kerninghan.csv", 'w')
f_S = open(out_dir + "result_Spectral.csv", 'w')
f_B = open(out_dir + "result_buttom_up.csv", 'w')
f_r.write(",MQ, time(s), cluster\n")
f_K.write(",MQ, time(s), cluster\n")
f_S.write(",MQ, time(s), cluster\n")
f_B.write(",MQ, time(s), cluster\n")
for fname in os.listdir(data_dir):
if not os.path.isfile(data_dir+fname):
continue
graph = csv_to_list(data_dir+fname)
f_r.write(fname + ",")
f_K.write(fname + ",")
f_S.write(fname + ",")
f_B.write(fname + ",")
#print_graph(graph)
#print()
print("=========="+fname+"==========")
start = time.time()
print("random...")
cluster_result1 = clustering_random(graph)
end = time.time()
t_r = end - start
print("Kernighan Lin nver...")
start = time.time()
cluster_result2 = clustering_Kernighan_Lin_nver(graph) #needs long time...
end = time.time()
t_K = end - start
print("Spectral Bisection nver...")
start = time.time()
cluster_result3 = clustering_Spectral_Bisection_nver(graph)
end = time.time()
t_S = end - start
'''print("Kernighan Lin...")
cluster_result4 = clustering_Kernighan_Lin(graph)
print("Spectral Bisection...")
cluster_result5 = clustering_Spectral_Bisection(graph)'''
print("Bottom Up...")
start = time.time()
cluster_result6 = clustering_bottom_up(graph)
end = time.time()
t_B = end - start
print()
print("random")
print_cluster_result_list(cluster_result1)
MQ_r = MQ(cluster_result1, graph)
print(MQ_r)
print()
f_r.write(str(MQ_r)+","+str(t_r)+",")
for c in cluster_result1:
f_r.write(str(c)+",")
f_r.write("\n")
'''
print("Kerninghan Lin")
print_cluster_result_list(cluster_result4)
print(MQ(cluster_result4, graph))
print()
'''
print("Kerninghan Lin nver")
print_cluster_result_list(cluster_result2)
MQ_K = MQ(cluster_result2, graph)
print(MQ_K)
print()
f_K.write(str(MQ_K) + "," + str(t_K) + ",")
for c in cluster_result2:
f_K.write(str(c) + ",")
f_K.write("\n")
'''
print("Spectral Bisection")
print_cluster_result_list(cluster_result5)
print(MQ(cluster_result5, graph))
print()
'''
print("Spectral Bisection nver")
print_cluster_result_list(cluster_result3)
MQ_S = MQ(cluster_result3, graph)
print(MQ_S)
print()
f_S.write(str(MQ_S) + "," + str(t_S) + ",")
for c in cluster_result3:
f_S.write(str(c) + ",")
f_S.write("\n")
print("Bottom Up")
print_cluster_result_list(cluster_result6)
MQ_B = MQ(cluster_result6, graph)
print(MQ_B)
print()
print()
f_B.write(str(MQ_B) + "," + str(t_B) + ",")
for c in cluster_result6:
f_B.write(str(c) + ",")
f_B.write("\n")
f_r.close()
f_K.close()
f_S.close()
f_B.close()
return
if __name__ == "__main__":
main()