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
46% Self-looping classes are pinned by the SLC clamp below; they must not
47% contribute a (saturating) surrogate arrival stream to the inner algorithm.
50inner_options = options;
51inner_options.iter_max = max(20, ceil(options.iter_max/10));
52inner_options.verbose = false;
53% Cap the MMAP phase-dimension truncation at the same value used by the
54% solver_mna_closed wrapper this routine mirrors. The analyzer default
55% (space_max=128) lets the FJ synchronization/superposition inflate the
56% arrival MMAP to ~128 phases, whose ETAQA/QBD solve is O(dim^3), while the
57% bisection re-enters the inner solve ~29 times: ~154s on a 3-class model
58% that CTMC solves in 0.7s. Measured, the compressed result is bit-identical
59% from space_max=4 up to 128 (the surrogate arrival stream carries only a
60% few effective phases), so the inflated dimension is pure wasted work.
61% Capping at 16 is lossless here and matches solver_mna_closed.
62if ~isfield(inner_options.config, 'space_max
') || inner_options.config.space_max > 16
63 inner_options.config.space_max = 16;
73% Last successful inner-algorithm outputs (for fallback if final trial diverges)
74QN_last = QN; UN_last = UN; RN_last = RN;
75TN_last = TN; CN_last = CN; XN_last = XN;
78bisect_tol = max(options.iter_tol, 1e-3);
80while max(abs(QN_chain - QNc)) > bisect_tol && it_out < options.iter_max
83 bracket_collapsed = true;
85 if ~isfinite(QNc(k)) || QNc(k) == 0 || sn.isslc(k)
88 if QN_chain(k) < QNc(k)
89 lambda_lb(k) = lambda(k);
91 lambda_ub(k) = lambda(k);
93 lambda(k) = 0.5 * (lambda_lb(k) + lambda_ub(k));
94 % Bisection can still refine class k only while its bracket is
95 % wider than the precision floor below which LAMBDA cannot move
96 % any reported metric.
97 if (lambda_ub(k) - lambda_lb(k)) > GlobalConstants.FineTol * max(1, abs(lambda_ub(k)))
98 bracket_collapsed = false;
101 % Bracket-width stagnation break. The loop condition above tests only
102 % the residual population gap, which never closes when no surrogate
103 % LAMBDA reproduces the closed population (the per-class targets are
104 % not simultaneously attainable). The bisection then keeps halving
105 % brackets that have already narrowed past FineTol, re-entering the
106 % inner algorithm for changes it cannot resolve. Stop once every
107 % active bracket has collapsed and keep the outputs in hand.
115 [QN, UN, RN, TN, CN, XN, ~] = solver_mam_basic_mmap_inner(sn, inner_options, lambda);
118 % Inner algorithm diverged (typically MMAPPH1FCFS / lyap NaN under
119 % saturation). Treat all chains as overloaded so the bisection
120 % drops lambda on its next step.
121 algorithm_ok = false;
125 % SLC clamp: all jobs at refstat for self-looping classes
129 QN(sn.refstat(k), k) = sn.njobs(k);
132 QN_chain = sum(QN, 1);
133 QN_chain(isnan(QN_chain) | isinf(QN_chain)) = 1/GlobalConstants.FineTol;
134 QN_last = QN; UN_last = UN; RN_last = RN;
135 TN_last = TN; CN_last = CN; XN_last = XN;
138 QN_chain = ones(1,K) * (1/GlobalConstants.FineTol);
142% If the last trial diverged, fall back to the most recent successful one
143if ~algorithm_ok && have_good
144 QN = QN_last; UN = UN_last; RN = RN_last;
145 TN = TN_last; CN = CN_last; XN = XN_last;
148% Final SLC pass: pin throughput/utilisation at refstat (mirrors solver_mna_closed)
153 QN(ist, k) = sn.njobs(k);
154 TN(ist, k) = sn.njobs(k) * sn.rates(ist, k);
156 RN(ist, k) = QN(ist, k) / TN(ist, k);
160 UN(ist, k) = S(ist, k) * TN(ist, k);
164% Population redistribution within chain (matches solver_mna_closed:323-328)
166 inchain = sn.inchain{c};
167 if isfinite(sn.njobs(c))
168 sumQ = sum(sum(QN(:,inchain)));
170 QN(:,inchain) = sn.njobs(c) .* QN(:,inchain) / sumQ;
175% Delay/INF utilisation = mean number of jobs (matches solver_mna_closed)
176for ist=1:sn.nstations
177 if sn.sched(ist) == SchedStrategy.INF
178 UN(ist,:) = QN(ist,:);