LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
solver_ctmc_qrf_analyzer.m
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
3%
4% [QN,UN,RN,TN,CN,XN,RUNTIME] = SOLVER_CTMC_QRF_ANALYZER(SN, OPTIONS)
5%
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.
9%
10% Copyright (c) 2012-2026, Imperial College London
11% All rights reserved.
12
13Tstart = tic;
14
15M = sn.nstations;
16K = sn.nclasses;
17N = sum(sn.njobs);
18S = sn.nservers;
19PH = sn.proc;
20
21% QRF only supports single-class closed networks
22if K ~= 1
23 line_error(mfilename, 'QRF methods only support single-class networks (found %d classes).', K);
24end
25if any(isinf(sn.njobs))
26 line_error(mfilename, 'QRF methods only support closed networks.');
27end
28
29% Extract MAPs as cell array: MAPs{i} = {D0, D1}
30MAPs = cell(M, 1);
31K_phases = zeros(M, 1);
32for i = 1:M
33 if ~isempty(PH{i}{1})
34 MAPs{i} = PH{i}{1};
35 K_phases(i) = size(MAPs{i}{1}, 1);
36 else
37 K_phases(i) = 1;
38 MAPs{i} = {-1, 1}; % fallback exponential rate 1
39 end
40end
41
42% Build routing matrix (M x M) from sn.rt (MK x MK)
43rt = zeros(M);
44for i = 1:M
45 for j = 1:M
46 rt(i,j) = sn.rt(i, j); % K=1, so indexing is direct
47 end
48end
49
50% Extract mu and v arrays from MAPs
51Kmax = max(K_phases);
52mu = zeros(M, Kmax, Kmax);
53v = zeros(M, Kmax, Kmax);
54for i = 1:M
55 D0 = MAPs{i}{1};
56 D1 = MAPs{i}{2};
57 for h = 1:K_phases(i)
58 for k = 1:K_phases(i)
59 mu(i, h, k) = D1(h, k);
60 if h == k
61 v(i, k, h) = 0;
62 else
63 v(i, k, h) = D0(h, k);
64 end
65 end
66 end
67end
68
69% Dispatch based on method
70switch options.method
71 case 'qrf.mmi'
72 MR = 1;
73 [UN_qrf, QN_qrf] = qrf_noblo_mmi(M, MR, K_phases(:)', N, mu, v, rt);
74
75 case 'qrf.mem'
76 [UN_qrf, QN_qrf] = qrf_noblo_mem(MAPs, N, rt);
77
78 case 'qrf.mmi.ld'
79 if isfield(options.config, 'qrf_alpha') && ~isempty(options.config.qrf_alpha)
80 alpha = options.config.qrf_alpha;
81 else
82 alpha = ones(M, N);
83 end
84 [UN_qrf, QN_qrf] = qrf_noblo_mmi_ld(MAPs, N, rt, alpha);
85
86 case 'qrf.mmi.linear'
87 if isfield(options.config, 'qrf_alpha') && ~isempty(options.config.qrf_alpha)
88 alpha = options.config.qrf_alpha;
89 else
90 alpha = ones(M, N);
91 end
92 [UN_qrf, QN_qrf] = qrf_noblo_mmi_linear(MAPs, N, rt, alpha);
93
94 case 'qrf.bas.mmi'
95 qp = options.config.qrf_params;
96 if isempty(qp)
97 line_error(mfilename, 'qrf.bas.mmi requires options.config.qrf_params with fields: f, MR, BB, F.');
98 end
99 f = qp.f;
100 MR = qp.MR;
101 BB = qp.BB;
102 F = qp.F;
103 [UN_qrf, QN_qrf] = qrf_bas_mmi_simple(f, M, MR, BB, K_phases(:)', F, N, mu, v, rt);
104
105 case 'qrf.bas.mem'
106 qp = options.config.qrf_params;
107 if isempty(qp)
108 line_error(mfilename, 'qrf.bas.mem requires options.config.qrf_params with fields: f, MR, MM, MM1, ZZ, ZM, BB, F.');
109 end
110 f = qp.f;
111 MR = qp.MR;
112 MM = qp.MM;
113 MM1 = qp.MM1;
114 ZZ = qp.ZZ;
115 ZM = qp.ZM;
116 BB = qp.BB;
117 F = qp.F;
118 [UN_qrf, QN_qrf] = qrf_bas_mem(f, M, MR, MM, MM1, ZZ, ZM, BB, K_phases(:)', F, N, mu, v, rt);
119
120 case 'qrf.bas'
121 params = sn_to_qrf_params(sn, MAPs, K_phases, N, mu, v, rt, options);
122 [result] = qrf_bas(params);
123 % qrf_bas returns utilization bounds; derive QN via visit ratios
124 [UN_qrf, QN_qrf] = derive_qn_from_bounds(result.U(:)', M, N, S, PH, sn);
125
126 case 'qrf.rsrd'
127 params = sn_to_qrf_params(sn, MAPs, K_phases, N, mu, v, rt, options);
128 [result] = qrf_rsrd(params);
129 [UN_qrf, QN_qrf] = derive_qn_from_bounds(result.U(:)', M, N, S, PH, sn);
130
131 otherwise
132 line_error(mfilename, 'Unknown QRF method: %s', options.method);
133end
134
135% Normalize QN to population constraint
136if sum(QN_qrf) > 0
137 QN_qrf = QN_qrf / sum(QN_qrf) * N;
138end
139
140% Map 1D QRF results to M x K matrices (K=1)
141UN = zeros(M, K);
142QN = zeros(M, K);
143TN = zeros(M, K);
144RN = zeros(M, K);
145XN = zeros(1, K);
146CN = zeros(1, K);
147
148QN(:, 1) = QN_qrf(:);
149
150% Derive all metrics from QN using Little's law and visit ratios.
151% QRF's "UN" is the sum of phase effective utilizations, which can exceed 1
152% for multi-phase PH service; it does not match LINE's utilization definition.
153% Instead, we derive throughput from QN at a delay station (if present) or
154% from the visit-ratio weighted relationship, then compute UN = TN * stime / S.
155
156% Compute visit ratios
157if isfield(sn, 'visits') && ~isempty(sn.visits)
158 V = cellsum(sn.visits);
159else
160 [visits] = sn_refresh_visits(sn, sn.chains, sn.rt, sn.rtnodes);
161 V = cellsum(visits);
162end
163
164refstat = sn.refstat(1);
165
166% Find system throughput XN from QN using Little's law at reference station
167stime_ref = 0;
168if ~isempty(PH{refstat}{1})
169 stime_ref = map_mean(PH{refstat}{1});
170end
171if stime_ref > 0 && V(refstat, 1) > 0
172 % QN(refstat) = XN * V(refstat,1) * stime_ref (for INF server at delay)
173 % QN(refstat) = TN(refstat) * RN(refstat) in general
174 % For delay: RN = stime, so QN = TN * stime = XN * V(refstat,1) * stime
175 XN(1) = QN(refstat, 1) / (V(refstat, 1) * stime_ref);
176end
177
178% Derive per-station metrics from XN and visit ratios
179for i = 1:M
180 if ~isempty(PH{i}{1})
181 stime = map_mean(PH{i}{1});
182 TN(i, 1) = XN(1) * V(i, 1);
183 if isinf(S(i))
184 % Delay (infinite server): UN = QN by LINE convention
185 UN(i, 1) = QN(i, 1);
186 else
187 % Finite server: UN = TN * stime / S
188 if stime > 0
189 UN(i, 1) = TN(i, 1) * stime / S(i);
190 end
191 end
192 % Response time via Little's law
193 if TN(i, 1) > 0
194 RN(i, 1) = QN(i, 1) / TN(i, 1);
195 end
196 end
197end
198
199% Cycle time
200if XN(1) > 0
201 CN(1) = N / XN(1);
202end
203
204% Clean NaN values
205QN(isnan(QN)) = 0;
206UN(isnan(UN)) = 0;
207RN(isnan(RN)) = 0;
208TN(isnan(TN)) = 0;
209XN(isnan(XN)) = 0;
210CN(isnan(CN)) = 0;
211
212runtime = toc(Tstart);
213end
214
215function params = sn_to_qrf_params(sn, MAPs, K_phases, N, mu, v, rt, options)
216% Build params struct for qrf_bas / qrf_rsrd from sn struct
217M = sn.nstations;
218params = struct();
219params.M = M;
220params.N = N;
221params.K = K_phases(:);
222params.r = rt;
223
224% Convert mu/v from 3D arrays to cell arrays expected by qrf_bas/qrf_rsrd
225% Each mu{i}, v{i} must be a [Ki x Ki] matrix (even for Ki=1)
226params.mu = cell(M, 1);
227params.v = cell(M, 1);
228for i = 1:M
229 Ki = K_phases(i);
230 params.mu{i} = reshape(mu(i, 1:Ki, 1:Ki), Ki, Ki);
231 params.v{i} = reshape(v(i, 1:Ki, 1:Ki), Ki, Ki);
232end
233
234% Capacity
235params.F = zeros(M, 1);
236for i = 1:M
237 if isfield(sn, 'cap') && ~isempty(sn.cap)
238 params.F(i) = sn.cap(i);
239 if isinf(params.F(i))
240 params.F(i) = N;
241 end
242 else
243 params.F(i) = N;
244 end
245end
246
247% Blocking parameters from options or auto-infer
248qp = options.config.qrf_params;
249if ~isempty(qp)
250 % Validate required fields for BAS blocking configuration
251 if strcmp(options.method, 'qrf.bas')
252 required_bas = {'f', 'MR', 'BB', 'MM', 'MM1', 'ZZ', 'ZM'};
253 for idx = 1:length(required_bas)
254 if ~isfield(qp, required_bas{idx})
255 line_error(mfilename, 'qrf_params must contain field ''%s'' for qrf.bas method.', required_bas{idx});
256 end
257 end
258 end
259 if isfield(qp, 'f'), params.f = qp.f; end
260 if isfield(qp, 'MR'), params.MR = qp.MR; end
261 if isfield(qp, 'BB'), params.BB = qp.BB; end
262 if isfield(qp, 'MM'), params.MM = qp.MM; end
263 if isfield(qp, 'ZZ'), params.ZZ = qp.ZZ; end
264 if isfield(qp, 'ZM'), params.ZM = qp.ZM; end
265 if isfield(qp, 'MM1'), params.MM1 = qp.MM1; end
266else
267 if strcmp(options.method, 'qrf.bas')
268 line_warning(mfilename, 'qrf_params not provided for qrf.bas; using no-blocking defaults (MR=1).');
269 end
270 % Default: no blocking
271 params.f = 1;
272 params.MR = 1;
273 params.BB = zeros(1, M);
274 params.MM = zeros(1, 2);
275 params.ZZ = 0;
276 params.ZM = 0;
277 params.MM1 = zeros(1, M);
278end
279
280% Load-dependent alpha
281if isfield(options.config, 'qrf_alpha') && ~isempty(options.config.qrf_alpha)
282 alpha_mat = options.config.qrf_alpha;
283 params.alpha = cell(M, 1);
284 for i = 1:M
285 params.alpha{i} = alpha_mat(i, :)';
286 end
287end
288end
289
290function [UN_qrf, QN_qrf] = derive_qn_from_bounds(U_bounds, M, N, S, PH, sn)
291% Derive QN from utilization bounds using visit ratios and Little's law.
292% For bounds methods (qrf.bas, qrf.rsrd), only utilization is returned.
293% We compute system throughput from the utilization at queue stations,
294% then derive QN at all stations via Little's law.
295UN_qrf = U_bounds;
296
297% Compute visit ratios
298if isfield(sn, 'visits') && ~isempty(sn.visits)
299 V = cellsum(sn.visits);
300else
301 [visits] = sn_refresh_visits(sn, sn.chains, sn.rt, sn.rtnodes);
302 V = cellsum(visits);
303end
304
305% Find XN from the first finite-server station with nonzero utilization
306XN_est = 0;
307for i = 1:M
308 if ~isinf(S(i)) && ~isempty(PH{i}{1}) && U_bounds(i) > 0 && V(i,1) > 0
309 stime = map_mean(PH{i}{1});
310 if stime > 0
311 % UN = XN * V(i) * stime / S(i), so XN = UN * S(i) / (V(i) * stime)
312 XN_est = U_bounds(i) * S(i) / (V(i, 1) * stime);
313 break;
314 end
315 end
316end
317
318% Derive QN at each station: QN(i) = XN * V(i) * RN(i)
319% For single-server queues: RN(i) >= stime(i) (at least one service time)
320% Use Little's law: QN(i) = TN(i) * RN(i) where TN(i) = XN * V(i)
321QN_qrf = zeros(1, M);
322for i = 1:M
323 if ~isempty(PH{i}{1})
324 stime = map_mean(PH{i}{1});
325 TN_i = XN_est * V(i, 1);
326 if isinf(S(i))
327 % Delay: QN = TN * stime
328 QN_qrf(i) = TN_i * stime;
329 UN_qrf(i) = QN_qrf(i); % LINE convention for INF server
330 else
331 % Queue: QN = TN * stime / (1 - U) for M/G/1-like estimate
332 if U_bounds(i) < 1
333 QN_qrf(i) = TN_i * stime / (1 - U_bounds(i));
334 else
335 QN_qrf(i) = N; % saturated
336 end
337 end
338 end
339end
340
341% Rescale to population constraint
342if sum(QN_qrf) > 0
343 QN_qrf = QN_qrf / sum(QN_qrf) * N;
344end
345end