LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
solver_mam_basic_mmap_closed.m
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)
3%
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.
8%
9% Copyright (c) 2012-2026, Imperial College London
10% All rights reserved.
11
12K = sn.nclasses;
13M = sn.nstations;
14S = 1./sn.rates;
15
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);
20for k=1:K
21 rates_k = [];
22 if ~isempty(nonInfStations)
23 rates_k = sn.rates(nonInfStations, k);
24 rates_k = rates_k(isfinite(rates_k) & rates_k > 0);
25 end
26 if isempty(rates_k)
27 infStations = find(sn.nservers == Inf);
28 rates_inf = sn.rates(infStations, k);
29 rates_inf = rates_inf(isfinite(rates_inf) & rates_inf > 0);
30 if isempty(rates_inf)
31 lambda_ub(k) = 1;
32 else
33 lambda_ub(k) = max(rates_inf);
34 end
35 else
36 lambda_ub(k) = min(rates_k);
37 end
38end
39
40QNc = sn.njobs;
41QNc(~isfinite(QNc)) = 0; % open classes contribute 0; only closed populations gate convergence
42QN_chain = zeros(1,K);
43
44it_out = 0;
45lambda = lambda_ub;
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.
48lambda(sn.isslc) = 0;
49
50inner_options = options;
51inner_options.iter_max = max(20, ceil(options.iter_max/10));
52inner_options.verbose = false;
53% Cap MMAP phase truncation at 16 (lossless, avoids O(dim^3) waste);
54% see _kb/06-solver-catalog.md for rationale
55if ~isfield(inner_options.config, 'space_max') || inner_options.config.space_max > 16
56 inner_options.config.space_max = 16;
57end
58
59QN = zeros(M,K);
60UN = zeros(M,K);
61RN = zeros(M,K);
62TN = zeros(M,K);
63CN = zeros(1,K);
64XN = zeros(1,K);
65
66% Last successful inner-algorithm outputs (for fallback if final trial diverges)
67QN_last = QN; UN_last = UN; RN_last = RN;
68TN_last = TN; CN_last = CN; XN_last = XN;
69have_good = false;
70
71bisect_tol = max(options.iter_tol, 1e-3);
72
73while max(abs(QN_chain - QNc)) > bisect_tol && it_out < options.iter_max
74 it_out = it_out + 1;
75 if it_out > 1
76 bracket_collapsed = true;
77 for k=1:K
78 if ~isfinite(QNc(k)) || QNc(k) == 0 || sn.isslc(k)
79 continue;
80 end
81 if QN_chain(k) < QNc(k)
82 lambda_lb(k) = lambda(k);
83 else
84 lambda_ub(k) = lambda(k);
85 end
86 lambda(k) = 0.5 * (lambda_lb(k) + lambda_ub(k));
87 % Bisection can still refine class k only while its bracket is
88 % wider than the precision floor below which LAMBDA cannot move
89 % any reported metric.
90 if (lambda_ub(k) - lambda_lb(k)) > GlobalConstants.FineTol * max(1, abs(lambda_ub(k)))
91 bracket_collapsed = false;
92 end
93 end
94 % Bracket-width stagnation break; see _kb/06-solver-catalog.md for rationale
95 if bracket_collapsed
96 it_out = it_out - 1;
97 break;
98 end
99 end
100
101 try
102 [QN, UN, RN, TN, CN, XN, ~] = solver_mam_basic_mmap_inner(sn, inner_options, lambda);
103 algorithm_ok = true;
104 catch
105 % Inner algorithm diverged (typically MMAPPH1FCFS / lyap NaN under
106 % saturation). Treat all chains as overloaded so the bisection
107 % drops lambda on its next step.
108 algorithm_ok = false;
109 end
110
111 if algorithm_ok
112 % SLC clamp: all jobs at refstat for self-looping classes
113 for k=1:K
114 if sn.isslc(k)
115 QN(:,k) = 0;
116 QN(sn.refstat(k), k) = sn.njobs(k);
117 end
118 end
119 QN_chain = sum(QN, 1);
120 QN_chain(isnan(QN_chain) | isinf(QN_chain)) = 1/GlobalConstants.FineTol;
121 QN_last = QN; UN_last = UN; RN_last = RN;
122 TN_last = TN; CN_last = CN; XN_last = XN;
123 have_good = true;
124 else
125 QN_chain = ones(1,K) * (1/GlobalConstants.FineTol);
126 end
127end
128
129% If the last trial diverged, fall back to the most recent successful one
130if ~algorithm_ok && have_good
131 QN = QN_last; UN = UN_last; RN = RN_last;
132 TN = TN_last; CN = CN_last; XN = XN_last;
133end
134
135% Final SLC pass: pin throughput/utilisation at refstat (mirrors solver_mna_closed)
136for k=1:K
137 if sn.isslc(k)
138 QN(:,k) = 0;
139 ist = sn.refstat(k);
140 QN(ist, k) = sn.njobs(k);
141 TN(ist, k) = sn.njobs(k) * sn.rates(ist, k);
142 if TN(ist, k) > 0
143 RN(ist, k) = QN(ist, k) / TN(ist, k);
144 else
145 RN(ist, k) = 0;
146 end
147 UN(ist, k) = S(ist, k) * TN(ist, k);
148 end
149end
150
151% Population redistribution within chain (matches solver_mna_closed:323-328)
152for c=1:sn.nchains
153 inchain = sn.inchain{c};
154 if isfinite(sn.njobs(c))
155 sumQ = sum(sum(QN(:,inchain)));
156 if sumQ > 0
157 QN(:,inchain) = sn.njobs(c) .* QN(:,inchain) / sumQ;
158 end
159 end
160end
161
162% Delay/INF utilisation = mean number of jobs (matches solver_mna_closed)
163for ist=1:sn.nstations
164 if sn.sched(ist) == SchedStrategy.INF
165 UN(ist,:) = QN(ist,:);
166 end
167end
168
169CN = sum(RN, 1);
170QN(isnan(QN)) = 0;
171UN(isnan(UN)) = 0;
172RN(isnan(RN)) = 0;
173TN(isnan(TN)) = 0;
174CN(isnan(CN)) = 0;
175totiter = it_out;
176end