LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
solver_nc_lcfsqn.m
1function [Q,U,R,T,C,X,lG] = solver_nc_lcfsqn(sn, options, lcfsStat, lcfsprStat)
2% [Q,U,R,T,C,X,LG] = SOLVER_NC_LCFSQN(SN, OPTIONS, LCFSSTAT, LCFSPRSTAT)
3% Specialized NC solver for LCFS + LCFS-PR 2-station networks
4%
5% This function wraps the pfqn_lcfsqn_ca algorithm and computes performance
6% metrics using the convolution approach.
7%
8% Parameters:
9% sn - network structure
10% options - solver options
11% lcfsStat - index of the LCFS station
12% lcfsprStat - index of the LCFS-PR station
13%
14% Returns:
15% Q - queue length matrix (stations x classes)
16% U - utilization matrix (stations x classes)
17% R - response time matrix (stations x classes)
18% T - throughput matrix (stations x classes)
19% C - cycle time vector (1 x classes)
20% X - throughput vector (1 x classes)
21% lG - log of normalizing constant
22%
23% Copyright (c) 2012-2026, Imperial College London
24% All rights reserved.
25
26M = sn.nstations;
27R = sn.nclasses;
28njobs = sn.njobs;
29
30% Extract service times for each class at each station
31% alpha(r) = mean service time at LCFS station for class r
32% beta(r) = mean service time at LCFS-PR station for class r
33alpha = zeros(1, R);
34beta = zeros(1, R);
35
36rates = sn.rates;
37for r = 1:R
38 if njobs(r) > 0
39 % Service rate at station for class r
40 mu_lcfs = rates(lcfsStat, r);
41 mu_lcfspr = rates(lcfsprStat, r);
42
43 if mu_lcfs <= 0 || ~isfinite(mu_lcfs)
44 line_error(mfilename, sprintf('Invalid service rate at LCFS station for class %d.', r));
45 end
46 if mu_lcfspr <= 0 || ~isfinite(mu_lcfspr)
47 line_error(mfilename, sprintf('Invalid service rate at LCFS-PR station for class %d.', r));
48 end
49
50 alpha(r) = 1 / mu_lcfs;
51 beta(r) = 1 / mu_lcfspr;
52 end
53end
54
55% Get population vector
56N = njobs;
57K = sum(N);
58
59% Call the LCFS convolution algorithm to get normalizing constant
60[G, ~] = pfqn_lcfsqn_ca(alpha, beta, N);
61
62% Compute log normalizing constant
63if G > 0
64 lG = log(G);
65else
66 lG = -Inf;
67end
68
69% Initialize output matrices
70Q_lcfs = zeros(2, R);
71U_lcfs = zeros(2, R);
72T_lcfs = zeros(2, R);
73
74% Compute throughputs and queue lengths for each class
75% Based on the approach in lcfsqn_perf.m
76for r = 1:R
77 if njobs(r) > 0
78 % Compute throughput using permanent calculations
79 for xt = 1:K
80 Tx = make_Tx(alpha, beta, xt, K, R, r);
81 permT = perm(Tx);
82 T_lcfs(1:2,r) = T_lcfs(1:2,r) + alpha(r)^(xt-1) * permT / G;
83
84 % Compute queue length at station 1 (LCFS)
85 Yx = make_Yx(alpha, beta, xt, K, R, r);
86 permY = perm(Yx);
87 Q_lcfs(1,r) = Q_lcfs(1,r) + permY/G;
88 end
89 % Queue length at station 2 (LCFS-PR) by conservation
90 Q_lcfs(2,r) = njobs(r) - Q_lcfs(1,r);
91 end
92end
93
94% Compute utilizations
95for i = 1:2
96 for r = 1:R
97 if i == 1
98 U_lcfs(i,r) = T_lcfs(i,r) * alpha(r);
99 else
100 U_lcfs(i,r) = T_lcfs(i,r) * beta(r);
101 end
102 end
103end
104
105% Map results back to LINE format
106% Initialize output matrices for all stations
107Q = zeros(M, R);
108U = zeros(M, R);
109T = zeros(M, R);
110R_resp = zeros(M, R);
111X = zeros(1, R);
112C = zeros(1, R);
113
114% Map queue lengths
115Q(lcfsStat, :) = Q_lcfs(1, :);
116Q(lcfsprStat, :) = Q_lcfs(2, :);
117
118% Map utilizations
119U(lcfsStat, :) = U_lcfs(1, :);
120U(lcfsprStat, :) = U_lcfs(2, :);
121
122% Throughput is the same at all stations in a closed network
123for r = 1:R
124 if njobs(r) > 0
125 X(r) = T_lcfs(1,r);
126 T(lcfsStat, r) = T_lcfs(1,r);
127 T(lcfsprStat, r) = T_lcfs(2,r);
128 end
129end
130
131% Compute response times: R = Q / T (using Little's Law)
132for k = [lcfsStat, lcfsprStat]
133 for r = 1:R
134 if T(k, r) > 0
135 R_resp(k, r) = Q(k, r) / T(k, r);
136 end
137 end
138end
139
140% Compute cycle times: C = sum of response times at all stations
141for r = 1:R
142 if njobs(r) > 0
143 C(r) = R_resp(lcfsStat, r) + R_resp(lcfsprStat, r);
144 end
145end
146
147% Return R_resp in variable R (overriding function parameter)
148R = R_resp;
149
150end
151
152function Tx = make_Tx(alpha, beta, xt, K, R, r)
153% Make Tx matrix for throughput computation
154% alpha : vector of length R
155% beta : vector of length R
156% xt : integer
157% K : total population
158% R : number of classes
159% r : class index to exclude
160
161if issym(alpha)
162 Tx = sym(zeros(K-1, K-1));
163else
164 Tx = zeros(K-1, K-1);
165end
166
167idx = 0;
168for i = 1:R
169 if i ~= r
170 idx = idx + 1;
171 for j = 1:(xt-1)
172 Tx(idx, j) = alpha(i)^j;
173 end
174 for j = 1:(K-xt)
175 Tx(idx, xt-1+j) = alpha(i)^(xt+j-1) * beta(i);
176 end
177 end
178end
179end
180
181function Y = make_Yx(alpha, beta, xt, K, R, r)
182% Make Yx matrix for queue length computation
183% alpha : vector of length R
184% beta : vector of length R
185% xt : integer
186% K : total population
187% R : number of classes
188% r : class index
189
190if issym(alpha)
191 Y = sym(zeros(K, K));
192else
193 Y = zeros(K, K);
194end
195
196for i = 1:R
197 for j = 1:xt
198 Y(i, j) = alpha(i)^j;
199 end
200 for j = 1:(K-xt)
201 if i~=r
202 Y(i, xt+j) = alpha(i)^(xt+j-1) * beta(i);
203 end
204 end
205end
206end