LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
fj_rmax_erlang.m
1%{ @file fj_rmax_erlang.m
2 % @brief Maximum response time R_K^max for Erlang service times
3 %
4 % @author LINE Development Team
5%}
6
7%{
8 % @brief Maximum response time R_K^max for Erlang service times
9 %
10 % @details
11 % Computes the expected value of the maximum response time for K
12 % M/E_k/1 queues (Poisson arrivals, Erlang-k service times).
13 %
14 % For K=2 (two servers), uses the closed-form formula:
15 % R_2^max = R_1 + R_2 - sum_{m=0}^{k1-1} sum_{n=0}^{k2-1} C(m+n,m) * mu1^m * mu2^n / (mu1+mu2)^{m+n+1}
16 %
17 % For general K, uses numerical integration:
18 % R_K^max = integral_0^inf [1 - prod_{i=1}^{K} F_Erlang(t)] dt
19 %
20 % where F_Erlang(t) = 1 - exp(-mu*t) * sum_{j=0}^{k-1} (mu*t)^j / j!
21 %
22 % @par Syntax:
23 % @code
24 % Rmax = fj_rmax_erlang(K, k, lambda, mu)
25 % @endcode
26 %
27 % @par Parameters:
28 % <table>
29 % <tr><th>Name<th>Description
30 % <tr><td>K<td>Number of parallel servers (positive integer)
31 % <tr><td>k<td>Number of Erlang stages (positive integer)
32 % <tr><td>lambda<td>Arrival rate
33 % <tr><td>mu<td>Service rate per Erlang stage (mean service = k/mu)
34 % </table>
35 %
36 % @par Returns:
37 % <table>
38 % <tr><th>Name<th>Description
39 % <tr><td>Rmax<td>Expected maximum response time R_K^max
40 % </table>
41 %
42 % @par Reference:
43 % A. Thomasian, "Analysis of Fork/Join and Related Queueing Systems",
44 % ACM Computing Surveys, Vol. 47, No. 2, Article 17, July 2014.
45 % Eq. (33) and (34) on page 17:18.
46%}
47function Rmax = fj_rmax_erlang(K, k, lambda, mu)
48
49if K < 1
50 line_error(mfilename, 'K must be a positive integer. Got K=%d.', K);
51end
52
53if k < 1
54 line_error(mfilename, 'k (number of stages) must be a positive integer. Got k=%d.', k);
55end
56
57% Compute mean service time and utilization
58mean_service = k / mu;
59rho = lambda * mean_service;
60
61if rho >= 1
62 line_error(mfilename, 'System is unstable: rho = %.4f >= 1.', rho);
63end
64
65% Compute response time for M/E_k/1 queue using Pollaczek-Khinchin formula
66% R = mean_service / (1 - rho) * [1 + rho * (1 + 1/k) / 2]
67% Simplified: R = mean_service * (1 + rho*(k+1)/(2*k*(1-rho)))
68cv2 = 1 / k; % Squared CV of Erlang-k
69R_single = mean_service * (1 + rho * (1 + cv2) / (2 * (1 - rho)));
70
71if K == 2
72 % Use closed-form formula (Eq. 34)
73 % For identical servers: k1 = k2 = k, mu1 = mu2 = k/R
74 mu_resp = k / R_single; % Effective rate for response time
75
76 % R_2^max = 2*R - sum_{m=0}^{k-1} sum_{n=0}^{k-1} C(m+n,m) * mu^m * mu^n / (2*mu)^{m+n+1}
77 correction = 0;
78 for m = 0:(k-1)
79 for n = 0:(k-1)
80 correction = correction + nchoosek(m + n, m) * (mu_resp^m) * (mu_resp^n) / ((2 * mu_resp)^(m + n + 1));
81 end
82 end
83 Rmax = 2 * R_single - correction;
84else
85 % Use numerical integration for general K
86 % The response time distribution for M/E_k/1 can be approximated by Erlang
87 % with parameters matched to the response time moments
88
89 % For M/E_k/1, use the approximation that response time is approximately
90 % Erlang with adjusted parameters
91 k_resp = ceil(1 / cv2_response(k, rho)); % Effective stages for response time
92 mu_resp = k_resp / R_single;
93
94 % Erlang CDF for response time
95 erlang_cdf = @(t) erlang_cdf_func(t, k_resp, mu_resp);
96
97 % Expected maximum: integral_0^inf [1 - F(t)^K] dt
98 integrand = @(t) 1 - erlang_cdf(t).^K;
99
100 % Numerical integration
101 upper_limit = R_single * 20; % Well beyond the tail
102 Rmax = integral(integrand, 0, upper_limit, 'RelTol', 1e-10, 'AbsTol', 1e-12);
103end
104
105end
106
107function cv2 = cv2_response(k, rho)
108% Squared CV of response time for M/E_k/1 queue (approximation)
109% Based on: c2_R ≈ (c2_X + rho*(1+c2_X)/2) / (1 + rho*(1+c2_X)/2)
110cv2_X = 1 / k;
111cv2 = (cv2_X + rho) / (1 + rho); % Simplified approximation
112cv2 = max(cv2, 1/20); % Bound to avoid numerical issues
113end
114
115function F = erlang_cdf_func(t, k, mu)
116% Erlang-k CDF: F(t) = 1 - exp(-mu*t) * sum_{j=0}^{k-1} (mu*t)^j / j!
117F = zeros(size(t));
118for i = 1:numel(t)
119 if t(i) <= 0
120 F(i) = 0;
121 else
122 S = 0;
123 for j = 0:(k-1)
124 S = S + (mu * t(i))^j / factorial(j);
125 end
126 F(i) = 1 - exp(-mu * t(i)) * S;
127 end
128end
129end