LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
passAndSwap.m
1function [cnew, depClass, chain] = passAndSwap(c, p, G)
2% [CNEW, DEPCLASS, CHAIN] = PASSANDSWAP(C, P, G)
3%
4% Applies the pass-and-swap mechanism (Dorsman & Gardner 2024, Queueing
5% Systems 107:205-256, Sect. 2.3) triggered by the service completion of the
6% job in position P of an order-independent/pass-and-swap (PAS) queue.
7%
8% Inputs:
9% c - 1 x n ordered state vector of class indices; c(1) is the oldest job.
10% p - position (1..n) of the job whose service token completes.
11% G - (nclasses x nclasses) swapping graph adjacency (G(i,j)~=0 iff a class-i
12% job may take the place of a class-j job; undirected, self-loops allowed).
13%
14% Outputs:
15% cnew - 1 x (n-1) ordered state vector after the pass-and-swap transition.
16% depClass - class index of the job that departs the system.
17% chain - sequence of positions (in the original c) visited by the scan;
18% chain(1)=p is the vacated slot, c(chain(end)) is the departing job.
19%
20% Mechanism: starting at position p, the completing job scans backwards (towards
21% newer/later positions) for the first job whose class is swappable with its own
22% per G; it takes that job's place, ejecting it. The ejected job repeats the
23% scan from its new position. The chain ends when an ejected job finds no
24% swappable successor: that job departs. Classes shift one step along the chain
25% and the head-of-chain slot is removed.
26%
27% Copyright (c) 2012-2026, Imperial College London
28% All rights reserved.
29
30n = numel(c);
31if p < 1 || p > n
32 line_error(mfilename, 'Completion position p is out of range for state c.');
33end
34
35chain = p; % q0: position of the completing job (becomes vacated)
36movingCls = c(p); % class currently looking for a swappable successor
37curpos = p;
38while true
39 q = 0;
40 for j = curpos+1:n
41 if G(movingCls, c(j))
42 q = j;
43 break;
44 end
45 end
46 if q == 0
47 break; % movingCls finds no swappable successor -> it departs
48 end
49 chain(end+1) = q; %#ok<AGROW>
50 movingCls = c(q);
51 curpos = q;
52end
53
54depClass = c(chain(end)); % the last ejected job departs
55
56% Shift classes one step along the chain: c(chain(i)) -> chain(i+1).
57% The class previously at chain(end) is overwritten (it departed); the
58% head-of-chain slot chain(1) is then removed.
59cnew = c;
60for i = 1:numel(chain)-1
61 cnew(chain(i+1)) = c(chain(i));
62end
63cnew(chain(1)) = [];
64end