-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpartitioning.py
168 lines (143 loc) · 4.45 KB
/
partitioning.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 25 15:26:05 2020
@author: Rahul
"""
import numpy as np
import random
import networkx as nx
from parse import netadj85
import matplotlib.pyplot as plt
def cut_size(n1, n2, A):
count = 0
for i in range(len(A)):
for j in range(len(A)):
if A[i][j] == 1:
if (i in n1 and j in n2) or (i in n2 and j in n1):
count+=1
return (count)
def bestswap(n1 ,n2, A, pairs):
mark = []
partition = []
cut_s = []
while(len(mark) != pairs-1):
r_best, ij, part = [], [], []
r_old = cut_size(n1, n2, A)
for i in range(len(n1)):
for j in range(len(n2)):
if ([n1[i],n2[j]] not in mark and ([n2[j],n1[i]] not in mark)):
n11, n22 = n1.copy(), n2.copy()
n11[i] = n2[j]
n22[j] = n1[i]
del_R = r_old - cut_size(n11, n22, A)
r_best.append(del_R)
part.append([n11, n22])
ij.append([n11[i],n22[j]])
a = np.argmax(r_best)
c_part = part[a]
partition.append(c_part)
mark.append(ij[a])
cut_s.append(cut_size(c_part[0], c_part[1], A))
n1, n2 = c_part[0], c_part[1]
return(cut_s[np.argmin(cut_s)], partition[np.argmin(cut_s)])
def kernighanlin(n1, n2, A, pairs):
cs = []
for i in range(1):
CutSize, partition = bestswap(n1, n2, A, pairs)
n1 = partition[0]
n2 = partition[1]
cs.append(CutSize)
if len(cs) >= 10:
ts = cs[-5:]
if (ts[0]==ts[1]==ts[2]==ts[3]==ts[4]):
break
return(CutSize, partition)
def for_pos(p, count1, positions):
n=len(p)
p1,p2,p3,p4 = p[0:n//4],p[n//4:n//2],p[n//2:3*n//4],p[3*n//4:]
count2=0
for i in p1:
positions[i]=[count1,count2]
count1+=1
count2+=1
for j in p2:
positions[j]=[count1,count2]
count1-=1
count2+=1
for k in p3:
positions[k]=[count1,count2]
count1-=1
count2-=1
for l in p4:
positions[l]=[count1,count2]
count1+=1
count2-=1
return positions
def main():
print ("Enter the name of the file containing the required netlist: ")
filename = input("\n")
nodes, A = netadj85(filename)
noded = dict()
for i in range(len(nodes)):
noded[i] = nodes[i]
n = len(A[0])
v = [ i for i in range(n)]
random.shuffle(v)
cut = int(n/2)
n1, n2 = np.sort(v[:cut]), np.sort(v[cut:])
pairs = len(n1)*len(n2)
n11 = [0]*len(n1)
n22 = [0]*len(n2)
j = 0
for i in n1:
n11[j] = nodes[i][0]
j = j+1
j = 0
for i in n2:
n22[j] = nodes[i][0]
j = j+1
print("\nInitial Cut size is : {}".format(cut_size(n1, n2, A)))
print("Initial Partition 1 : {}".format(n11))
print("Initial Partition 2 : {}".format(n22))
c, p = kernighanlin(n1, n2, A, pairs)
p11, p22 = np.sort(p[0]), np.sort(p[1])
p1 = [0]*len(p11)
p2 = [0]*len(p22)
j = 0
for i in p11:
p1[j] = nodes[i][0]
j = j+1
j = 0
for i in p22:
p2[j] = nodes[i][0]
j = j+1
print("Final Cut size is : {}".format(c))
print("Final Partition 1 : {}".format(p1))
print("Final Partition 2 : {}".format(p2))
nd = dict()
for i in range(len(noded)):
nd[i] = noded[i][0][:-3]
G = nx.from_numpy_matrix(np.asarray(A))
for n in G.nodes():
G.nodes[n]['color'] = 'y' if n in p11 else 'g'
colors = [node[1]['color'] for node in G.nodes(data=True)]
positions = dict()
positions = for_pos(p11, 5, positions)
positions = for_pos(p22, 30, positions)
nx.draw_networkx(G, positions, with_labels=True, node_color=colors,
labels=nd)
plt.savefig("Initial_Partitoning.png",dpi = 1000)
plt.show()
G = nx.from_numpy_matrix(np.asarray(A))
for n in G.nodes():
G.nodes[n]['color'] = 'y' if n in n1 else 'g'
colors = [node[1]['color'] for node in G.nodes(data=True)]
positions = dict()
positions = for_pos(n1, 5, positions)
positions = for_pos(n2, 30, positions)
nx.draw_networkx(G, positions, with_labels=True, node_color=colors,
labels=nd)
plt.savefig("Final_Partioning.png",dpi = 1000)
plt.show()
if __name__ == '__main__':
main()