-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmax-cut0.py
101 lines (70 loc) · 1.71 KB
/
max-cut0.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
import csv
import numpy as np
from numpy import genfromtxt
from cvxopt import matrix, solvers
import cvxopt as cvx
import cvxopt.lapack
from scipy.sparse import csgraph
import picos as pic
import networkx as nx
from cvxpy import *
#Reading the adjacency matrix
graph = genfromtxt('Graph_1.csv', delimiter=',')
#print(graph.shape)
#Graph laplacian:
Ll = 1/4*csgraph.laplacian(graph, normed=False)
Ll = pic.tools._retrieve_matrix(Ll)
Ll = Ll[0]
#Problem setup
N = graph.shape[1]
X = Variable(N,N)
X = pic.tools._retrieve_matrix(X)
X = X[0]
objective = Maximize(trace(Ll*X))
#------Constraints----------
#X positive semidefinite
constr = [(X >> 0)]
#ones on the diagonal
diag_val = []
for i in range(N):
diag_val.append(1)
constr1 = (diag(X) == diag_val)
constr.append(constr1)
#objective
prob = Problem(objective, constr)
print('bound from the SDP relaxation: {0}'.format(prob.solve()))
'''
#Cholesky decomposition
V=X.value
cvxopt.lapack.potrf(V)
for i in range(N):
for j in range(i+1,N):
V[i,j]=0
#random projection algorithm
#Repeat 100 times or until we are within a factor .878 of the SDP optimal value
count=0
obj_sdp=maxcut.obj_value()
obj=0
while (count <2 or obj<.878*obj_sdp):
r=cvx.normal(N,1)
#print(V,r)
x=cvx.matrix(np.sign(V*r))
o=(x.T*L*x).value[0]
if o>obj:
x_cut=x
obj=o
count+=1
#original Objectve value from randomized rounding
print("Objective value:", o)
#Preparing for output
for i in range(len(x)):
if x[i] == -1:
x[i] = 0
x[i] = int(x[i])
final_x = list(map(int, x))
#print(final_x)
#Output
with open('6_sol', 'w') as myfile:
wr = csv.writer(myfile)
wr.writerow(final_x)
'''