LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
infer_compute_ql_at_arrival.m
1function ql = infer_compute_ql_at_arrival(at, at_jobid, rt, rt_jobid, class, R)
2% INFER_COMPUTE_QL_AT_ARRIVAL Compute per-class queue lengths at arrival.
3%
4% Reconstructs the queue state seen by each arriving job using arrival
5% and departure times. Arrival times and response times are matched by
6% job ID, so they need not be in the same order or come from the same
7% data source.
8%
9% At ties, departures are processed before arrivals.
10%
11% Inputs:
12% at - arrival times (column vector, n x 1)
13% at_jobid - job IDs for arrival times (column vector, n x 1)
14% rt - response times (column vector, m x 1, m >= n)
15% rt_jobid - job IDs for response times (column vector, m x 1)
16% class - class of each arrival sample (column vector, n x 1,
17% ordered consistently with at and at_jobid)
18% R - number of classes
19%
20% Returns:
21% ql - n x R matrix of per-class queue lengths at each arrival,
22% rows ordered consistently with the input at/at_jobid
23%
24% Copyright (c) 2012-2026, Imperial College London
25% All rights reserved.
26% This code is released under the 3-Clause BSD License.
27
28n = length(at);
29
30% Match response times to arrivals by job ID
31[found, loc] = ismember(at_jobid, rt_jobid);
32if ~all(found)
33 error('infer_compute_ql_at_arrival: not all arrival job IDs found in response time job IDs.');
34end
35rt_matched = rt(loc);
36
37% Sort arrivals by time
38[at_sorted, sortIdx] = sort(at);
39class_sorted = class(sortIdx);
40rt_sorted = rt_matched(sortIdx);
41
42exitTimes = at_sorted + rt_sorted;
43
44% Event list: [time, type (-1=dep/+1=arv), sortedIdx, class]
45events = zeros(2*n, 4);
46events(1:n, :) = [at_sorted, ones(n,1), (1:n)', class_sorted];
47events(n+1:2*n, :) = [exitTimes, -ones(n,1), (1:n)', class_sorted];
48
49% Sort by time; departures (-1) before arrivals (+1) at same time
50events = sortrows(events, [1, 2]);
51
52state = zeros(1, R);
53ql_sorted = zeros(n, R);
54for i = 1:2*n
55 c = events(i, 4);
56 if events(i, 2) == 1 % arrival
57 state(c) = state(c) + 1;
58 ql_sorted(events(i, 3), :) = state;
59 else % departure
60 state(c) = state(c) - 1;
61 end
62end
63
64% Unsort back to original input order
65ql = zeros(n, R);
66ql(sortIdx, :) = ql_sorted;
67
68end