LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
toMarginalAggr.m
1function [ni, nir] = toMarginalAggr(sn, ind, state_i, K, Ks, space_buf, space_srv, space_var) %#ok<INUSD>
2% TOMARGINALAGGR Compute aggregate marginal distributions for a specific node
3%
4% [NI, NIR] = TOMARGINALAGGR(SN, IND, STATE_I, K, KS, SPACE_BUF, SPACE_SRV, SPACE_VAR)
5%
6% @brief Computes aggregate marginal job counts from state information without phase details
7%
8% This function provides a simplified version of toMarginal that computes
9% aggregate job counts per node and per class, without considering individual
10% service phases. It is more efficient when phase-level detail is not required.
11%
12% @param sn Network structure or Network object
13% @param ind Node index to extract marginal information for
14% @param state_i Global state matrix or vector
15% @param K Vector of population for each class
16% @param Ks Matrix of populations per chain and class
17% @param space_buf Buffer space configuration
18% @param space_srv Service space configuration
19% @param space_var Variable space configuration
20%
21% @return ni Total jobs in node IND (aggregate across all classes)
22% @return nir Jobs per class in node IND [vector: classes]
23
24% Copyright (c) 2012-2026, Imperial College London
25% All rights reserved.
26
27if ~isstruct(sn) % the input can be a Network object too
28 sn=sn.getStruct();
29end
30% ind: node index
31ist = sn.nodeToStation(ind);
32%isf = sn.nodeToStateful(ind);
33R = sn.nclasses;
34if ~sn.isstation(ind) && sn.isstateful(ind) % if stateful node
35 % Strip trailing nvars columns (e.g. RROBIN pointer on a Router) and
36 % keep the buffer portion. If the buffer is empty (the node has no
37 % per-class job buffer at all, e.g. a Router only carrying nvars
38 % bookkeeping), return zeros of the expected (1, R) shape rather than
39 % an empty slice, so callers can index nir(r) for r = 1..R.
40 nvarsSum = sum(sn.nvars(ind,:));
41 bufferLen = size(state_i, 2) - nvarsSum;
42 if bufferLen <= 0
43 ni = 0;
44 nir = zeros(1, R);
45 else
46 ni = sum(state_i(1:bufferLen));
47 nir = state_i(1:bufferLen);
48 if numel(nir) < R
49 nir(end+1:R) = 0;
50 end
51 end
52 return
53end
54
55% PAS/OI stations: the state is the ordered list of class indices (no server split).
56if sn.isstation(ind) && sn.sched(ist) == SchedStrategy.PAS
57 Vp = sum(sn.nvars(ind,:));
58 listcols = state_i(:,1:(end-Vp));
59 nir = zeros(size(state_i,1),R);
60 for r=1:R
61 nir(:,r) = sum(listcols==r,2);
62 end
63 ni = sum(nir,2);
64 return
65end
66
67% Place nodes: compute total jobs per class from state
68if sn.nodetype(ind) == NodeType.Place
69 state_len = size(state_i, 2);
70 % Check if this is queue-based state (FCFS/LCFS/HOL) vs count-based
71 % Queue-based: state contains class indices [1,1,2,1] meaning job arrivals
72 % Count-based: state contains counts per class [n1, n2, ...] or [buf(R), srv]
73 % Detect queue-based: length != R and length != 2*R and all values are valid class indices
74 is_queue_based = state_len ~= R && state_len ~= 2*R;
75 if is_queue_based && state_len > 0
76 % Check all values are valid class indices (1 to R)
77 all_vals = state_i(:);
78 is_queue_based = all(all_vals >= 1 & all_vals <= R);
79 end
80
81 if is_queue_based
82 % Queue-based format: count occurrences of each class
83 nir = zeros(size(state_i,1), R);
84 for r = 1:R
85 nir(:,r) = sum(state_i == r, 2);
86 end
87 elseif nargin >= 4 && ~isempty(K)
88 expected_len = R + sum(K);
89 if state_len == expected_len
90 % State has [buffer, server] format
91 buf_part = state_i(:, 1:R);
92 srv_part = state_i(:, (R+1):end);
93 nir = buf_part;
94 for r = 1:R
95 for k = 1:K(r)
96 nir(:,r) = nir(:,r) + srv_part(:, Ks(r)+k);
97 end
98 end
99 elseif state_len == R
100 % State is just counts per class (simple SIRO format)
101 nir = state_i;
102 else
103 % Unknown format - just take first R columns
104 nir = state_i(:, 1:min(R, state_len));
105 if size(nir, 2) < R
106 nir = [nir, zeros(size(nir,1), R - size(nir,2))];
107 end
108 end
109 else
110 % No K provided - assume state is counts per class
111 nir = state_i(:, 1:min(R, size(state_i, 2)));
112 if size(nir, 2) < R
113 nir = [nir, zeros(size(nir,1), R - size(nir,2))];
114 end
115 end
116 ni = sum(nir,2);
117 return
118end
119
120% Source nodes have infinite population but report queue length 0 (jobs are external)
121if sn.nodetype(ind) == NodeType.Source
122 nir = zeros(size(state_i,1), R);
123 ni = zeros(size(state_i,1), 1);
124 return
125end
126
127if nargin < 4
128 K = sn.phasessz(ist,:);
129end
130 if nargin < 5
131 Ks = sn.phaseshift(ist,:);
132end
133
134if nargin < 8
135 space_var = state_i(:,(end-sum(sn.nvars(ind,:))+1):end); % server state
136 space_srv = state_i(:,(end-sum(K)+1):(end-sum(sn.nvars(ind,:))));
137 space_buf = state_i(:,1:(end-sum(K)));
138end
139
140nir = zeros(size(state_i,1),R); % class-r jobs in service
141for r=1:R
142 for k=1:K(r)
143 nir(:,r) = nir(:,r) + space_srv(:,Ks(r)+k);
144 end
145end
146switch sn.sched(ist)
147 case SchedStrategy.EXT
148 for r=1:R
149 nir(:,r) = Inf;
150 end
151 case SchedStrategy.FCFS
152 for r=1:R
153 nir(:,r) = nir(:,r) + sum(space_buf==r,2); % class-r jobs in station
154 end
155 case SchedStrategy.HOL
156 for r=1:R
157 nir(:,r) = nir(:,r) + sum(space_buf==r,2); % class-r jobs in station
158 end
159 case SchedStrategy.LCFS
160 for r=1:R
161 nir(:,r) = nir(:,r) + sum(space_buf==r,2); % class-r jobs in station
162 end
163 case SchedStrategy.SIRO
164 for r=1:R
165 nir(:,r) = nir(:,r) + space_buf(:,r); % class-r jobs in station
166 end
167 case SchedStrategy.SEPT
168 for r=1:R
169 nir(:,r) = nir(:,r) + space_buf(:,r); % class-r jobs in station
170 end
171 case SchedStrategy.LEPT
172 for r=1:R
173 nir(:,r) = nir(:,r) + space_buf(:,r); % class-r jobs in station
174 end
175 %otherwise % possibly other stateful nodes
176 % no-op
177end
178
179for r=1:R
180 if isnan(sn.rates(ist,r)) && sn.nodetype(ind) ~= NodeType.Place % if disabled
181 nir(:,r) = 0;
182 end
183end
184
185ni = sum(nir,2); % total jobs in station
186end