This repository has been archived by the owner on Mar 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.py
209 lines (192 loc) · 4.81 KB
/
helper.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import numpy as np
from scipy.misc import comb
import sys
from Hamiltonian import Hamiltonian
PROB_TYPE = sys.argv[1]
def expr_g (gij):
return {
'max_clique': 1-gij,
'max_vertex_indep': gij,
'min_vertex_cover': gij,
'min_unknown': 1-gij
}[PROB_TYPE]
def expr_z (z):
return {
'max_clique': z,
'max_vertex_indep': z,
'min_vertex_cover': 1-z,
'min_unknown': 1-z
}[PROB_TYPE]
# Get Hamming weight of binary number
def Hamming (num):
return bin(num).count('1')
# Get idx-th bit from right
def Bit (num, idx):
return (num >> idx) & 1
# Solve problem classically; return size of answer and array of answers
def classical_solve (n, G):
return {
'max_clique': classical_solve_max(n, G),
'max_vertex_indep': classical_solve_max(n, G),
'min_vertex_cover': classical_solve_min(n, G),
'min_unknown': classical_solve_min(n, G)
}[PROB_TYPE]
def classical_solve_max (n, G):
for k in range(8, 0, -1):
ans = []
cnk, rank, knar = compress(n, k)
for z in knar:
flag = True
index = []
for i in range(n):
if expr_z(Bit(z, i)):
index.append(i)
for i in index:
for j in index:
if i != j and expr_g(G[i][j]):
flag = False
break
if not flag:
break
if flag:
ans.append(z)
if ans != []:
break
return k, ans
'''
k = 0
ans = []
for z in range(2**n):
flag = True
for i in range(n):
for j in range(i):
if expr_g(G[i][j]) and expr_z(Bit(z, i)) and expr_z(Bit(z, j)):
flag = False
break
if not flag:
break
if flag:
if k < Hamming(z):
ans = []
k = Hamming(z)
if k == Hamming(z):
ans.append(z)
return k, ans
'''
def classical_solve_min (n, G):
k = n+1
ans = []
for z in range(2**n):
flag = True
for i in range(n):
for j in range(i):
if expr_g(G[i][j]) and expr_z(Bit(z, i)) and expr_z(Bit(z, j)):
flag = False
break
if not flag:
break
if flag:
if k > Hamming(z):
ans = []
k = Hamming(z)
if k == Hamming(z):
ans.append(z)
return k, ans
# Compute subset information
# Input
# n: size of set
# k: size of subset
# Output
# cnk: n choose k
# rank: map from binary number to rank in subset list
# knar: map from rank in subset list to binary number
def compress (n, k):
cnk = int(comb(n, k))
rank = {}
knar = np.zeros(cnk, dtype=int)
pos = 0
z = (1 << k) - 1
while z < (1 << n):
rank[z] = pos
knar[pos] = z
pos += 1
x = z & -z
y = z + x
z = (((z & ~y) / x) >> 1) | y
return cnk, rank, knar
# Compute Hamiltonian beginning
# Complexity: cnk * n^2
def HB (n, k, cnk, knar, rank, off_index):
on_diag = np.repeat(-(k*(k-1)+(n-k)*(n-k-1)), cnk)
off_diag = -1
# off_index = np.zeros((cnk, k*(n-k)))
for ii in range(cnk):
pos = 0
for i in range(n):
for j in range(n):
if Bit(knar[ii], i) == 1 and Bit(knar[ii], j) == 0:
off_index[ii][pos] = rank[knar[ii] - (1<<i) + (1<<j)]
pos += 1
H_B = Hamiltonian(on_diag, off_diag, k*(n-k))
return H_B
def HP (n, k, cnk, knar, G):
on_diag = np.array([(sum([(expr_g(G[i][j]))*expr_z(Bit(knar[ii], i))*expr_z(Bit(knar[ii], j)) for i in range(n) for j in range(i)])) for ii in range(cnk)])
off_diag = 0
H_P = Hamiltonian(on_diag, off_diag, k*(n-k))
return H_P
'''
def classical_solve_clique (n, G):
return nx.algorithms.clique.graph_clique_number(nx.from_numpy_matrix(G)), []
def is_clique (z, G):
n = G.shape[0]
flag = True
for i in range(n):
for j in range(n):
if i != j and expr_g(G[i][j])*expr_z(Bit(z, i))*expr_z(Bit(z, j)):
flag = False
return flag
def HB_csc (n, k, cnk, knar, rank):
data = []
row_ind = []
col_ind = []
for ii in range(cnk):
data.append(-(k * (k-1) + (n-k) * (n-k-1)) / 2)
row_ind.append(ii)
col_ind.append(ii)
for i in range(n):
for j in range(n):
if Bit(knar[ii], i) == 1 and Bit(knar[ii], j) == 0:
data.append(-1)
row_ind.append(ii)
col_ind.append(rank[knar[ii] - (1<<i) + (1<<j)])
H_B = scipy.sparse.csc_matrix((data, (row_ind, col_ind)), shape=(cnk, cnk))
return H_B
def HP_csc (n, G, cnk, knar):
data = []
row_ind = []
col_ind = []
for ii in range(cnk):
data.append(sum([(expr_g(G[i][j]))*expr_z(Bit(knar[ii], i))*expr_z(Bit(knar[ii], j)) for i in range(n) for j in range(i)]))
row_ind.append(ii)
col_ind.append(ii)
H_P = scipy.sparse.csc_matrix((data, (row_ind, col_ind)), shape=(cnk, cnk))
return H_P
'''
def HB_diff (n, x, y):
ans = 0
for b in range(n):
if Bit(x, b) != Bit(y, b):
ans += 1
return ans
def HB_dense (n, k, cnk, knar):
H_B = np.zeros((cnk, cnk))
for i in range(cnk):
for j in range(cnk):
if i == j:
H_B[i][j] = -(k * (k-1) + (n-k) * (n-k-1)) / 2
elif HB_diff(n, knar[i], knar[j]) == 2:
H_B[i][j] = -1
return H_B
def HP_dense (n, G, cnk, knar):
H_P = np.diag([(sum([(expr_g(G[i][j]))*expr_z(Bit(knar[ii], i))*expr_z(Bit(knar[ii], j)) for i in range(n) for j in range(i)])) for ii in range(cnk)])
return H_P