-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetWeights.m
139 lines (97 loc) · 3.11 KB
/
getWeights.m
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
clear all
clc
% Boyd & Vandenberghe "Convex Optimization"
% Joëlle Skaf - 09/26/05
%
% The 'fastest mixing Markov chain problem' is to find a transition
% probability matrix P on a graph E that minimizes the mixing rate r, where
% r = max{ lambda_2, -lambda_n } with lambda_1>=...>=lambda_n being the
% eigenvalues of P.
% you need to have the cvx package to run this
% runs in linear time with respect to adding nodes.
%INPUT WIDTH OF SQUARE GRID
%ONLY THING YOU NEED TO CHANGE
k = 10;
%number of cells
n = k^2;
%adjacency matrix
E = zeros(n,n);
for cell = 1:n
% has all eight neighbors
if(mod(cell,k) ~= 0 && mod(cell-1,k) ~= 0 && cell < (n-k) && cell > (k+1))
E(cell, cell - 1) = 1;
E(cell, cell + 1) = 1;
E(cell, cell - k) = 1;
E(cell, cell + k) = 1;
E(cell, cell - (k+1)) = 1;
E(cell, cell + (k+1)) = 1;
E(cell, cell - (k-1)) = 1;
E(cell, cell + (k-1)) = 1;
% bottom left corner
elseif(cell == 1)
E(cell, cell + 1) = 1;
E(cell, cell + k) = 1;
E(cell, cell + (k+1)) = 1;
% bottom right corner
elseif(cell == k)
E(cell, cell - 1) = 1;
E(cell, cell + k) = 1;
E(cell, cell + (k-1)) = 1;
% top left corner
elseif(cell == (n-k+1))
E(cell, cell + 1) = 1;
E(cell, cell - k) = 1;
E(cell, cell - (k-1)) = 1;
% top right corner
elseif(cell == n)
E(cell, cell - 1) = 1;
E(cell, cell - k) = 1;
E(cell, cell - (k+1)) = 1;
% is on left edge
elseif(mod(cell-1,k) == 0)
E(cell, cell + 1) = 1;
E(cell, cell - k) = 1;
E(cell, cell + k) = 1;
E(cell, cell + (k+1)) = 1;
E(cell, cell - (k-1)) = 1;
% is on right edge
elseif(mod(cell,k) == 0)
E(cell, cell - 1) = 1;
E(cell, cell - k) = 1;
E(cell, cell + k) = 1;
E(cell, cell - (k+1)) = 1;
E(cell, cell + (k-1)) = 1;
% is on top edge
elseif(cell > (n-k+1))
E(cell, cell - 1) = 1;
E(cell, cell + 1) = 1;
E(cell, cell - k) = 1;
E(cell, cell - (k+1)) = 1;
E(cell, cell - (k-1)) = 1;
% is on bottom edge
elseif(cell < k)
E(cell, cell - 1) = 1;
E(cell, cell + 1) = 1;
E(cell, cell + k) = 1;
E(cell, cell + (k+1)) = 1;
E(cell, cell + (k-1)) = 1;
end
end
% Create and solve model
cvx_begin
variable P(n,n) symmetric
minimize(norm(P - (1/n)*ones(n)))
P*ones(n,1) == ones(n,1);
P >= 0;
P(E==0) == 0;
cvx_end
e = flipud(eig(P));
r = max(e(2), -e(n));
% Display results
disp('------------------------------------------------------------------------');
disp('The transition probability matrix of the optimal Markov chain is: ');
disp('The optimal mixing rate is: ');
disp(r);
% rounds probabilities to 4 decimal places
Y = round(P, 4);
disp(Y);