-
Notifications
You must be signed in to change notification settings - Fork 1
/
graphPartitioning.py
179 lines (144 loc) · 3.73 KB
/
graphPartitioning.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
169
170
171
172
173
174
175
176
177
178
179
import matplotlib.pyplot as plt
import random
import math
import numpy as np
import time
import copy
INF = 10000
alpha = 0.999
threshold = 0.01
# we want to initialize the structure
# i.e. place the k-elements on the N X N grid
def initialize(k):
v1=[i for i in range(1,int(k/2))]
v2=[i for i in range(int(k/2),k+1)]
return v1,v2
# Suppose you are in any state say S, then you can go to
# some other state S'
def findNeighbourState(v1,v2):
# We will follow these two protocols, each 50% of time
# 1. swap positions of any two elements
# 2. Move any element fom v1/v2 to v2/v1
l1 = len(v1)
l2 = len(v2)
tempv1 = copy.deepcopy(v1)
tempv2 = copy.deepcopy(v2)
if(l1==0):
j = random.randint(0,l2-1)
temp = tempv2[j]
tempv2.remove(temp)
tempv1.append(temp)
return tempv1, tempv2
if(l2 == 0):
i = random.randint(0,l1-1)
temp = tempv1[i]
tempv1.remove(temp)
tempv2.append(temp)
return tempv1, tempv2
if(np.random.random()<0.5):
i = random.randint(0,l1-1)
j = random.randint(0,l2-1)
temp = tempv1[i]
tempv1[i] = tempv2[j]
tempv2[j] = temp
else:
choose = random.randint(1,2)
if(choose ==1 ):
i = random.randint(0,l1-1)
temp = tempv1[i]
tempv1.remove(temp)
tempv2.append(temp)
else:
j = random.randint(0,l2-1)
temp = tempv2[j]
tempv2.remove(temp)
tempv1.append(temp)
return tempv1, tempv2
# This will return the updated temperature
# according to some annealing schedule
def updateTemp(T):
return alpha*T
# This function finds the total wirelength
# V1 and V2 is list
def cost(v1, v2, connections):
distance = 0
l1 = len(v1)
l2 = len(v2)
for i in v1:
for j in v2:
if(i<j):
if(connections[i,j] == 1):
distance+=1
#print i,j
else:
if(connections[j,i] == 1):
distance+=1
#print i,j
c = distance + (0.5*(l1-l2)**2)
print v1, v2, c
return (c)
def annealing(k, connections):
T = INF
# We initialize the the two partitions
v1,v2 = initialize(k)
minCost = cost(v1,v2,connections)
print "Initial Cost",minCost
tic = time.clock()
# No. of interation at each temperature
# No. of temperature points to try
while(T > threshold):
tempv1,tempv2 = findNeighbourState(v1,v2)
tempCost = cost(tempv1, tempv2, connections)
delta = tempCost - minCost
if (delta<0):
v1 = tempv1
v2 = tempv2
minCost = tempCost
else:
p = np.exp(-delta / T)
if(np.random.random()<p):
v1 = tempv1
v2 = tempv2
minCost = tempCost
T = updateTemp(T)
return v1,v2,minCost, tic
def create(k) :
#we want k nodes to be connected to each other
connections = np.zeros([k+1,k+1])
ii = 0
while ii < k+1:
i = int(random.randint(1,k))
j = int(random.randint(1,k))
if (i > j )& (connections[j,i] ==0):
connections[j,i] = 1
ii = ii+1
print (j,i)
if (i < j) & (connections[i,j] ==0):
connections[i,j] = 1
ii = ii+1
print (i,j)
return connections
def mainrun(auto):
if auto == 1:
k = 6
else :
k = auto
# connections = create(k)
# we can create random connections
connections = np.zeros([k+1,k+1])
connections[1,3] = 1
connections[2,3] = 1
connections[4,5] = 1
connections[1,2] = 1
connections[3,4] = 1
connections[4,6] = 1
connections[5,6] = 1
if auto != 1:
connections = create(k)
v1,v2,minCost, tic = annealing(k, connections)
toc = time.clock()
tim = toc - tic
print "time taken ", tim
print "Final Cost", minCost
print v1
print v2