1function [QN,UN,RN,TN,CN,XN,totiter,method,runtime] = solver_mam(sn, options)
2%[Q,U,R,T,C,X,totiter] = SOLVER_MAM(QN, PH, OPTIONS)
4%Copyright (c) 2012-2026, Imperial College London
7method = options.method;
8config = options.config;
16V = cellsum(sn.visits);
27 inchain = sn.inchain{c};
28 lambdas_inchain = sn.rates(sn.refstat(inchain(1)),inchain);
29 lambdas_inchain = lambdas_inchain(isfinite(lambdas_inchain));
30 lambda(inchain) = sum(lambdas_inchain);
35 chain(k) = find(sn.chains(:,k));
40 case SchedStrategy.EXT
42 case {SchedStrategy.FCFS, SchedStrategy.HOL, SchedStrategy.FCFSPRPRIO, SchedStrategy.PS}
46 line_warning(mfilename,'The dec.mmap method does not support
this scheduling strategy.\n
');
48 [QN,UN,RN,TN,CN,XN] = deal([],[],[],[],[],[]);
51 runtime = toc(Tstart);
56if all(isinf(sn.njobs)) % is open
57 % open queueing system (one node is the external world)
62 case SchedStrategy.EXT
63 TN(ist,:) = sn.rates(ist,:);
64 TN(ist,isnan(TN(ist,:)))=0;
65 case {SchedStrategy.FCFS, SchedStrategy.HOL, SchedStrategy.FCFSPRPRIO, SchedStrategy.PS}
67 % divide service time by number of servers and put
68 % later a surrogate delay server in tandem to compensate
69 PH{ist}{k} = map_scale(PH{ist}{k}, map_mean(PH{ist}{k})/sn.nservers(ist));
70 pie{ist}{k} = map_pie(PH{ist}{k});
71 D0{ist,k} = PH{ist}{k}{1};
72 if any(isnan(D0{ist,k}))
73 D0{ist,k} = -GlobalConstants.Immediate;
75 PH{ist}{k} = map_exponential(GlobalConstants.Immediate);
81 % departure-process fixed point (parametric decomposition), driven on
82 % the station queue lengths by the generic DA driver
85 fpopts.config.da_miniter = 3; % legacy loop tested convergence only from the third sweep
86 fpopts.config.da_norm = @(xn,xr) max(abs(xn(:)-xr(:))./xr(:)); % relative difference
87 [~, totiter] = da_fpi(@mam_dec_sweep, QN, fpopts);
89 line_printf('\nMAM parametric decomposition completed in %d iterations.
',totiter);
93 line_warning(mfilename,'This model
is not supported by SolverMAM yet. Returning with no result.\n
');
98 function [xnew, xref] = mam_dec_sweep(~, itnum)
100 % now estimate arrival processes
102 % initially form departure processes using scaled service
103 % DEP, PH and V are all STATION-indexed, and solver_mam_traffic
104 % reads DEP{ist,r} likewise. Indexing them by node would only
105 % coincide when every station precedes every non-station node in
106 % node order (e.g. Source,Queue,Sink); with a Sink declared before a
107 % Queue, nodeToStation returns NaN and PH{NaN} errors out.
111 DEP{ist,r} = map_scale(PH{ist}{r}, 1 / (lambda(r) * V(ist,r)) );
116 ARV = solver_mam_traffic(sn, DEP, config);
120 ind = sn.stationToNode(ist);
121 finiteCapUsed = false;
122 switch sn.nodetype(ind)
124 if length(ARV{ind}{1}) > config.space_max
125 line_printf('\nArrival process at node %d
is now at %d states. Compressing.
',ind,length(ARV{ind}{1}));
126 ARV{ind} = mmap_compress(ARV{ind});
128 TN(ist,:) = mmap_lambda(ARV{ind});
130 case {SchedStrategy.FCFS, SchedStrategy.HOL, SchedStrategy.FCFSPRPRIO}
131 isFiniteCap = isfinite(sn.cap(ist));
134 [isMmck, muMmck] = mam_detect_mmck(sn, ist, K, ARV{ind});
136 aggrLambda_ist = sum(TN(ist,:), 'omitnan
');
137 exactRes = qsys_mmck(aggrLambda_ist, muMmck, sn.nservers(ist), capK);
138 meanQ_fc = exactRes.meanQueueLength;
139 lossProb_fc = exactRes.lossProbability;
141 [meanQ_fc, lossProb_fc, ~] = mam_truncate_renorm( ...
142 {ARV{ind}{[1,3:end]}}, {pie{ist}{:}}, {D0{ist,:}}, capK);
144 lambdaInflow = TN(ist,:); % per-class rates from mmap_lambda
145 lambdaInflow(isnan(lambdaInflow)) = 0;
146 TN_eff = lambdaInflow * (1 - lossProb_fc);
148 % Actual per-class service mean (PH was scaled by 1/c)
149 S_actual = zeros(1, K);
151 S_actual(k) = map_mean(PH{ist}{k}) * sn.nservers(ist);
154 Savg_eff = sum(TN_eff .* S_actual, 'omitnan
') / sumTN;
155 Wq = max(0, meanQ_fc / sumTN - Savg_eff);
160 TN(ist,k) = TN_eff(k);
161 UN(ist,k) = TN(ist,k) * map_mean(PH{ist}{k});
163 RN(ist,k) = Wq + S_actual(k);
164 QN(ist,k) = TN(ist,k) * RN(ist,k);
170 finiteCapUsed = true;
172 [Qret{1:K}, ~] = MMAPPH1FCFS({ARV{ind}{[1,3:end]}}, {pie{ist}{:}}, {D0{ist,:}}, 'ncMoms
', 1, 'ncDistr
',2);
174 QN(ist,k) = sum(Qret{k});
177 case SchedStrategy.PS
179 UN(ist,k) = TN(ist,k) * map_mean(PH{ist}{k});
181 Uden = min([1-GlobalConstants.FineTol, sum(UN(ist,:))]);
183 QN(ist,k) = UN(ist,k)/(1-Uden);
189 UN(ist,k) = TN(ist,k) * map_mean(PH{ist}{k});
190 %add number of jobs at the surrogate delay server
191 QN(ist,k) = QN(ist,k) + TN(ist,k)*(map_mean(PH{ist}{k})*sn.nservers(ist)) * (sn.nservers(ist)-1)/sn.nservers(ist);
192 RN(ist,k) = QN(ist,k) ./ TN(ist,k);
198 ind = sn.stationToNode(ist);
199 switch sn.nodetype(ind)
202 % extract class-r arrival MAP
203 A = mmap_hide(ARV{ind},setdiff(1:K,r));
207 etaqa_n = config.etaqa_trunc;
208 etaqa_sz = (etaqa_n+1)*na*ns;
209 rho = sum(UN(ist,:));
210 % use ETAQA if state space is manageable and queue is stable
211 if etaqa_sz <= config.space_max && rho < 1-GlobalConstants.FineTol
214 case {SchedStrategy.FCFS, SchedStrategy.HOL, SchedStrategy.FCFSPRPRIO}
215 DEP{ist,r} = qbd_depproc_etaqa(A, S, etaqa_n);
216 case SchedStrategy.PS
217 DEP{ist,r} = qbd_depproc_etaqa_ps(A, S, etaqa_n);
219 DEP{ist,r} = map_normalize(DEP{ist,r});
221 % fall back to scaled service on ETAQA failure
222 DEP{ist,r} = PH{ist}{r};
225 DEP{ist,r} = PH{ist}{r};
227 DEP{ist,r} = map_scale(DEP{ist,r}, 1 / (lambda(r) * V(ist,r)) );
228 SCVd(ist,r) = map_scv(DEP{ist,r});
229 IDCd(ist,r) = map_idc(DEP{ist,r});