-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfmmc.py
More file actions
83 lines (71 loc) · 2.4 KB
/
fmmc.py
File metadata and controls
83 lines (71 loc) · 2.4 KB
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
"""
Copyright 2013 Steven Diamond
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
#!/usr/bin/env python
# Keith Briggs 2014-08-19
# Fastest-mixing Markov chain
# Boyd, Diaconis, and Xiao SIAM Rev. 46 (2004) 667-689 at p.672
import numpy as np
import cvxpy
def antiadjacency(g):
# form the complementary graph
n=1+max(g.keys()) # assumes keys start at 0
a=dict((i,[]) for i in range(n))
for x in g:
for y in range(n):
if y not in g[x]:
a[x].append(y)
return a
def FMMC(g,verbose: bool = False):
# Fastest-mixing Markov chain on the graph g
# this is formulation (5), p.672
# Boyd, Diaconis, and Xiao SIAM Rev. 46 (2004) 667-689
a=antiadjacency(g)
n=len(a.keys())
P=cvxpy.Variable(n,n)
o=np.ones(n)
objective=cvxpy.Minimize(cvxpy.norm(P-1.0/n))
constraints=[P*o==o,P.T==P,P>=0]
for i in a:
for j in a[i]: # i-j is a not-edge of g!
if i!=j: constraints.append(P[i,j]==0)
prob=cvxpy.Problem(objective,constraints)
prob.solve()
if verbose: print('status: %s.'%prob.status,'optimal value=%.6f'%prob.value)
return prob.status,prob.value,P.value
def print_result(P, n, eps: float = 1e-8):
for row in P:
for i in range(n):
x=row[0,i]
if abs(x)<eps: x=0.0
print('%8.4f'%x)
print
def examples_p674():
print('SIAM Rev. 46 examples p.674: Figure 1 and Table 1')
print('(a) line graph L(4)')
g={0:(1,),1:(0,2,),2:(1,3,),3:(2,)}
status,value,P=FMMC(g,verbose=True)
print_result(P,len(g))
print('(b) triangle+one edge')
g={0:(1,),1:(0,2,3,),2:(1,3,),3:(1,2,)}
status,value,P=FMMC(g,verbose=True)
print_result(P,len(g))
print('(c) bipartite 2+3')
g={0:(1,3,4,),1:(0,2,),2:(1,3,4,),3:(0,2,),4:(0,2,)}
status,value,P=FMMC(g,verbose=True)
print_result(P,len(g))
print('(d) square+central point')
g={0:(1,2,4,),1:(0,3,4,),2:(0,3,4,),3:(1,2,4,),4:(0,1,2,3,4,)}
status,value,P=FMMC(g,verbose=True)
print_result(P,len(g))
if __name__=='__main__':
examples_p674()