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 kernel 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
79it_max = options.iter_max;
80for it=1:it_max
81 % Initialize departure processes (node-indexed: DEP{ind,r})
82 if it == 1
83 DEP = cell(I,K);
84 for ind=1:I
85 isForkJoin = (sn.nodetype(ind) == NodeType.Fork || sn.nodetype(ind) == NodeType.Join);
86 if sn.isstation(ind) && ~isForkJoin
87 ist = sn.nodeToStation(ind);
88 for r=1:K
89 if V(ist,r) > 0 && lambda(r) > 0
90 DEP{ind,r} = map_scale(PH{ist}{r}, 1 / (lambda(r) * V(ist,r)));
91 else
92 DEP{ind,r} = PH{ist}{r};
93 end
94 end
95 else
96 for r=1:K
97 if lambda(r) > 0
98 DEP{ind,r} = map_exponential(1/lambda(r));
99 else
100 DEP{ind,r} = map_exponential(1/GlobalConstants.Immediate);
101 end
102 end
103 end
104 end
105 end
106
107 % Compute arrival processes with FJ synchronization
108 ARV = solver_mam_traffic_mmap(sn, DEP, config, fjSyncMap);
109
110 QN_1 = QN;
111 for ist=1:M
112 ind = sn.stationToNode(ist);
113 switch sn.nodetype(ind)
114 case NodeType.Join
115 for k=1:K
116 TN(ist,k) = lambda(k);
117 UN(ist,k) = 0;
118 QN(ist,k) = 0;
119 RN(ist,k) = 0;
120 end
121 case NodeType.Queue
122 if ~isempty(ARV{ind}) && iscell(ARV{ind})
123 if length(ARV{ind}{1}) > config.space_max
124 if options.verbose
125 line_printf('\nArrival process at node %d is now at %d states. Compressing.', ind, length(ARV{ind}{1}));
126 end
127 ARV{ind} = mmap_compress(ARV{ind}, config);
128 end
129
130 finiteCapUsed = false;
131 switch sn.sched(ist)
132 case {SchedStrategy.FCFS, SchedStrategy.HOL, SchedStrategy.FCFSPRPRIO}
133 isFiniteCap = isfinite(sn.cap(ist));
134 if isFiniteCap
135 capK = sn.cap(ist);
136 [isMmck, muMmck] = mam_detect_mmck(sn, ist, K, ARV{ind});
137 if isMmck
138 aggrLambda_ist = sum(mmap_lambda(ARV{ind}), 'omitnan');
139 exactRes = qsys_mmck(aggrLambda_ist, muMmck, sn.nservers(ist), capK);
140 meanQ_fc = exactRes.meanQueueLength;
141 lossProb_fc = exactRes.lossProbability;
142 else
143 [meanQ_fc, lossProb_fc, ~] = mam_truncate_renorm( ...
144 {ARV{ind}{[1,3:end]}}, {pie{ist}{:}}, {D0{ist,:}}, capK);
145 end
146 lambdaInflow = mmap_lambda(ARV{ind});
147 lambdaInflow(isnan(lambdaInflow)) = 0;
148 TN_eff = lambdaInflow * (1 - lossProb_fc);
149 sumTN = sum(TN_eff);
150 S_actual = zeros(1, K);
151 for k=1:K
152 S_actual(k) = map_mean(PH{ist}{k}) * sn.nservers(ist);
153 end
154 if sumTN > 0
155 Savg_eff = sum(TN_eff .* S_actual, 'omitnan') / sumTN;
156 Wq = max(0, meanQ_fc / sumTN - Savg_eff);
157 else
158 Wq = 0;
159 end
160 for k=1:K
161 TN(ist,k) = TN_eff(k);
162 UN(ist,k) = TN(ist,k) * map_mean(PH{ist}{k});
163 if TN(ist,k) > 0
164 RN(ist,k) = Wq + S_actual(k);
165 QN(ist,k) = TN(ist,k) * RN(ist,k);
166 else
167 RN(ist,k) = 0;
168 QN(ist,k) = 0;
169 end
170 end
171 finiteCapUsed = true;
172 else
173 rho_ist_classes = mmap_lambda(ARV{ind}) .* arrayfun(@(k) map_mean(PH{ist}{k}), 1:K);
174 rho_ist_classes(isnan(rho_ist_classes)) = 0;
175 rho_ist = sum(rho_ist_classes);
176 if rho_ist < 1 - GlobalConstants.FineTol
177 [Qret{1:K}, ~] = MMAPPH1FCFS({ARV{ind}{[1,3:end]}}, {pie{ist}{:}}, {D0{ist,:}}, 'ncMoms', 1, 'ncDistr', 2);
178 for k=1:K
179 QN(ist,k) = sum(Qret{k});
180 end
181 else
182 % Saturation: bound queue lengths so the
183 % wrapper's bisection can recognise overload
184 % without NaNs propagating from MMAPPH1FCFS.
185 for k=1:K
186 if isfinite(sn.njobs(k))
187 QN(ist,k) = sn.njobs(k);
188 else
189 QN(ist,k) = 1/GlobalConstants.FineTol;
190 end
191 end
192 end
193 TN(ist,:) = mmap_lambda(ARV{ind});
194 end
195 case SchedStrategy.PS
196 TN(ist,:) = mmap_lambda(ARV{ind});
197 for k=1:K
198 UN(ist,k) = TN(ist,k) * S(ist,k);
199 end
200 Uden = min([1-GlobalConstants.FineTol, sum(UN(ist,:))]);
201 for k=1:K
202 QN(ist,k) = UN(ist,k)/(1-Uden);
203 end
204 end
205
206 if ~finiteCapUsed
207 for k=1:K
208 UN(ist,k) = TN(ist,k) * map_mean(PH{ist}{k});
209 QN(ist,k) = QN(ist,k) + TN(ist,k)*(map_mean(PH{ist}{k})*sn.nservers(ist)) * (sn.nservers(ist)-1)/sn.nservers(ist);
210 RN(ist,k) = QN(ist,k) ./ TN(ist,k);
211 end
212 end
213 end
214 otherwise
215 switch sn.sched(ist)
216 case SchedStrategy.INF
217 if ~isempty(ARV{ind}) && iscell(ARV{ind})
218 TN(ist,:) = mmap_lambda(ARV{ind});
219 end
220 for k=1:K
221 if TN(ist,k) > 0
222 UN(ist,k) = S(ist,k)*TN(ist,k);
223 QN(ist,k) = TN(ist,k)*S(ist,k);
224 RN(ist,k) = S(ist,k);
225 end
226 end
227 case SchedStrategy.EXT
228 % Source: TN already set above
229 end
230 end
231 end
232
233 if it >= 3 && max(abs(QN(:)-QN_1(:))./(QN_1(:)+GlobalConstants.FineTol)) < options.iter_tol
234 break;
235 end
236
237 % Update departure processes
238 for ist=1:M
239 ind = sn.stationToNode(ist);
240 switch sn.nodetype(ind)
241 case NodeType.Queue
242 if ~isempty(ARV{ind}) && iscell(ARV{ind})
243 switch sn.sched(ist)
244 case {SchedStrategy.FCFS, SchedStrategy.HOL, SchedStrategy.FCFSPRPRIO}
245 for r=1:K
246 A = mmap_hide(ARV{ind}, setdiff(1:K,r));
247 Srv = PH{ist}{r};
248 na = length(A{1});
249 ns = length(Srv{1});
250 etaqa_n = config.etaqa_trunc;
251 etaqa_sz = (etaqa_n+1)*na*ns;
252 rho = sum(UN(ist,:));
253 if etaqa_sz <= config.space_max && rho < 1-GlobalConstants.FineTol
254 try
255 DEP{ind,r} = qbd_depproc_etaqa(A, Srv, etaqa_n);
256 DEP{ind,r} = map_normalize(DEP{ind,r});
257 catch
258 DEP{ind,r} = Srv;
259 end
260 else
261 DEP{ind,r} = Srv;
262 end
263 if V(ist,r) > 0 && lambda(r) > 0
264 DEP{ind,r} = map_scale(DEP{ind,r}, 1 / (lambda(r) * V(ist,r)));
265 end
266 end
267 case SchedStrategy.PS
268 for r=1:K
269 A = mmap_hide(ARV{ind}, setdiff(1:K,r));
270 Srv = PH{ist}{r};
271 na = length(A{1});
272 ns = length(Srv{1});
273 etaqa_n = config.etaqa_trunc;
274 etaqa_sz = (etaqa_n+1)*na*ns;
275 rho = sum(UN(ist,:));
276 if V(ist,r) > 0 && lambda(r) > 0
277 if etaqa_sz <= config.space_max && rho < 1-GlobalConstants.FineTol
278 try
279 DEP{ind,r} = qbd_depproc_etaqa_ps(A, Srv, etaqa_n);
280 DEP{ind,r} = map_normalize(DEP{ind,r});
281 catch
282 DEP{ind,r} = Srv;
283 end
284 else
285 DEP{ind,r} = Srv;
286 end
287 DEP{ind,r} = map_scale(DEP{ind,r}, 1 / (lambda(r) * V(ist,r)));
288 end
289 end
290 end
291 end
292 case NodeType.Join
293 for r=1:K
294 if TN(ist,r) > 0
295 DEP{ind,r} = map_exponential(1/TN(ist,r));
296 end
297 end
298 end
299 end
300end
301
302totiter = it;
303if options.verbose
304 line_printf('\nMAM FJ parametric decomposition completed in %d iterations.', it);
305end
306
307% Join: derive QN/RN from parallel branch means
308for joinIdx = find(sn.nodetype == NodeType.Join)'
309 joinStat = sn.nodeToStation(joinIdx);
310 if isnan(joinStat)
311 continue;
312 end
313 syncGroups = unique(fjSyncMap.nodeSync(joinIdx, :));
314 syncGroups = syncGroups(syncGroups > 0);
315 for r = 1:K
316 if TN(joinStat, r) <= 0
317 continue;
318 end
319 syncDelay = 0;
320 joinArrivalRate = 0;
321 for gid = syncGroups
322 branchNodes = find(fjSyncMap.nodeSync(joinIdx, :) == gid);
323 branchRt = zeros(1, numel(branchNodes));
324 branchTput = zeros(1, numel(branchNodes));
325 used = 0;
326 for b = 1:numel(branchNodes)
327 branchStat = sn.nodeToStation(branchNodes(b));
328 if isnan(branchStat) || RN(branchStat, r) <= 0
329 continue;
330 end
331 used = used + 1;
332 branchRt(used) = RN(branchStat, r);
333 branchTput(used) = TN(branchStat, r);
334 end
335 branchRt = branchRt(1:used);
336 branchTput = branchTput(1:used);
337 if numel(branchRt) < 2
338 continue;
339 end
340 lambdai = 1 ./ branchRt;
341 maxBranchRt = 0;
342 for pow = 0:(numel(branchRt) - 1)
343 maxBranchRt = maxBranchRt + (-1)^pow * sum(1 ./ sum(nchoosek(lambdai, pow + 1), 2));
344 end
345 syncDelay = syncDelay + max(maxBranchRt - mean(branchRt), 0);
346 joinArrivalRate = joinArrivalRate + sum(branchTput);
347 end
348 RN(joinStat, r) = syncDelay;
349 QN(joinStat, r) = joinArrivalRate * syncDelay;
350 UN(joinStat, r) = 0;
351 end
352end
353
354CN = sum(RN,1);
355QN(isnan(QN)) = 0;
356RN(isnan(RN)) = 0;
357UN(isnan(UN)) = 0;
358TN(isnan(TN)) = 0;
359end