LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
pfqn_nc.m
1% Computes the normalizing constant of a product-form queueing network.
2% [LG,X,Q,METHOD] = PFQN_NC(LAMBDA,L,N,Z,VARARGIN)
3function [lG,X,Q,method] = pfqn_nc(lambda,L,N,Z,varargin)
4
5options = Solver.parseOptions(varargin, SolverNC.defaultOptions);
6method = 'exact'; % if early return is triggered
7% backup initial parameters
8Rin = length(N);
9
10if any(N<0) || isempty(N)
11 lG = -Inf;
12 X = [];
13 Q = [];
14 return
15end
16
17if sum(N)==0
18 lG = 0;
19 X = [];
20 Q = [];
21 return
22end
23
24if isempty(lambda)
25 lambda=0*N;
26end
27
28X=[]; Q=[];
29
30% compute open class contributions
31Qopen = [];
32lGopen = 0;
33Ut = zeros(1, size(L,1));
34for i=1:size(L,1)
35 Ut(i) = (1-lambda*L(i,:)');
36 if isnan(Ut(i))
37 Ut(i) = 0;
38 end
39 L(i,:) = L(i,:)/Ut(i);
40 Qopen(i,:) = lambda.*L(i,:)/Ut(i);
41 %lGopen = lGopen + log(Ut(i));
42end
43Qopen(isnan(Qopen))=0;
44ocl = find(isinf(N));
45% then erase open classes
46N(isinf(N)) = 0;
47
48% first remove empty classes
49nnzClasses = find(N);
50lambda = lambda(:,nnzClasses);
51L = L(:,nnzClasses);
52N = N(:,nnzClasses);
53Z = Z(:,nnzClasses);
54
55% then scale demands in [0,1], importat that stays before the other
56% simplications in case both D and Z are all very small or very large in a
57% given class, in which case the may look to filter but not if all of them
58% are at the same scale
59R = length(N);
60scalevec = ones(1,R);
61%switch options.method
62% case {'adaptive','comom','default'}
63% % no-op
64% otherwise
65%end
66for r=1:R
67 scalevec(r) = max([L(:,r);Z(:,r)]);
68end
69%end
70L = L ./ repmat(scalevec,size(L,1),1);
71Z = Z ./ scalevec;
72
73% remove stations with no demand
74Lsum = sum(L,2);
75Lmax = max(L,[],2);
76demStations = find((Lmax./Lsum)>GlobalConstants.FineTol);
77noDemStations = setdiff(1:size(L,1), demStations);
78L = L(demStations,:);
79if any(N((sum(L,1) + sum(Z,1)) == 0)>0) % if there is a class with jobs but L and Z all zero
80 if options.verbose
81 line_warning(mfilename,'The model has no positive demands in any class.\n');
82 end
83 if isempty(Z) || sum(Z(:))<options.tol
84 lG = 0;
85 else
86 lG = - sum(factln(N)) + sum(N.*log(sum(Z,1))) + N*log(scalevec)';
87 end
88 return
89end
90
91% update M and R
92[M,R]=size(L);
93
94% return immediately if degenerate case
95if isempty(L) || sum(L(:))<options.tol % all demands are zero
96 if isempty(Z) || sum(Z(:))<options.tol
97 lG = lGopen;
98 else
99 lG = lGopen - sum(factln(N)) + sum(N.*log(sum(Z,1))) + N*log(scalevec)';
100 end
101 return
102elseif M==1 && (isempty(Z) || sum(Z(:))<options.tol) % single node and no think time
103 lG = factln(sum(N)) - sum(factln(N)) + sum(N.*log(L(1,:))) + N*log(scalevec)';
104 return
105elseif size(unique(L,'rows'),1)==1 && (isempty(Z) || sum(Z(:))<options.tol) % M identical replicas
106 lG = factln(sum(N)+M-1) - sum(factln(N)) + sum(N.*log(L(1,:))) + N*log(scalevec)' - factln(M-1);
107 return
108end
109
110% determine contribution from jobs that permanently loop at delay
111zeroDemandClasses = find(sum(L,1)<options.tol); % all jobs in delay
112nonzeroDemandClasses = setdiff(1:R, zeroDemandClasses);
113
114if isempty(sum(Z,1)) || all(sum(Z(:,zeroDemandClasses),1)<options.tol)
115 lGzdem = 0;
116 Nz = 0;
117else
118 if isempty(zeroDemandClasses) % for old MATLAB release compatibility
119 lGzdem = 0;
120 Nz = 0;
121 else
122 Nz = N(zeroDemandClasses);
123 lGzdem = - sum(factln(Nz)) + sum(Nz.*log(sum(Z(:,zeroDemandClasses),1))) + Nz*log(scalevec(zeroDemandClasses))';
124 end
125end
126L = L(:,nonzeroDemandClasses);
127N = N(nonzeroDemandClasses);
128Zz = Z(:,zeroDemandClasses);
129Z = Z(:,nonzeroDemandClasses);
130scalevecz = scalevec(nonzeroDemandClasses);
131% compute G for classes No with non-zero demand
132[lGnzdem,Xnnzdem,Qnnzdem,method] = compute_norm_const(L, N, Z, options);
133
134if isempty(Xnnzdem) % in this case the NC method does not return metrics as a by-product
135 X = [];
136 Q = [];
137else
138 zClasses = setdiff(1:Rin, nnzClasses);
139 Xz = zeros(1,length(zClasses));
140 Xnnz = zeros(1,length(nnzClasses));
141 Xnnz(zeroDemandClasses) = Nz./ sum(Zz,1)./ scalevec(zeroDemandClasses);
142 Xnnz(nonzeroDemandClasses) = Xnnzdem./ scalevec(nonzeroDemandClasses);
143 X(1,[zClasses, nnzClasses]) = [Xz, Xnnz];
144 X(ocl) = lambda(ocl);
145 Qz = zeros(size(Qnnzdem,1),length(zClasses));
146 Qnnz = zeros(size(Qnnzdem,1),length(nnzClasses));
147 Qnnz(:,zeroDemandClasses) = 0; % they are all in the delay
148 Qnnz(:,nonzeroDemandClasses) = Qnnzdem; % Q does not require scaling
149 Q(noDemStations,:) = 0;
150 Q(demStations,[zClasses, nnzClasses]) = [Qz, Qnnz];
151 Q(:,ocl) = Qopen(:,ocl);
152end
153% scale back to original demands
154lG = lGopen + lGnzdem + lGzdem + N*log(scalevecz)';
155end
156
157function [lG,X,Q,method] = compute_norm_const(L,N,Z,options)
158% LG = COMPUTE_NORM_CONST(L,N,Z,OPTIONS)
159% Auxiliary script that computes LG after the initial filtering of L,N,Z
160
161% Note: methods that can handle more efficiently replicas need to do so
162% within the method function
163
164% L,N,Z
165[M,R] = size(L);
166X=[];Q=[];
167method = options.method;
168switch options.method
169 case {'ca'}
170 [~,lG] = pfqn_ca(L,N,sum(Z,1));
171 case {'adaptive','default'}
172 if M>1
173 if sum(N)<1e3
174 % Sequence of Grundmann-Mueller cubature (CUB) executions
175 %
176 % This is slowish since CUB is O(N^M) but competitors fails
177 % 'imci' has occasionally large errors in the benchmarks and
178 % 'kt' fails with self-looping customers leading to
179 % degradations see eg in example_mixedModel_4
180 Cmax = M*R*(50)^3; % upper cost budget
181 maxorder = min(ceil((sum(N)-1)/2),16);
182
183 totCost = 0;
184 order = 0; % will be raised as far as possible
185 while order < maxorder
186 nextCost = R * nchoosek(M + 2*(order+1), M-1); % cost of order+1
187 if totCost + nextCost <= Cmax
188 order = order + 1;
189 totCost = totCost + nextCost;
190 else
191 break
192 end
193 end
194
195 [~,lG] = pfqn_cub(L,N,sum(Z,1),order,GlobalConstants.FineTol);
196 method = 'cub';
197 else
198 [~,lG] = pfqn_le(L,N,sum(Z,1));
199 method = 'le';
200 end
201 elseif sum(Z(:))==0 % single queue, no delay
202 lG = -N*log(L)';
203 method = 'exact';
204 else % repairman model
205 if N<10000
206 % gleint is a better method but there are JAR loading issues
207 % at times upon loading the txt files
208 %[~,lG] = pfqn_mmint2_gausslegendre(L,N,sum(Z,1));
209 %method = 'gleint';
210 [lG] = pfqn_comomrm(L,N,Z,1,options.tol);
211 method = 'comom';
212 else
213 [~,lG] = pfqn_le(L,N,sum(Z,1));
214 method = 'le';
215 end
216 end
217 case {'sampling'}
218 if M==1
219 [~,lG] = pfqn_mmsample2(L,N,sum(Z,1),options.samples);
220 method = 'sampling';
221 elseif M>R
222 [~,lG] = pfqn_mci(L,N,sum(Z,1),options.samples,'imci');
223 method = 'imci';
224 else
225 [~,lG] = pfqn_ls(L,N,sum(Z,1),options.samples);
226 method = 'ls';
227 end
228 case {'mmint2','gleint'}
229 if size(L,1)>1
230 if options.verbose
231 line_warning(mfilename,sprintf('The %s method requires a model with a delay and a single queueing station.',options.method));
232 end
233 lG = [];
234 return
235 end
236 [~,lG] = pfqn_mmint2_gausslegendre(L,N,sum(Z,1));
237 case {'cub','gm'} % Grundmann-Mueller cubatures
238 order = ceil((sum(N)-1)/2); % exact
239 [~,lG] = pfqn_cub(L,N,sum(Z,1),order,GlobalConstants.FineTol);
240 case 'kt'
241 [~,lG] = pfqn_kt(L,N,sum(Z,1));
242 case 'le'
243 [~,lG] = pfqn_le(L,N,sum(Z,1));
244 case 'ls'
245 [~,lG] = pfqn_ls(L,N,sum(Z,1),options.samples);
246 case {'mci','imci'}
247 [~,lG] = pfqn_mci(L,N,sum(Z,1),options.samples,options.method);
248 case {'mva'}
249 [~,~,~,~,lG] = pfqn_mva(L,N,sum(Z,1));
250 %case 'mom'
251 % line_warning(mfilename,'mom supported only in SolverJMT (method ).');
252 case {'exact'}
253 if M>=R || sum(N)>10 || sum(Z(:))>0
254 [~,lG] = pfqn_ca(L,N,sum(Z,1));
255 method = 'exact/ca';
256 else
257 [~,lG] = pfqn_recal(L,N,sum(Z,1));% implemented with Z=0
258 method = 'exact/recal';
259 end
260 case {'comom'}
261 if R>1
262 try
263 % comom has a bug in computing X, sometimes the
264 % order is switched
265 if M>1
266 if options.verbose
267 line_warning(mfilename,'comom supported only for M=1 queue. Use SolverJMT instead (method jmva.comom).');
268 end
269 lG = [];
270 return
271 else % use double precision script for M=1
272 [lG] = pfqn_comomrm(L,N,Z,1,options.tol);
273 end
274 catch ME
275 getReport(ME,'basic')
276 % java exception, probably singular linear system
277 %if options.verbose
278 %line_warning(mfilename,'Numerical problems.');
279 %end
280 lG = [];
281 end
282 else
283 [~,lG] = pfqn_ca(L,N,sum(Z,1));
284 method = 'ca';
285 end
286 case {'panacea'}
287 [~,lG] = pfqn_panacea(L,N,sum(Z,1));
288 if isnan(lG)
289 if options.verbose
290 line_warning(mfilename,'Model is not in normal usage, panacea cannot continue.\n');
291 lG = [];
292 return
293 end
294 end
295 case 'propfair'
296 [~,lG] = pfqn_propfair(L,N,sum(Z,1));
297 case {'recal'}
298 if sum(Z(:))>0
299 if options.verbose
300 line_warning(mfilename,'RECAL is currently available only for models with non-zero think times.\n');
301 lG = [];
302 return
303 end
304 end
305 [~,lG] = pfqn_recal(L,N,sum(Z,1));
306 % case 'rgf'
307 % if sum(Z(:))>0
308 % if option.verbose
309 % line_warning(mfilename,'RGF is defined only for models with non-zero think times.\n');
310 % lG = [];
311 % return
312 % end
313 % end
314 % [~,lG] = pfqn_rgf(L,N);
315 otherwise
316 lG=[];
317 X=[];
318 Q=[];
319 if options.verbose
320 line_warning(mfilename,sprintf('Unrecognized method: %s',options.method));
321 lG = [];
322 return
323 end
324 return
325end
326end