LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
MG1_RR.m
1function G=MG1_RR(D,varargin)
2%MG1_RR Ramaswami Reduction based Algorithm for M/G/1-Type Markov Chains
3% [Bini,Meini,Ramaswami]
4%
5% G=MG1_RR(A) computes the minimal nonnegative solution to the
6% matrix equation G = A0 + A1 G + A2 G^2 + A3 G^3 + ... + A_max G^max,
7% where A = [A0 A1 A2 A3 ... A_max] has m rows and m*max columns and is
8% a nonnegative matrix, with (A0+A1+A2+...+A_max) irreducible and
9% stochastic
10%
11% Optional Parameters:
12%
13% MaxNumIt: Maximum number of iterations (default: 50)
14% Verbose: The residual error is printed at each step when set to 1,
15% (default:0)
16% Mode: 'Direct' does not rely on the displacement structure,
17% Requirements: memory O(m^2N^2), time O(m^3N^3)
18% 'DispStruct' makes use of the displacement structure,
19% Requirements: memory O(m^2N), time O(m^3N^2)
20% 'DispStructFFT' uses the displacement structure and FFTs
21% Requirements: memory O(m^2N), time O(m^2NlogN+m^3N)
22% (default: 'DispStruct')
23
24OptionNames=['Mode ';
25 'MaxNumIt ';
26 'Verbose '];
27OptionTypes=['char ';
28 'numeric';
29 'numeric'];
30OptionValues{1}=['Direct ';
31 'DispStruct ';
32 'DispStructFFT'];
33
34options=[];
35for i=1:size(OptionNames,1)
36 options.(deblank(OptionNames(i,:)))=[];
37end
38
39% Default settings
40%options.ProgressBar=0;
41options.Mode='Direct';
42options.MaxNumIt=50;
43options.Verbose=0;
44
45% Parse Optional Parameters
46options=ParseOptPara(options,OptionNames,OptionTypes,OptionValues,varargin);
47
48% check whether G is known explicitly
49G=MG1_EG(D,options.Verbose);
50if (~isempty(G))
51 return
52end
53
54% Start RR
55m=size(D,1);
56N=size(D,2)/m-1;
57D0=D(:,1:m);
58vT=D(:,m+1:end);
59uhat=zeros(N*m,m);
60uhat(1:m,1:m)=eye(m);
61
62if (strcmp(options.Mode,'Direct'))
63 B=[zeros(m,N*m); eye((N-1)*m) zeros((N-1)*m,m)];
64 check=min(norm(B,inf),norm(D0,inf));
65
66 % ITERATIONS
67 numit=0;
68 while (check > 10^(-15) && numit < options.MaxNumIt)
69 numit=numit+1;
70
71 % step 1:
72 S=inv(eye(m)-vT*uhat);
73 ZZTuhat=[zeros(m,m) ; uhat(m+1:N*m,:)];
74 T=vT*ZZTuhat;
75 T=S*T;
76
77 % step 2:
78 newD0=D0*(S*vT(:,1:m)+T+eye(m))*D0;
79
80 % step 3:
81 % Use (7) of Theorem 3, Section 3.1
82 newB=vT*B;
83 newB=S*newB;
84 newB=uhat*newB;
85 newB=B+newB;
86 newB=B*newB;
87 % we continue with updating uhat and vT
88 temp=uhat*(S*vT(:,1:m)+T+eye(m))*D0;
89 newuhat=uhat+B*temp;
90 temp=D0*S;
91 temp=temp*vT;
92 newvT=vT+temp*B;
93 % update other variables
94 B=newB;
95 vT=newvT;
96 uhat=newuhat;
97 D0=newD0;
98
99 check=min(norm(B,inf),norm(D0,inf));
100 if (options.Verbose==1)
101 fprintf('Check value of iteration %d equals %d \n',numit,check);
102 drawnow;
103 end
104 end
105else
106 b=zeros(N*m,m);
107 b(m+1:2*m,1:m)=eye(m);
108 c1=zeros(m*N,m);
109 c2=zeros(m*N,m);
110 r1=zeros(m*N,m);
111 r2=zeros(m*N,m);
112
113 first=1;
114 check=min(sum(sum(b)),norm(D0,inf));
115 % ITERATIONS
116 numit=0;
117 while ((check > 10^(-15) || first == 1) && numit < options.MaxNumIt)
118 numit=numit+1;
119 first=0;
120
121 % step 1:
122 S=inv(eye(m)-vT*uhat);
123 temp=[zeros(m,m) ; uhat(m+1:N*m,:)]; %% ZZTuhat
124 T=vT*temp;
125 T=S*T;
126
127 % step 2:
128 newD0=D0*(S*vT(:,1:m)+T+eye(m))*D0;
129
130 % step 3:
131 temp=uhat*(S*vT(:,1:m)+T+eye(m))*D0;
132 if (strcmp(options.Mode,'DispStruct'))
133 MG1_RR_Btemp;
134 else
135 MG1_RR_BtempFFT;
136 end
137 newuhat=uhat+temp;
138 temp=D0*S;
139 temp=temp*vT;
140 if (strcmp(options.Mode,'DispStruct'))
141 MG1_RR_tempB;
142 else
143 MG1_RR_tempBFFT;
144 end
145 newvT=vT+temp;
146
147 % step 4:
148 % we start by calculating (I-A)^(-1) b using the structure of (I-A)^-1
149 temp=[vT*b; b(1:m,1:m)];
150 temp=[S T; S eye(m)+T]*temp;
151 newb(1:m,1:m)=b(1:m,1:m)+temp(1:m,:);
152 newb(m+1:N*m,1:m)=b(m+1:end,1:m)+uhat(m+1:end,:)*temp(m+1:end,:);
153 temp=newb;
154 if (strcmp(options.Mode,'DispStruct'))
155 MG1_RR_Btemp;
156 else
157 MG1_RR_BtempFFT;
158 end
159 newb=temp;
160
161 % step 5, part 1:
162 % H
163 e1pZu=[eye(m); uhat(m+1:end,:)];
164 Z2u=[zeros(2*m,m); uhat(m+1:(N-1)*m,:)];
165 e2pZ2u=[zeros(m,m); eye(m); uhat(m+1:(N-1)*m,:)];
166 H=[e1pZu e2pZ2u*S e2pZ2u*T+Z2u];
167
168 clear e1pZu;
169 clear Z2u;
170 clear e2pZ2u;
171
172 % KT
173 temp=[vT(:,m+1:end) zeros(m,m)];
174 KT=[S*temp; -vT; [-eye(m) zeros(m,(N-1)*m)]];
175
176 % W
177 W=[c1 c2];
178 if (strcmp(options.Mode,'DispStruct'))
179 temp=H;
180 MG1_RR_Btemp;
181 W(1:N*m,2*m+1:5*m)=temp;
182 else
183 for colp=0:2
184 temp=H(:,colp*m+1:(colp+1)*m);
185 MG1_RR_BtempFFT;
186 W(1:N*m,(2+colp)*m+1:(3+colp)*m)=temp;
187 end
188 end
189 clear H;
190 temp=[vT*[c1 c2]; [c1(1:m,1:m) c2(1:m,1:m)]];
191 temp=[S T; S eye(m)+T]*temp;
192 temp2(1:m,1:2*m)=[c1(1:m,1:m) c2(1:m,1:m)]+temp(1:m,:);
193 temp2(m+1:N*m,1:2*m)=[c1(m+1:end,1:m) c2(m+1:end,1:m)]+...
194 uhat(m+1:end,:)*temp(m+1:end,:);
195 if (strcmp(options.Mode,'DispStruct'))
196 temp=temp2;
197 clear temp2;
198 MG1_RR_Btemp;
199 W(1:N*m,5*m+1:7*m)=temp;
200 else
201 supertemp2=temp2;
202 clear temp2;
203 for colp=0:1
204 temp=supertemp2(:,colp*m+1:(1+colp)*m);
205 MG1_RR_BtempFFT;
206 W(1:N*m,(5+colp)*m+1:(6+colp)*m)=temp;
207 end
208 clear supertemp2;
209 end
210 clear temp;
211
212 % Step 6, part 1 (this order allows us to clear W more rapidly)
213 [Q1,R1]=qr(W,0);
214 clear W;
215
216 % YT
217 YT(5*m+1:7*m,1:N*m)=[r1 r2]';
218 if (strcmp(options.Mode,'DispStruct'))
219 temp=KT;
220 MG1_RR_tempB;
221 YT(2*m+1:5*m,1:N*m)=temp;
222 else
223 for rowp=0:2
224 temp=KT(rowp*m+1:(1+rowp)*m,:);
225 MG1_RR_tempBFFT;
226 YT((2+rowp)*m+1:(3+rowp)*m,1:N*m)=temp;
227 end
228 end
229 clear KT;
230 Zu=[zeros(m,m); uhat(m+1:end,:)];
231 temp=[[r1(1:m,1:m) r2(1:m,1:m)]' [r1 r2]'*Zu];
232 temp=temp*[S T; S eye(m)+T];
233 temp2=temp(:,1:m)*vT;
234 temp2(:,1:m)=temp2(:,1:m)+temp(:,m+1:2*m);
235 if (strcmp(options.Mode,'DispStruct'))
236 temp=temp2+[r1 r2]';
237 clear temp2;
238 MG1_RR_tempB;
239 YT(1:2*m,1:N*m)=temp;
240 else
241 supertemp2=temp2+[r1 r2]';
242 clear temp2;
243 for rowp=0:1
244 temp=supertemp2(rowp*m+1:(1+rowp)*m,:);
245 MG1_RR_tempBFFT;
246 YT(rowp*m+1:(1+rowp)*m,1:N*m)=temp;
247 end
248 clear supertemp2;
249 end
250 clear temp;
251
252 % Step 6, part 2:
253 [Q2,R2]=qr(YT',0);
254 clear YT;
255
256 % Step 7:
257 [U,Xsi,V]=svd(R1*R2');
258 for temp=1:2*m
259 first2m(temp)=Xsi(temp,temp);
260 end
261 clear Xsi;
262 clear R1;
263 clear R2;
264
265 % Step 8:
266 temp=Q1*U(:,1:2*m)*diag(first2m);
267 clear first2m;
268 clear Q1;
269 clear U;
270 c1=temp(:,1:m);
271 c2=temp(:,m+1:2*m);
272 temp=(V(:,1:2*m)'*Q2')';
273 clear Q2;
274 clear V;
275 r1=temp(:,1:m);
276 r2=temp(:,m+1:2*m);
277
278 % update other variables
279 b=newb;
280 vT=newvT;
281 uhat=newuhat;
282 D0=newD0;
283
284 clear newb;
285 clear newvT;
286 clear newuhat;
287 clear newD0;
288
289 check=min(sum(sum(b)),norm(D0,inf));
290 if (options.Verbose==1)
291 fprintf('Check value of iteration %d equals %d \n',numit,check);
292 drawnow;
293 end
294
295 end
296end
297if (numit == options.MaxNumIt && check > 10^(-15))
298 warning('Maximum Number of Iterations %d reached',numit);
299end
300
301% Compute G by means of formula at the end of Section 3.
302temp=eye(m)-D(:,m+1:end)*uhat;
303G=temp^(-1)*D(:,1:m);
304
305if (options.Verbose==1)
306 temp=D(:,end-m+1:end);
307 for i=size(D,2)/m-1:-1:1
308 temp=D(:,(i-1)*m+1:i*m)+temp*G;
309 end
310 res_norm=norm(G-temp,inf);
311 fprintf('Final Residual Error for G: %d\n',res_norm);
312end
313
314
315
316