1function [QN,UN,RN,TN,CN,XN,totiter] = solver_mam_basic_mmap_closed(sn, options)
2% [QN,UN,RN,TN,CN,XN,TOTITER] = SOLVER_MAM_BASIC_MMAP_CLOSED(SN, OPTIONS)
4% Closed-network wrapper around solver_mam_basic_mmap_inner. Drives a
5% per-
class bisection on the surrogate arrival rate LAMBDA so that the
6% inner solver
's queue lengths match the closed population SN.NJOBS.
7% Mirrors the outer-loop structure of solver_mna_closed.
9% Copyright (c) 2012-2026, Imperial College London
16% Per-class bisection bounds: upper = slowest non-INF station rate for that class
17nonInfStations = find(sn.nservers < Inf);
18lambda_lb = zeros(1,K);
19lambda_ub = zeros(1,K);
22 if ~isempty(nonInfStations)
23 rates_k = sn.rates(nonInfStations, k);
24 rates_k = rates_k(isfinite(rates_k) & rates_k > 0);
27 infStations = find(sn.nservers == Inf);
28 rates_inf = sn.rates(infStations, k);
29 rates_inf = rates_inf(isfinite(rates_inf) & rates_inf > 0);
33 lambda_ub(k) = max(rates_inf);
36 lambda_ub(k) = min(rates_k);
41QNc(~isfinite(QNc)) = 0; % open classes contribute 0; only closed populations gate convergence
47inner_options = options;
48inner_options.iter_max = max(20, ceil(options.iter_max/10));
49inner_options.verbose = false;
58% Last successful inner-kernel outputs (for fallback if final trial diverges)
59QN_last = QN; UN_last = UN; RN_last = RN;
60TN_last = TN; CN_last = CN; XN_last = XN;
63bisect_tol = max(options.iter_tol, 1e-3);
65while max(abs(QN_chain - QNc)) > bisect_tol && it_out < options.iter_max
69 if ~isfinite(QNc(k)) || QNc(k) == 0
72 if QN_chain(k) < QNc(k)
73 lambda_lb(k) = lambda(k);
75 lambda_ub(k) = lambda(k);
77 lambda(k) = 0.5 * (lambda_lb(k) + lambda_ub(k));
82 [QN, UN, RN, TN, CN, XN, ~] = solver_mam_basic_mmap_inner(sn, inner_options, lambda);
85 % Inner kernel diverged (typically MMAPPH1FCFS / lyap NaN under
86 % saturation). Treat all chains as overloaded so the bisection
87 % drops lambda on its next step.
92 % SLC clamp: all jobs at refstat for self-looping classes
96 QN(sn.refstat(k), k) = sn.njobs(k);
99 QN_chain = sum(QN, 1);
100 QN_chain(isnan(QN_chain) | isinf(QN_chain)) = 1/GlobalConstants.FineTol;
101 QN_last = QN; UN_last = UN; RN_last = RN;
102 TN_last = TN; CN_last = CN; XN_last = XN;
105 QN_chain = ones(1,K) * (1/GlobalConstants.FineTol);
109% If the last trial diverged, fall back to the most recent successful one
110if ~kernel_ok && have_good
111 QN = QN_last; UN = UN_last; RN = RN_last;
112 TN = TN_last; CN = CN_last; XN = XN_last;
115% Final SLC pass: pin throughput/utilisation at refstat (mirrors solver_mna_closed)
120 QN(ist, k) = sn.njobs(k);
121 TN(ist, k) = sn.njobs(k) * sn.rates(ist, k);
123 RN(ist, k) = QN(ist, k) / TN(ist, k);
127 UN(ist, k) = S(ist, k) * TN(ist, k);
131% Population redistribution within chain (matches solver_mna_closed:323-328)
133 inchain = sn.inchain{c};
134 if isfinite(sn.njobs(c))
135 sumQ = sum(sum(QN(:,inchain)));
137 QN(:,inchain) = sn.njobs(c) .* QN(:,inchain) / sumQ;
142% Delay/INF utilisation = mean number of jobs (matches solver_mna_closed)
143for ist=1:sn.nstations
144 if sn.sched(ist) == SchedStrategy.INF
145 UN(ist,:) = QN(ist,:);