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
4% [NI, NIR] = TOMARGINALAGGR(SN, IND, STATE_I, K, KS, SPACE_BUF, SPACE_SRV, SPACE_VAR)
6% @brief Computes aggregate marginal job counts from state information without phase details
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.
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
21% @
return ni Total jobs in node IND (aggregate across all
classes)
22% @
return nir Jobs per
class in node IND [vector:
classes]
24% Copyright (c) 2012-2026, Imperial College London
27if ~isstruct(sn) % the input can be a Network object too
31ist = sn.nodeToStation(ind);
32%isf = sn.nodeToStateful(ind);
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;
46 ni = sum(state_i(1:bufferLen));
47 nir = state_i(1:bufferLen);
55% Place nodes: compute total jobs per class from state
56if sn.nodetype(ind) == NodeType.Place
57 state_len = size(state_i, 2);
58 % Check if this is queue-based state (FCFS/LCFS/HOL) vs count-based
59 % Queue-based: state contains class indices [1,1,2,1] meaning job arrivals
60 % Count-based: state contains counts per class [n1, n2, ...] or [buf(R), srv]
61 % Detect queue-based: length != R and length != 2*R and all values are valid class indices
62 is_queue_based = state_len ~= R && state_len ~= 2*R;
63 if is_queue_based && state_len > 0
64 % Check all values are valid class indices (1 to R)
65 all_vals = state_i(:);
66 is_queue_based = all(all_vals >= 1 & all_vals <= R);
70 % Queue-based format: count occurrences of each class
71 nir = zeros(size(state_i,1), R);
73 nir(:,r) = sum(state_i == r, 2);
75 elseif nargin >= 4 && ~isempty(K)
76 expected_len = R + sum(K);
77 if state_len == expected_len
78 % State has [buffer, server] format
79 buf_part = state_i(:, 1:R);
80 srv_part = state_i(:, (R+1):end);
84 nir(:,r) = nir(:,r) + srv_part(:, Ks(r)+k);
88 % State
is just counts per class (simple SIRO format)
91 % Unknown format - just take first R columns
92 nir = state_i(:, 1:min(R, state_len));
94 nir = [nir, zeros(size(nir,1), R - size(nir,2))];
98 % No K provided - assume state is counts per class
99 nir = state_i(:, 1:min(R, size(state_i, 2)));
101 nir = [nir, zeros(size(nir,1), R - size(nir,2))];
108% Source nodes have infinite population but report queue length 0 (jobs are external)
109if sn.nodetype(ind) == NodeType.Source
110 nir = zeros(size(state_i,1), R);
111 ni = zeros(size(state_i,1), 1);
116 K = sn.phasessz(ist,:);
119 Ks = sn.phaseshift(ist,:);
123 space_var = state_i(:,(end-sum(sn.nvars(ind,:))+1):end); % server state
124 space_srv = state_i(:,(end-sum(K)+1):(end-sum(sn.nvars(ind,:))));
125 space_buf = state_i(:,1:(end-sum(K)));
128nir = zeros(size(state_i,1),R); % class-r jobs in service
131 nir(:,r) = nir(:,r) + space_srv(:,Ks(r)+k);
135 case SchedStrategy.EXT
139 case SchedStrategy.FCFS
141 nir(:,r) = nir(:,r) + sum(space_buf==r,2); % class-r jobs in station
143 case SchedStrategy.HOL
145 nir(:,r) = nir(:,r) + sum(space_buf==r,2); % class-r jobs in station
147 case SchedStrategy.LCFS
149 nir(:,r) = nir(:,r) + sum(space_buf==r,2); % class-r jobs in station
151 case SchedStrategy.SIRO
153 nir(:,r) = nir(:,r) + space_buf(:,r); % class-r jobs in station
155 case SchedStrategy.SEPT
157 nir(:,r) = nir(:,r) + space_buf(:,r); % class-r jobs in station
159 case SchedStrategy.LEPT
161 nir(:,r) = nir(:,r) + space_buf(:,r); % class-r jobs in station
163 %otherwise % possibly other stateful nodes
168 if isnan(sn.rates(ist,r)) && sn.nodetype(ind) ~= NodeType.Place % if disabled
173ni = sum(nir,2); % total jobs in station