1function [QN,UN,RN,TN,CN,XN,runtime] = solver_ba_qrf_analyzer(sn, options)
2% SOLVER_BA_QRF_ANALYZER Adapter
for QRF library functions within SolverBA
4% [QN,UN,RN,TN,CN,XN,RUNTIME] = SOLVER_BA_QRF_ANALYZER(SN, OPTIONS)
6% Bridges
the LINE sn
struct to QRF (Quadratic Reduction Framework) library
7% functions
for approximating performance metrics of single-
class closed
8% queueing networks with PH service.
10% Copyright (c) 2012-2026, Imperial College London
21% QRF only supports single-
class closed networks
23 line_error(mfilename,
'QRF methods only support single-class networks (found %d classes).', K);
25if any(isinf(sn.njobs))
26 line_error(mfilename, 'QRF methods only support closed networks.');
29% Extract MAPs as cell array: MAPs{i} = {D0, D1}
31K_phases = zeros(M, 1);
35 K_phases(i) = size(MAPs{i}{1}, 1);
38 MAPs{i} = {-1, 1}; % fallback exponential rate 1
42% Build routing matrix (M x M) from sn.rt (MK x MK)
46 rt(i,j) = sn.rt(i, j); % K=1, so indexing
is direct
50% Extract mu and v arrays from MAPs
52mu = zeros(M, Kmax, Kmax);
53v = zeros(M, Kmax, Kmax);
59 % Both mu and v are indexed (from phase, to phase), matching
60 % qrf_noblo_*
's q{i,j}(k,h) = v{i}(k,h) + r(i,i)*mu{i}(k,h).
61 % mu{i}(k,h) is the completion rate from phase k leaving to phase
62 % h, i.e. D1(k,h); v{i}(k,h) is the background phase change k -> h,
63 % i.e. D0(k,h) off the diagonal. Writing v as D0(h,k) transposes
64 % it, which is invisible for a reversible D0 but reverses the phase
65 % order of an Erlang (upper bidiagonal D0) and silently changes the
67 mu(i, h, k) = D1(h, k);
71 v(i, h, k) = D0(h, k);
77% Dispatch based on method
81 [UN_qrf, QN_qrf] = qrf_noblo_mmi(M, MR, K_phases(:)', N, mu, v, rt);
84 [UN_qrf, QN_qrf] = qrf_noblo_mem(MAPs, N, rt);
87 if isfield(options.config,
'qrf_alpha') && ~isempty(options.config.qrf_alpha)
88 alpha = options.config.qrf_alpha;
92 [UN_qrf, QN_qrf] = qrf_noblo_mmi_ld(MAPs, N, rt, alpha);
95 if isfield(options.config, 'qrf_alpha') && ~isempty(options.config.qrf_alpha)
96 alpha = options.config.qrf_alpha;
100 [UN_qrf, QN_qrf] = qrf_noblo_mmi_linear(MAPs, N, rt, alpha);
103 qp = options.config.qrf_params;
105 line_error(mfilename, 'qrf.bas.mmi requires options.config.qrf_params with fields: f, MR, BB, F.');
111 [UN_qrf, QN_qrf] = qrf_bas_mmi_simple(f, M, MR, BB, K_phases(:)', F, N, mu, v, rt);
114 qp = options.config.qrf_params;
116 line_error(mfilename, 'qrf.bas.mem requires options.config.qrf_params with fields: f, MR, MM, MM1, ZZ, ZM, BB, F.');
126 [UN_qrf, QN_qrf] = qrf_bas_mem(f, M, MR, MM, MM1, ZZ, ZM, BB, K_phases(:)', F, N, mu, v, rt);
129 params = sn_to_qrf_params(sn, MAPs, K_phases, N, mu, v, rt, options);
130 params.verbose = options.verbose > 0;
131 [result] = qrf_bas(params);
132 % qrf_bas returns utilization bounds; derive QN via visit ratios
133 [UN_qrf, QN_qrf] = derive_qn_from_bounds(result.U(:)', M, N, S, PH, sn);
136 params = sn_to_qrf_params(sn, MAPs, K_phases, N, mu, v, rt, options);
137 params.verbose = options.verbose > 0;
138 [result] = qrf_rsrd(params);
139 [UN_qrf, QN_qrf] = derive_qn_from_bounds(result.U(:)', M, N, S, PH, sn);
142 line_error(mfilename, 'Unknown QRF method: %s', options.method);
145% Normalize QN to population constraint
147 QN_qrf = QN_qrf / sum(QN_qrf) * N;
150% Map 1D QRF results to M x K matrices (K=1)
160% Derive all metrics from
the QRF utilization using Little's law and visit
163% UN_qrf(i)
is sum_{ni>=1, ki} p2(i,ni,ki,i,ni,ki), i.e.
P(n_i >= 1)
164% marginalised over phase. That IS
the utilization and it
is bounded by 1
165% through
the ONE constraint. An earlier comment here held that it
"is the sum
166% of phase effective utilizations, which can exceed 1 for phase-type service"; that
167%
is false, and what exceeds 1
is the sum ACROSS stations, which
is expected.
168% Adjudicated against glpsol on
the paper
's AMPL model at K=2 (2-station
169% closed, Exp + Erlang-2): every UN_qrf entry lands inside the glpsol
170% [min,max] range for that station's utilization.
172% Throughput therefore comes from a finite-server station via
173% U_i = X * V_i * stime_i, which
is exact
for a single server. The previous
174% inversion X = QN(refstat) / (V*stime_ref) instead assumed R == stime at
the
175% reference station, which holds only
for an infinite server; with a FCFS
176% reference station it returned X above
the bottleneck capacity and hence
177% UN > 1. UN_qrf(i)/(V_i*stime_i)
is consistent across stations, so any
178% finite-server station determines X.
180% Compute visit ratios
181if isfield(sn,
'visits') && ~isempty(sn.visits)
184 [
visits] = sn_refresh_visits(sn, sn.chains, sn.rt, sn.rtnodes);
188refstat = sn.refstat(1);
190% Per-station mean service times
193 if ~isempty(PH{i}{1})
194 stimes(i) = map_mean(PH{i}{1});
198% Find system throughput XN from
the QRF utilization at a finite-server
199% station: U_i = XN * V(i,1) * stime_i / S(i), exact
for a single server.
202 if ~isinf(S(i)) && stimes(i) > 0 && V(i, 1) > 0 && UN_qrf(i) > 0
203 XN(1) = UN_qrf(i) * S(i) / (V(i, 1) * stimes(i));
208 % No finite-server station carries load: fall back to the reference
209 % station, where QN = XN * V * stime holds exactly for a delay.
210 if stimes(refstat) > 0 && V(refstat, 1) > 0
211 XN(1) = QN(refstat, 1) / (V(refstat, 1) * stimes(refstat));
215% Derive per-station metrics from XN and visit ratios
217 if ~isempty(PH{i}{1})
219 TN(i, 1) = XN(1) * V(i, 1);
221 % Delay (infinite server): UN = QN by LINE convention
224 % Finite server: the QRF utilization directly
226 UN(i, 1) = UN_qrf(i);
229 % Response time via Little's law
231 RN(i, 1) = QN(i, 1) / TN(i, 1);
249runtime = toc(Tstart);
252function params = sn_to_qrf_params(sn, MAPs, K_phases, N, mu, v, rt, options)
253% Build params
struct for qrf_bas / qrf_rsrd from sn struct
258params.K = K_phases(:);
261% Convert mu/v from 3D arrays to cell arrays expected by qrf_bas/qrf_rsrd
262% Each mu{i}, v{i} must be a [Ki x Ki] matrix (even
for Ki=1)
263params.mu = cell(M, 1);
264params.v = cell(M, 1);
267 params.mu{i} = reshape(mu(i, 1:Ki, 1:Ki), Ki, Ki);
268 params.v{i} = reshape(v(i, 1:Ki, 1:Ki), Ki, Ki);
272params.F = zeros(M, 1);
274 if isfield(sn,
'cap') && ~isempty(sn.cap)
275 params.F(i) = sn.cap(i);
276 if isinf(params.F(i))
284% Blocking parameters from options or auto-infer
285qp = options.config.qrf_params;
287 % Validate required fields for BAS blocking configuration
288 if strcmp(options.method, 'qrf.bas')
289 required_bas = {
'f',
'MR',
'BB',
'MM',
'MM1',
'ZZ',
'ZM'};
290 for idx = 1:length(required_bas)
291 if ~isfield(qp, required_bas{idx})
292 line_error(mfilename,
'qrf_params must contain field ''%s'' for qrf.bas method.', required_bas{idx});
296 if isfield(qp,
'f'), params.f = qp.f; end
297 if isfield(qp,
'MR'), params.MR = qp.MR; end
298 if isfield(qp,
'BB'), params.BB = qp.BB; end
299 if isfield(qp,
'MM'), params.MM = qp.MM; end
300 if isfield(qp,
'ZZ'), params.ZZ = qp.ZZ; end
301 if isfield(qp,
'ZM'), params.ZM = qp.ZM; end
302 if isfield(qp,
'MM1'), params.MM1 = qp.MM1; end
304 if strcmp(options.method,
'qrf.bas')
305 line_warning(mfilename, 'qrf_params not provided for qrf.bas; using no-blocking defaults (MR=1).');
307 % Default: no blocking
310 params.BB = zeros(1, M);
311 params.MM = zeros(1, 2);
314 params.MM1 = zeros(1, M);
317% Load-dependent alpha
318if isfield(options.config, 'qrf_alpha') && ~isempty(options.config.qrf_alpha)
319 alpha_mat = options.config.qrf_alpha;
320 params.alpha = cell(M, 1);
322 params.alpha{i} = alpha_mat(i, :)
';
327function [UN_qrf, QN_qrf] = derive_qn_from_bounds(U_bounds, M, N, S, PH, sn)
328% Derive QN from utilization bounds using visit ratios and Little's law.
329% For bounds methods (qrf.bas, qrf.rsrd), only utilization
is returned.
330% We compute system throughput from
the utilization at queue stations,
331% then derive QN at all stations via Little
's law.
334% Compute visit ratios
335if isfield(sn, 'visits') && ~isempty(sn.visits)
336 V = cellsum(sn.visits);
338 [visits] = sn_refresh_visits(sn, sn.chains, sn.rt, sn.rtnodes);
342% Find XN from the first finite-server station with nonzero utilization
345 if ~isinf(S(i)) && ~isempty(PH{i}{1}) && U_bounds(i) > 0 && V(i,1) > 0
346 stime = map_mean(PH{i}{1});
348 % UN = XN * V(i) * stime / S(i), so XN = UN * S(i) / (V(i) * stime)
349 XN_est = U_bounds(i) * S(i) / (V(i, 1) * stime);
355% Derive QN at each station: QN(i) = XN * V(i) * RN(i)
356% For single-server queues: RN(i) >= stime(i) (at least one service time)
357% Use Little's law: QN(i) = TN(i) * RN(i) where TN(i) = XN * V(i)
360 if ~isempty(PH{i}{1})
361 stime = map_mean(PH{i}{1});
362 TN_i = XN_est * V(i, 1);
364 % Delay: QN = TN * stime
365 QN_qrf(i) = TN_i * stime;
366 UN_qrf(i) = QN_qrf(i); % LINE convention
for INF server
368 % Queue: QN = TN * stime / (1 - U)
for M/G/1-like estimate
370 QN_qrf(i) = TN_i * stime / (1 - U_bounds(i));
372 QN_qrf(i) = N; % saturated
378% Rescale to population constraint
380 QN_qrf = QN_qrf / sum(QN_qrf) * N;