LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
solver_mam_basic_mmap_inner.m
1function [QN,UN,RN,TN,CN,XN,totiter] = solver_mam_basic_mmap_inner(sn, options, lambda)
2% [QN,UN,RN,TN,CN,XN,TOTITER] = SOLVER_MAM_BASIC_MMAP_INNER(SN, OPTIONS, LAMBDA)
3%
4% MAM/MMAP fork-join decomposition algorithm parameterised by per-class arrival
5% rates LAMBDA. Performs the departure-process refinement loop only;
6% population enforcement (closed networks) is the wrapper's responsibility.
7%
8% Copyright (c) 2012-2026, Imperial College London
9% All rights reserved.
10
11config = options.config;
12if ~isfield(config, 'fj_sync_q_len')
13 config.fj_sync_q_len = 2;
14end
15if ~isfield(config, 'etaqa_trunc')
16 config.etaqa_trunc = 8;
17end
18
19PH = sn.proc;
20I = sn.nnodes;
21M = sn.nstations;
22K = sn.nclasses;
23V = cellsum(sn.visits);
24S = 1./sn.rates;
25
26QN = zeros(M,K);
27UN = zeros(M,K);
28RN = zeros(M,K);
29TN = zeros(M,K);
30CN = zeros(1,K);
31XN = zeros(1,K);
32
33% Build FJ synchronization map
34fjSyncMap = sn_build_fj_sync_map(sn);
35
36% Prepare PH service distributions
37pie = {};
38D0 = {};
39for ist=1:M
40 switch sn.sched(ist)
41 case SchedStrategy.EXT
42 TN(ist,:) = sn.rates(ist,:);
43 TN(ist,isnan(TN(ist,:))) = 0;
44 case {SchedStrategy.FCFS, SchedStrategy.HOL, SchedStrategy.FCFSPRPRIO}
45 for k=1:K
46 PH{ist}{k} = map_scale(PH{ist}{k}, map_mean(PH{ist}{k})/sn.nservers(ist));
47 pie{ist}{k} = map_pie(PH{ist}{k});
48 D0{ist,k} = PH{ist}{k}{1};
49 if any(isnan(D0{ist,k}))
50 D0{ist,k} = -GlobalConstants.Immediate;
51 pie{ist}{k} = 1;
52 PH{ist}{k} = map_exponential(GlobalConstants.Immediate);
53 end
54 end
55 case SchedStrategy.INF
56 for k=1:K
57 pie{ist}{k} = map_pie(PH{ist}{k});
58 D0{ist,k} = PH{ist}{k}{1};
59 if any(isnan(D0{ist,k}))
60 D0{ist,k} = -GlobalConstants.Immediate;
61 pie{ist}{k} = 1;
62 PH{ist}{k} = map_exponential(GlobalConstants.Immediate);
63 end
64 end
65 case SchedStrategy.PS
66 for k=1:K
67 PH{ist}{k} = map_scale(PH{ist}{k}, map_mean(PH{ist}{k})/sn.nservers(ist));
68 pie{ist}{k} = map_pie(PH{ist}{k});
69 D0{ist,k} = PH{ist}{k}{1};
70 if any(isnan(D0{ist,k}))
71 D0{ist,k} = -GlobalConstants.Immediate;
72 pie{ist}{k} = 1;
73 PH{ist}{k} = map_exponential(GlobalConstants.Immediate);
74 end
75 end
76 end
77end
78
79% departure-process fixed point (FJ parametric decomposition), driven on
80% the station queue lengths by the generic DA driver
81DEP = {};
82fpopts = options;
83fpopts.config.da_miniter = 3; % legacy loop tested convergence only from the third sweep
84fpopts.config.da_norm = @(xn,xr) max(abs(xn(:)-xr(:))./(xr(:)+GlobalConstants.FineTol)); % relative difference
85[~, totiter] = da_fpi(@mmap_dec_sweep, QN, fpopts);
86if options.verbose
87 line_printf('\nMAM FJ parametric decomposition completed in %d iterations.', totiter);
88end
89
90% Join: derive QN/RN from parallel branch means
91for joinIdx = find(sn.nodetype == NodeType.Join)'
92 joinStat = sn.nodeToStation(joinIdx);
93 if isnan(joinStat)
94 continue;
95 end
96 syncGroups = unique(fjSyncMap.nodeSync(joinIdx, :));
97 syncGroups = syncGroups(syncGroups > 0);
98 for r = 1:K
99 if TN(joinStat, r) <= 0
100 continue;
101 end
102 syncDelay = 0;
103 joinArrivalRate = 0;
104 for gid = syncGroups
105 branchNodes = find(fjSyncMap.nodeSync(joinIdx, :) == gid);
106 branchRt = zeros(1, numel(branchNodes));
107 branchTput = zeros(1, numel(branchNodes));
108 used = 0;
109 for b = 1:numel(branchNodes)
110 branchStat = sn.nodeToStation(branchNodes(b));
111 if isnan(branchStat) || RN(branchStat, r) <= 0
112 continue;
113 end
114 used = used + 1;
115 branchRt(used) = RN(branchStat, r);
116 branchTput(used) = TN(branchStat, r);
117 end
118 branchRt = branchRt(1:used);
119 branchTput = branchTput(1:used);
120 if numel(branchRt) < 2
121 continue;
122 end
123 lambdai = 1 ./ branchRt;
124 maxBranchRt = 0;
125 for pow = 0:(numel(branchRt) - 1)
126 maxBranchRt = maxBranchRt + (-1)^pow * sum(1 ./ sum(nchoosek(lambdai, pow + 1), 2));
127 end
128 syncDelay = syncDelay + max(maxBranchRt - mean(branchRt), 0);
129 joinArrivalRate = joinArrivalRate + sum(branchTput);
130 end
131 RN(joinStat, r) = syncDelay;
132 QN(joinStat, r) = joinArrivalRate * syncDelay;
133 UN(joinStat, r) = 0;
134 end
135end
136
137CN = sum(RN,1);
138QN(isnan(QN)) = 0;
139RN(isnan(RN)) = 0;
140UN(isnan(UN)) = 0;
141TN(isnan(TN)) = 0;
142
143 function [xnew, xref] = mmap_dec_sweep(~, itnum)
144 % Initialize departure processes (node-indexed: DEP{ind,r})
145 if itnum == 1
146 DEP = cell(I,K);
147 for ind=1:I
148 isForkJoin = (sn.nodetype(ind) == NodeType.Fork || sn.nodetype(ind) == NodeType.Join);
149 if sn.isstation(ind) && ~isForkJoin
150 ist = sn.nodeToStation(ind);
151 for r=1:K
152 if V(ist,r) > 0 && lambda(r) > 0
153 DEP{ind,r} = map_scale(PH{ist}{r}, 1 / (lambda(r) * V(ist,r)));
154 elseif sn.isslc(r)
155 % SLC vanishing-rate departure process; see _kb/06-solver-catalog.md for rationale
156 DEP{ind,r} = map_exponential(1/GlobalConstants.Zero);
157 else
158 DEP{ind,r} = PH{ist}{r};
159 end
160 end
161 else
162 for r=1:K
163 if lambda(r) > 0
164 DEP{ind,r} = map_exponential(1/lambda(r));
165 else
166 DEP{ind,r} = map_exponential(1/GlobalConstants.Immediate);
167 end
168 end
169 end
170 end
171 end
172
173 % Compute arrival processes with FJ synchronization
174 ARV = solver_mam_traffic_mmap(sn, DEP, config, fjSyncMap);
175
176 xref = QN;
177 for ist=1:M
178 ind = sn.stationToNode(ist);
179 switch sn.nodetype(ind)
180 case NodeType.Join
181 for k=1:K
182 TN(ist,k) = lambda(k);
183 UN(ist,k) = 0;
184 QN(ist,k) = 0;
185 RN(ist,k) = 0;
186 end
187 case NodeType.Queue
188 if ~isempty(ARV{ind}) && iscell(ARV{ind})
189 if length(ARV{ind}{1}) > config.space_max
190 if options.verbose
191 line_printf('\nArrival process at node %d is now at %d states. Compressing.', ind, length(ARV{ind}{1}));
192 end
193 ARV{ind} = mmap_compress(ARV{ind}, config);
194 end
195
196 finiteCapUsed = false;
197 switch sn.sched(ist)
198 case {SchedStrategy.FCFS, SchedStrategy.HOL, SchedStrategy.FCFSPRPRIO}
199 isFiniteCap = isfinite(sn.cap(ist));
200 if isFiniteCap
201 capK = sn.cap(ist);
202 [isMmck, muMmck] = mam_detect_mmck(sn, ist, K, ARV{ind});
203 if isMmck
204 aggrLambda_ist = sum(mmap_lambda(ARV{ind}), 'omitnan');
205 exactRes = qsys_mmck(aggrLambda_ist, muMmck, sn.nservers(ist), capK);
206 meanQ_fc = exactRes.meanQueueLength;
207 lossProb_fc = exactRes.lossProbability;
208 else
209 [meanQ_fc, lossProb_fc, ~] = mam_truncate_renorm( ...
210 {ARV{ind}{[1,3:end]}}, {pie{ist}{:}}, {D0{ist,:}}, capK);
211 end
212 lambdaInflow = mmap_lambda(ARV{ind});
213 lambdaInflow(isnan(lambdaInflow)) = 0;
214 TN_eff = lambdaInflow * (1 - lossProb_fc);
215 sumTN = sum(TN_eff);
216 S_actual = zeros(1, K);
217 for k=1:K
218 S_actual(k) = map_mean(PH{ist}{k}) * sn.nservers(ist);
219 end
220 if sumTN > 0
221 Savg_eff = sum(TN_eff .* S_actual, 'omitnan') / sumTN;
222 Wq = max(0, meanQ_fc / sumTN - Savg_eff);
223 else
224 Wq = 0;
225 end
226 for k=1:K
227 TN(ist,k) = TN_eff(k);
228 UN(ist,k) = TN(ist,k) * map_mean(PH{ist}{k});
229 if TN(ist,k) > 0
230 RN(ist,k) = Wq + S_actual(k);
231 QN(ist,k) = TN(ist,k) * RN(ist,k);
232 else
233 RN(ist,k) = 0;
234 QN(ist,k) = 0;
235 end
236 end
237 finiteCapUsed = true;
238 else
239 rho_ist_classes = mmap_lambda(ARV{ind}) .* arrayfun(@(k) map_mean(PH{ist}{k}), 1:K);
240 rho_ist_classes(isnan(rho_ist_classes)) = 0;
241 % Exclude SLC from FCFS saturation test; see _kb/06-solver-catalog.md for rationale
242 rho_ist = sum(rho_ist_classes(~sn.isslc));
243 if rho_ist < 1 - GlobalConstants.FineTol
244 % Exact MAP/MAP/1 for correlated single-class service;
245 % see _kb/06-solver-catalog.md for rationale
246 useMapMap1 = (K == 1) && (sn.nservers(ist) == 1) && ...
247 (abs(map_acf(PH{ist}{1}, 1)) > GlobalConstants.CoarseTol);
248 if useMapMap1
249 Carv0 = ARV{ind}{1};
250 Carv1 = ARV{ind}{3};
251 ql = Q_CT_MAP_MAP_1(Carv0, Carv1, PH{ist}{1}{1}, PH{ist}{1}{2}, 'MaxNumComp', 100000);
252 ql = ql(:);
253 QN(ist,1) = sum((0:numel(ql)-1)' .* ql);
254 else
255 [Qret{1:K}, ~] = MMAPPH1FCFS({ARV{ind}{[1,3:end]}}, {pie{ist}{:}}, {D0{ist,:}}, 'ncMoms', 1, 'ncDistr', 2);
256 for k=1:K
257 QN(ist,k) = sum(Qret{k});
258 end
259 end
260 else
261 % Bound queue lengths under overload (no NaN);
262 % see _kb/06-solver-catalog.md for rationale
263 for k=1:K
264 if isfinite(sn.njobs(k))
265 QN(ist,k) = sn.njobs(k);
266 else
267 QN(ist,k) = 1/GlobalConstants.FineTol;
268 end
269 end
270 end
271 TN(ist,:) = mmap_lambda(ARV{ind});
272 end
273 case SchedStrategy.PS
274 TN(ist,:) = mmap_lambda(ARV{ind});
275 for k=1:K
276 UN(ist,k) = TN(ist,k) * S(ist,k);
277 end
278 % Exclude SLC from PS sharing denominator; see _kb/06-solver-catalog.md for rationale
279 Uden = min([1-GlobalConstants.FineTol, sum(UN(ist,~sn.isslc))]);
280 for k=1:K
281 QN(ist,k) = UN(ist,k)/(1-Uden);
282 end
283 end
284
285 if ~finiteCapUsed
286 for k=1:K
287 UN(ist,k) = TN(ist,k) * map_mean(PH{ist}{k});
288 QN(ist,k) = QN(ist,k) + TN(ist,k)*(map_mean(PH{ist}{k})*sn.nservers(ist)) * (sn.nservers(ist)-1)/sn.nservers(ist);
289 RN(ist,k) = QN(ist,k) ./ TN(ist,k);
290 end
291 end
292 end
293 otherwise
294 switch sn.sched(ist)
295 case SchedStrategy.INF
296 if ~isempty(ARV{ind}) && iscell(ARV{ind})
297 TN(ist,:) = mmap_lambda(ARV{ind});
298 end
299 for k=1:K
300 if TN(ist,k) > 0
301 UN(ist,k) = S(ist,k)*TN(ist,k);
302 QN(ist,k) = TN(ist,k)*S(ist,k);
303 RN(ist,k) = S(ist,k);
304 end
305 end
306 case SchedStrategy.EXT
307 % Source: TN already set above
308 end
309 end
310 end
311
312 % Update departure processes
313 for ist=1:M
314 ind = sn.stationToNode(ist);
315 switch sn.nodetype(ind)
316 case NodeType.Queue
317 if ~isempty(ARV{ind}) && iscell(ARV{ind})
318 switch sn.sched(ist)
319 case {SchedStrategy.FCFS, SchedStrategy.HOL, SchedStrategy.FCFSPRPRIO}
320 for r=1:K
321 A = mmap_hide(ARV{ind}, setdiff(1:K,r));
322 Srv = PH{ist}{r};
323 na = length(A{1});
324 ns = length(Srv{1});
325 etaqa_n = config.etaqa_trunc;
326 etaqa_sz = (etaqa_n+1)*na*ns;
327 rho = sum(UN(ist,:));
328 if etaqa_sz <= config.space_max && rho < 1-GlobalConstants.FineTol
329 try
330 DEP{ind,r} = qbd_depproc_etaqa(A, Srv, etaqa_n);
331 DEP{ind,r} = map_normalize(DEP{ind,r});
332 catch
333 DEP{ind,r} = Srv;
334 end
335 else
336 DEP{ind,r} = Srv;
337 end
338 if V(ist,r) > 0 && lambda(r) > 0
339 DEP{ind,r} = map_scale(DEP{ind,r}, 1 / (lambda(r) * V(ist,r)));
340 end
341 end
342 case SchedStrategy.PS
343 for r=1:K
344 A = mmap_hide(ARV{ind}, setdiff(1:K,r));
345 Srv = PH{ist}{r};
346 na = length(A{1});
347 ns = length(Srv{1});
348 etaqa_n = config.etaqa_trunc;
349 etaqa_sz = (etaqa_n+1)*na*ns;
350 rho = sum(UN(ist,:));
351 if V(ist,r) > 0 && lambda(r) > 0
352 if etaqa_sz <= config.space_max && rho < 1-GlobalConstants.FineTol
353 try
354 DEP{ind,r} = qbd_depproc_etaqa_ps(A, Srv, etaqa_n);
355 DEP{ind,r} = map_normalize(DEP{ind,r});
356 catch
357 DEP{ind,r} = Srv;
358 end
359 else
360 DEP{ind,r} = Srv;
361 end
362 DEP{ind,r} = map_scale(DEP{ind,r}, 1 / (lambda(r) * V(ist,r)));
363 end
364 end
365 end
366 end
367 case NodeType.Join
368 for r=1:K
369 if TN(ist,r) > 0
370 DEP{ind,r} = map_exponential(1/TN(ist,r));
371 end
372 end
373 end
374 end
375 xnew = QN;
376 end
377end