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=[];
101while norm(u-u_1,1)>1e-10
102 u_1=u;
103 for i=1:M
104 u(i)=1/(sum(N)+M);
105 for r=1:R
106 u(i)=u(i)+N(r)/(sum(N)+M)*L(i,r)*u_1(i)/(u_1'*L(:,r));
107 end
108 end
109 d(end+1,:)=abs(u-u_1)';
110end
111end
112
113function [u,v,d]=pfqn_le_fpiZ(L,N,Z)
114% [U,V,D]=PFQN_LE_FPIZ(L,N,Z)
115
116% find location of mode of gaussian
117[M,R]=size(L);
118eta = sum(N)+M;
119u=ones(M,1)/M;
120v=eta+1;
121u_1=Inf*u;
122v_1=Inf*v; %#ok<NASGU>
123d=[];
124while norm(u-u_1,1)>1e-10
125 u_1=u;
126 v_1=v;
127 for ist=1:M
128 u(ist)=1/eta;
129 for r=1:R
130 u(ist)=u(ist)+(N(r)/eta)*(Z(r)+v*L(ist,r))*u_1(ist)/(Z(r)+v*u_1'*L(:,r));
131 end
132 end
133 for r=1:R
134 xi(r)=N(r)/(Z(r)+v*u_1(:)'*L(:,r));
135 end
136 v=eta+1;
137 for r=1:R
138 v=v-xi(r)*Z(r);
139 end
140 d(end+1,:)=abs(u-u_1)'+abs(v-v_1);
141end
142
143end
144
145function hu=pfqn_le_hessian(L,N,u0)
146% HU=PFQN_LE_HESSIAN(L,N,U0)
147
148% find hessian of gaussian
149[M,R]=size(L);
150Ntot=sum(N);
151hu=zeros(M-1);
152for i=1:(M-1)
153 for j=1:(M-1)
154 if i~=j
155 hu(i,j)=-(Ntot+M)*u0(i)*u0(j);
156 for r=1:R
157 hu(i,j)=hu(i,j)+N(r)*L(i,r)*L(j,r)*(u0(i)*u0(j))/(u0*L(:,r))^2;
158 end
159 else % i=j
160 hu(i,j)=(Ntot+M)*u0(i)*sum(allbut(u0,i));
161 for r=1:R
162 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;
163 end
164 end
165 end
166end
167end
168
169function A=pfqn_le_hessianZ(L,N,Z,u,v)
170% A=PFQN_LE_HESSIANZ(L,N,Z,U,V)
171
172% find hessian of gaussian
173[K,R]=size(L);
174Ntot=sum(N);
175A=zeros(K);
176csi = zeros(1,R);
177for r=1:R
178 csi(r)=N(r)/(Z(r)+v*u*L(:,r));
179end
180Lhat = zeros(K,R);
181for k=1:K
182 for r=1:R
183 Lhat(k,r)=Z(r)+v*L(k,r);
184 end
185end
186eta=Ntot+K;
187for i=1:K
188 for j=1:K
189 if i~=j
190 A(i,j)=-eta*u(i)*u(j);
191 for r=1:R
192 A(i,j)=A(i,j)+csi(r)^2*Lhat(i,r)*Lhat(j,r)*(u(i)*u(j))/N(r);
193 end
194 end
195 end
196end
197for i=1:K
198 A(i,i)=-sum(allbut(A(i,:),i));
199end
200A=A(1:(K-1),1:(K-1));
201A(K,K)=1;
202for r=1:R
203 A(K,K)=A(K,K)-(csi(r)^2/N(r))*Z(r)*u*L(:,r);
204end
205A(K,K)=v*A(K,K);
206for i=1:(K-1)
207 A(i,K)=0;
208 for r=1:R
209 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));
210 end
211 A(K,i)=A(i,K);
212end
213end
214
215function y=allbut(y,xset)
216% Y=ALLBUT(Y,XSET)
217
218y=y(setdiff(1:length(y),xset));
219end
220
221function mln=multinomialln(n)
222% MLN=MULTINOMIALLN(N)
223
224mln = factln(sum(n))- sum(factln(n));
225end
226
227function lf=factln(n)
228% LF=FACTLN(N)
229
230lf = gammaln(1+n);
231end
232
233function f=simplex_fun(x,L,N)
234% F=SIMPLEX_FUN(X,L,N)
235
236x=x';
237M=length(x)+1;
238v=[];
239for i=1:(M-1)
240 v(i)=exp(x(i));
241end
242v(M)=1;
243
244f=exp(sum(N*log(v(:)'*L)')+sum(x)-(sum(N)+M)*log(sum(v)));
245end
246