LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
qsys_mg1_prio.m
1function [W, rho] = qsys_mg1_prio(lambda, mu, cs)
2% W=QSYS_MG1_PRIO(LAMBDA,MU,CS)
3%
4% Analyzes an M/G/1 queueing system with non-preemptive (Head-of-Line) priorities.
5%
6% This function computes per-class mean waiting times for multiple priority classes
7% using the Pollaczek-Khinchine formula extended for priorities.
8%
9% For K priority classes (class 1 = highest priority), the waiting time for class k is:
10% W_k = (B_0 / (1 - sum_{i=1}^{k-1} rho_i)) * (1 / (1 - sum_{i=1}^{k} rho_i))
11%
12% Where:
13% - rho_i = lambda_i / mu_i (utilization of class i)
14% - B_0 = sum_{i=1}^{K} lambda_i * (1/mu_i^2) * (1 + cs_i^2)
15% - cs_i = coefficient of variation for class i service time
16%
17% PARAMETERS:
18% lambda : Vector of arrival rates per priority class (class 1 = highest)
19% mu : Vector of service rates per priority class
20% cs : Vector of coefficients of variation per priority class
21%
22% RETURNS:
23% W : Vector of mean waiting times per priority class
24% rho : Overall system utilization (scalar)
25%
26% EXAMPLE:
27% % Two priority classes
28% lambda = [0.2; 0.3]; % Arrival rates
29% mu = [1.0; 1.0]; % Service rates
30% cs = [0.5; 1.0]; % Coefficients of variation
31% [W, rho] = qsys_mg1_prio(lambda, mu, cs);
32%
33% REFERENCE:
34% Kleinrock, L., "Queueing Systems, Volume I: Theory", Wiley, 1975, Section 3.5
35
36% Ensure inputs are column vectors
37lambda = reshape(lambda, [], 1);
38mu = reshape(mu, [], 1);
39cs = reshape(cs, [], 1);
40
41% Validate input lengths
42if ~isequal(length(lambda), length(mu), length(cs))
43 error('qsys_mg1_prio:InvalidInput', ...
44 'lambda, mu, and cs must have the same length');
45end
46
47% Validate positive values
48if any(lambda <= 0) || any(mu <= 0) || any(cs <= 0)
49 error('qsys_mg1_prio:InvalidInput', ...
50 'lambda, mu, and cs must all be positive');
51end
52
53% Compute per-class utilizations
54rho_i = lambda ./ mu;
55
56% Overall utilization
57rho = sum(rho_i);
58
59% Stability check
60if rho >= 1
61 error('qsys_mg1_prio:UnstableSystem', ...
62 sprintf('System is unstable: utilization rho = %g >= 1', rho));
63end
64
65% Compute mean second moment of service time (used in waiting time formula)
66% B_0 = sum_i lambda_i * E[S_i^2] / 2
67% where E[S_i^2] = (1 + cs_i^2) / mu_i^2
68B_0 = sum(lambda .* (1 + cs.^2) ./ (mu.^2)) / 2;
69
70% Compute per-class waiting times (in queue, not including service)
71K = length(lambda);
72W_q = zeros(K, 1);
73
74for k = 1:K
75 % Cumulative sum of utilizations for higher priority classes (1 to k-1)
76 if k == 1
77 rho_prev = 0;
78 else
79 rho_prev = sum(rho_i(1:k-1));
80 end
81
82 % Cumulative sum including current class (1 to k)
83 rho_curr = sum(rho_i(1:k));
84
85 % Waiting time (in queue) for class k
86 % W_q_k = B_0 / ((1 - rho_prev) * (1 - rho_curr))
87 W_q(k) = B_0 / ((1 - rho_prev) * (1 - rho_curr));
88end
89
90% Response time = waiting time + service time
91% W_k = W_q_k + 1/mu_k
92W = W_q + 1 ./ mu;
93
94% Compute rhohat = Q/(1+Q) to match qsys_mg1 convention
95% Q is the mean number of customers in the system (Little's law)
96% Q = sum_k lambda_k * W_k
97Q = sum(lambda .* W);
98rho = Q / (1 + Q);
99
100end