LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
pfqn_ls.m
1%{
2%{
3 % @file pfqn_ls.m
4 % @brief Logistic sampling approximation for normalizing constant.
5%}
6%}
7
8%{
9%{
10 % @brief Logistic sampling approximation for normalizing constant.
11 % @fn pfqn_ls(L, N, Z, I)
12 % @param L Service demand matrix (MxR).
13 % @param N Population vector (1xR).
14 % @param Z Think time vector (1xR).
15 % @param I Number of samples (default: 1e5).
16 % @return Gn Estimated normalizing constant.
17 % @return lGn Logarithm of normalizing constant.
18%}
19%}
20function [Gn,lGn]=pfqn_ls(L,N,Z,I)
21% [GN,LGN]=PFQN_LS(L,N,Z,I)
22
23% PFQN_MCI Approximate solution of closed product-form queueing networks
24% by logistic sampling
25%
26% [Gn,lGn]=pfqn_ls(L,N,Z,I)
27% Input:
28% L : MxR demand matrix. L(i,r) is the demand of class-r at queue i
29% N : 1xR population vector. N(r) is the number of jobs in class r
30% Z : 1xR think time vector. Z(r) is the total think time of class r
31% I : number of samples (default: 1e5)
32%
33% Output:
34% Gn : estimated normalizing constat
35%
36% Reference:
37% G. Casale. Accelerating performance inference over closed systems by
38% asymptotic methods. ACM SIGMETRICS 2017.
39% Available at: http://dl.acm.org/citation.cfm?id=3084445
40
41Lsum = sum(L,2);
42L = L(Lsum > 1e-4,:);
43[M,R]=size(L);
44samples=[];
45
46if isempty(L) || sum(L(:))<1e-4 || isempty(N) || sum(N)==0
47 lGn = - sum(factln(N)) + sum(N.*log(sum(Z,1)));
48elseif nargin<3 || isempty(Z) %~exist('Z','var') || isempty(Z)
49 umax=pfqn_le_fpi(L,N);
50 A = pfqn_le_hessian(L,N,umax'); % slightly faster than pfqn_le_hessianZ
51 A = (A+A')/2; % let's get rid of small numerical perturbations
52 iA = inv(A);
53 x0 = log(umax(1:M-1)/umax(M))'; % move to R^{K-1}
54 if isempty(samples)
55 samples = mvnrnd(x0,iA,I);
56 end
57 T = zeros(I,1);
58 h = @(x) simplex_fun(x,L,N);
59 for i=1:I
60 T(i) = h(samples(i,:));
61 end
62 dpdf=[];
63 for i=1:I
64 dpdf(i)=mvnpdf(samples(i,:)',x0,iA);
65 end
66 lGn = multinomialln([N,M-1]) + factln(M-1) + log(mean(T(:)./dpdf(:)));
67 Gn = exp(lGn);
68else % Z>0
69 [umax,vmax]=pfqn_le_fpiZ(L,N,Z);
70 A = pfqn_le_hessianZ(L,N,Z,umax',vmax);
71 A = (A+A')/2; % let's get rid of small numerical perturbations
72 iA = inv(A);
73 x0 = [log(umax(1:M-1)/umax(M))',log(vmax)]; % move to R^{K}
74 if isempty(samples)
75 samples = mvnrnd(x0,iA,I);
76 end
77 T = zeros(I,1);
78 epsilon=1e-10;
79 eN = epsilon*sum(N);
80 eta = sum(N)+M*(1+eN);
81 K=M;
82 h = @(x)exp(-exp(x(K))+K*(1+eN)*x(M)+sum(N*log( (L(K,:)*exp(x(K))+Z) + exp(x(1:K-1))*(L(1:K-1,:)*exp(x(K))+repmat(Z,K-1,1)))') +sum(x(1:K-1)) -eta*log(1+sum(exp(x(1:K-1)))));
83 for i=1:I
84 T(i) = h(samples(i,:));
85 end
86 dpdf = mvnpdf(samples,x0,iA)';
87 Gn = exp(-sum(gammaln(1+N))) * mean(T./dpdf(:));
88 lGn = log(Gn);
89end
90Gn=exp(lGn);
91end
92
93function [u,d]=pfqn_le_fpi(L,N)
94% [U,D]=PFQN_LE_FPI(L,N)
95
96% find location of mode of gaussian
97[M,R]=size(L);
98u=ones(M,1)/M;
99u_1=Inf*u;
100d=[];
101coder.varsize('d');
102while norm(u-u_1,1)>1e-10
103 u_1=u;
104 for i=1:M
105 u(i)=1/(sum(N)+M);
106 for r=1:R
107 u(i)=u(i)+N(r)/(sum(N)+M)*L(i,r)*u_1(i)/(u_1'*L(:,r));
108 end
109 end
110 d=[d; abs(u-u_1)'];
111end
112end
113
114function [u,v,d]=pfqn_le_fpiZ(L,N,Z)
115% [U,V,D]=PFQN_LE_FPIZ(L,N,Z)
116
117% find location of mode of gaussian
118[M,R]=size(L);
119eta = sum(N)+M;
120u=ones(M,1)/M;
121v=eta+1;
122u_1=Inf*u;
123v_1=Inf*v; %#ok<NASGU>
124d=[];
125coder.varsize('d');
126while norm(u-u_1,1)>1e-10
127 u_1=u;
128 v_1=v;
129 for ist=1:M
130 u(ist)=1/eta;
131 for r=1:R
132 u(ist)=u(ist)+(N(r)/eta)*(Z(r)+v*L(ist,r))*u_1(ist)/(Z(r)+v*u_1'*L(:,r));
133 end
134 end
135 for r=1:R
136 xi(r)=N(r)/(Z(r)+v*u_1(:)'*L(:,r));
137 end
138 v=eta+1;
139 for r=1:R
140 v=v-xi(r)*Z(r);
141 end
142 d=[d; abs(u-u_1)'+abs(v-v_1)];
143end
144
145end
146
147function hu=pfqn_le_hessian(L,N,u0)
148% HU=PFQN_LE_HESSIAN(L,N,U0)
149
150% find hessian of gaussian
151[M,R]=size(L);
152Ntot=sum(N);
153hu=zeros(M-1);
154for i=1:(M-1)
155 for j=1:(M-1)
156 if i~=j
157 hu(i,j)=-(Ntot+M)*u0(i)*u0(j);
158 for r=1:R
159 hu(i,j)=hu(i,j)+N(r)*L(i,r)*L(j,r)*(u0(i)*u0(j))/(u0*L(:,r))^2;
160 end
161 else % i=j
162 hu(i,j)=(Ntot+M)*u0(i)*sum(allbut(u0,i));
163 for r=1:R
164 hu(i,j)=hu(i,j)-N(r)*L(i,r)*u0(i)*(allbut(u0,i)*L(allbut(1:M,i),r))/(u0*L(:,r))^2;
165 end
166 end
167 end
168end
169end
170
171function A=pfqn_le_hessianZ(L,N,Z,u,v)
172% A=PFQN_LE_HESSIANZ(L,N,Z,U,V)
173
174% find hessian of gaussian
175[K,R]=size(L);
176Ntot=sum(N);
177A=zeros(K);
178csi = zeros(1,R);
179for r=1:R
180 csi(r)=N(r)/(Z(r)+v*u*L(:,r));
181end
182Lhat = zeros(K,R);
183for k=1:K
184 for r=1:R
185 Lhat(k,r)=Z(r)+v*L(k,r);
186 end
187end
188eta=Ntot+K;
189for i=1:K
190 for j=1:K
191 if i~=j
192 A(i,j)=-eta*u(i)*u(j);
193 for r=1:R
194 A(i,j)=A(i,j)+csi(r)^2*Lhat(i,r)*Lhat(j,r)*(u(i)*u(j))/N(r);
195 end
196 end
197 end
198end
199for i=1:K
200 A(i,i)=-sum(allbut(A(i,:),i));
201end
202A=A(1:(K-1),1:(K-1));
203A(K,K)=1;
204for r=1:R
205 A(K,K)=A(K,K)-(csi(r)^2/N(r))*Z(r)*u*L(:,r);
206end
207A(K,K)=v*A(K,K);
208for i=1:(K-1)
209 A(i,K)=0;
210 for r=1:R
211 A(i,K)=A(i,K)+v*u(i)*((csi(r)^2/N(r))*Lhat(i,r)*(u*L(:,r))-csi(r)*L(i,r));
212 end
213 A(K,i)=A(i,K);
214end
215end
216
217function y=allbut(y,xset)
218% Y=ALLBUT(Y,XSET)
219
220y=y(setdiff(1:length(y),xset));
221end
222
223function mln=multinomialln(n)
224% MLN=MULTINOMIALLN(N)
225
226mln = factln(sum(n))- sum(factln(n));
227end
228
229function lf=factln(n)
230% LF=FACTLN(N)
231
232lf = gammaln(1+n);
233end
234
235function f=simplex_fun(x,L,N)
236% F=SIMPLEX_FUN(X,L,N)
237
238x=x';
239M=length(x)+1;
240v=[];
241for i=1:(M-1)
242 v(i)=exp(x(i));
243end
244v(M)=1;
245
246f=exp(sum(N*log(v(:)'*L)')+sum(x)-(sum(N)+M)*log(sum(v)));
247end
248