1function [QN,UN,RN,TN,CN,XN,runtime] = solver_ctmc_qrf_analyzer(sn, options)
2% SOLVER_CTMC_QRF_ANALYZER Adapter
for QRF library functions within SolverCTMC
4% [QN,UN,RN,TN,CN,XN,RUNTIME] = SOLVER_CTMC_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 mu(i, h, k) = D1(h, k);
63 v(i, k, h) = D0(h, k);
69% Dispatch based on method
73 [UN_qrf, QN_qrf] = qrf_noblo_mmi(M, MR, K_phases(:)
', N, mu, v, rt);
76 [UN_qrf, QN_qrf] = qrf_noblo_mem(MAPs, N, rt);
79 if isfield(options.config, 'qrf_alpha
') && ~isempty(options.config.qrf_alpha)
80 alpha = options.config.qrf_alpha;
84 [UN_qrf, QN_qrf] = qrf_noblo_mmi_ld(MAPs, N, rt, alpha);
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_linear(MAPs, N, rt, alpha);
95 qp = options.config.qrf_params;
97 line_error(mfilename, 'qrf.bas.mmi requires options.config.qrf_params with fields: f, MR, BB, F.
');
103 [UN_qrf, QN_qrf] = qrf_bas_mmi_simple(f, M, MR, BB, K_phases(:)', F, N, mu, v, rt);
106 qp = options.config.qrf_params;
108 line_error(mfilename,
'qrf.bas.mem requires options.config.qrf_params with fields: f, MR, MM, MM1, ZZ, ZM, BB, F.');
118 [UN_qrf, QN_qrf] = qrf_bas_mem(f, M, MR, MM, MM1, ZZ, ZM, BB, K_phases(:)
', F, N, mu, v, rt);
121 params = sn_to_qrf_params(sn, MAPs, K_phases, N, mu, v, rt, options);
122 params.verbose = options.verbose > 0;
123 [result] = qrf_bas(params);
124 % qrf_bas returns utilization bounds; derive QN via visit ratios
125 [UN_qrf, QN_qrf] = derive_qn_from_bounds(result.U(:)', M, N, S, PH, sn);
128 params = sn_to_qrf_params(sn, MAPs, K_phases, N, mu, v, rt, options);
129 params.verbose = options.verbose > 0;
130 [result] = qrf_rsrd(params);
131 [UN_qrf, QN_qrf] = derive_qn_from_bounds(result.U(:)
', M, N, S, PH, sn);
134 line_error(mfilename, 'Unknown QRF method: %s
', options.method);
137% Normalize QN to population constraint
139 QN_qrf = QN_qrf / sum(QN_qrf) * N;
142% Map 1D QRF results to M x K matrices (K=1)
152% Derive all metrics from QN using Little's law and visit ratios.
153% QRF
's "UN" is the sum of phase effective utilizations, which can exceed 1
154% for multi-phase PH service; it does not match LINE's utilization definition.
155% Instead, we derive throughput from QN at a delay station (if present) or
156% from the visit-ratio weighted relationship, then compute UN = TN * stime / S.
158% Compute visit ratios
159if isfield(sn,
'visits') && ~isempty(sn.
visits)
162 [
visits] = sn_refresh_visits(sn, sn.chains, sn.rt, sn.rtnodes);
166refstat = sn.refstat(1);
168% Find system throughput XN from QN
using Little
's law at reference station
170if ~isempty(PH{refstat}{1})
171 stime_ref = map_mean(PH{refstat}{1});
173if stime_ref > 0 && V(refstat, 1) > 0
174 % QN(refstat) = XN * V(refstat,1) * stime_ref (for INF server at delay)
175 % QN(refstat) = TN(refstat) * RN(refstat) in general
176 % For delay: RN = stime, so QN = TN * stime = XN * V(refstat,1) * stime
177 XN(1) = QN(refstat, 1) / (V(refstat, 1) * stime_ref);
180% Derive per-station metrics from XN and visit ratios
182 if ~isempty(PH{i}{1})
183 stime = map_mean(PH{i}{1});
184 TN(i, 1) = XN(1) * V(i, 1);
186 % Delay (infinite server): UN = QN by LINE convention
189 % Finite server: UN = TN * stime / S
191 UN(i, 1) = TN(i, 1) * stime / S(i);
194 % Response time via Little's law
196 RN(i, 1) = QN(i, 1) / TN(i, 1);
214runtime = toc(Tstart);
217function params = sn_to_qrf_params(sn, MAPs, K_phases, N, mu, v, rt, options)
218% Build params
struct for qrf_bas / qrf_rsrd from sn struct
223params.K = K_phases(:);
226% Convert mu/v from 3D arrays to cell arrays expected by qrf_bas/qrf_rsrd
227% Each mu{i}, v{i} must be a [Ki x Ki] matrix (even
for Ki=1)
228params.mu = cell(M, 1);
229params.v = cell(M, 1);
232 params.mu{i} = reshape(mu(i, 1:Ki, 1:Ki), Ki, Ki);
233 params.v{i} = reshape(v(i, 1:Ki, 1:Ki), Ki, Ki);
237params.F = zeros(M, 1);
239 if isfield(sn,
'cap') && ~isempty(sn.cap)
240 params.F(i) = sn.cap(i);
241 if isinf(params.F(i))
249% Blocking parameters from options or auto-infer
250qp = options.config.qrf_params;
252 % Validate required fields for BAS blocking configuration
253 if strcmp(options.method, 'qrf.bas')
254 required_bas = {
'f',
'MR',
'BB',
'MM',
'MM1',
'ZZ',
'ZM'};
255 for idx = 1:length(required_bas)
256 if ~isfield(qp, required_bas{idx})
257 line_error(mfilename,
'qrf_params must contain field ''%s'' for qrf.bas method.', required_bas{idx});
261 if isfield(qp,
'f'), params.f = qp.f; end
262 if isfield(qp,
'MR'), params.MR = qp.MR; end
263 if isfield(qp,
'BB'), params.BB = qp.BB; end
264 if isfield(qp,
'MM'), params.MM = qp.MM; end
265 if isfield(qp,
'ZZ'), params.ZZ = qp.ZZ; end
266 if isfield(qp,
'ZM'), params.ZM = qp.ZM; end
267 if isfield(qp,
'MM1'), params.MM1 = qp.MM1; end
269 if strcmp(options.method,
'qrf.bas')
270 line_warning(mfilename, 'qrf_params not provided for qrf.bas; using no-blocking defaults (MR=1).');
272 % Default: no blocking
275 params.BB = zeros(1, M);
276 params.MM = zeros(1, 2);
279 params.MM1 = zeros(1, M);
282% Load-dependent alpha
283if isfield(options.config, 'qrf_alpha') && ~isempty(options.config.qrf_alpha)
284 alpha_mat = options.config.qrf_alpha;
285 params.alpha = cell(M, 1);
287 params.alpha{i} = alpha_mat(i, :)
';
292function [UN_qrf, QN_qrf] = derive_qn_from_bounds(U_bounds, M, N, S, PH, sn)
293% Derive QN from utilization bounds using visit ratios and Little's law.
294% For bounds methods (qrf.bas, qrf.rsrd), only utilization
is returned.
295% We compute system throughput from the utilization at queue stations,
296% then derive QN at all stations via Little
's law.
299% Compute visit ratios
300if isfield(sn, 'visits') && ~isempty(sn.visits)
301 V = cellsum(sn.visits);
303 [visits] = sn_refresh_visits(sn, sn.chains, sn.rt, sn.rtnodes);
307% Find XN from the first finite-server station with nonzero utilization
310 if ~isinf(S(i)) && ~isempty(PH{i}{1}) && U_bounds(i) > 0 && V(i,1) > 0
311 stime = map_mean(PH{i}{1});
313 % UN = XN * V(i) * stime / S(i), so XN = UN * S(i) / (V(i) * stime)
314 XN_est = U_bounds(i) * S(i) / (V(i, 1) * stime);
320% Derive QN at each station: QN(i) = XN * V(i) * RN(i)
321% For single-server queues: RN(i) >= stime(i) (at least one service time)
322% Use Little's law: QN(i) = TN(i) * RN(i) where TN(i) = XN * V(i)
325 if ~isempty(PH{i}{1})
326 stime = map_mean(PH{i}{1});
327 TN_i = XN_est * V(i, 1);
329 % Delay: QN = TN * stime
330 QN_qrf(i) = TN_i * stime;
331 UN_qrf(i) = QN_qrf(i); % LINE convention
for INF server
333 % Queue: QN = TN * stime / (1 - U)
for M/G/1-like estimate
335 QN_qrf(i) = TN_i * stime / (1 - U_bounds(i));
337 QN_qrf(i) = N; % saturated
343% Rescale to population constraint
345 QN_qrf = QN_qrf / sum(QN_qrf) * N;