1function [ni, nir, sir, kir] = toMarginal(sn, ind, state_i, phasesz, phaseshift, space_buf, space_srv, space_var) %#ok<INUSD>
2% TOMARGINAL Compute marginal distributions
for a specific node
4% [NI, NIR, SIR, KIR] = TOMARGINAL(SN, IND, STATE_I, PHASESZ, PHASESHIFT, SPACE_BUF, SPACE_SRV, SPACE_VAR)
6% @brief Extracts marginal job statistics from global state information
for a specific node
8% This function processes
the global network state to compute marginal
9% distributions and job counts
for a specific node, considering different
10% scheduling strategies, service phases, and node types.
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 phasesz Vector of phase sizes
for each class
16% @param phaseshift Phase shift parameters
for state extraction
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
22% @
return nir Total jobs per
class in node IND [matrix: states x classes]
23% @return sir Total jobs in service per class in node IND [matrix: states x classes]
24% @return kir Total jobs in service per class and per phase in node IND [3D array: states x classes x phases]
26% Copyright (c) 2012-2026, Imperial College London
29if ~isstruct(sn) %
the input can be a Network object too
33% Join stations on FJ-augmented structs: plain per-class
count state,
34% no buffer/phase split and no service (jobs wait for synchronization)
35if isfield(sn,'isfjaugmented') && sn.isfjaugmented && sn.nodetype(ind) == NodeType.Join
38 state_i = sn.state{sn.nodeToStateful(ind)};
40 nir = state_i(:,(end-R+1):end);
41 sir = zeros(size(nir));
42 kir = zeros(size(state_i,1),R,1);
48if ~sn.isstation(ind) && sn.isstateful(ind) % if stateful node
49 if sn.nodetype(ind) == NodeType.Transition
50 R = sn.nodeparam{ind}.nmodes;
52 % Initialize phasesz/phaseshift for Transition nodes from firingphases
53 phasesz = sn.nodeparam{ind}.firingphases;
54 if any(isnan(phasesz))
55 phasesz = zeros(1, R);
57 if iscell(sn.nodeparam{ind}.firingproc) && ~isempty(sn.nodeparam{ind}.firingproc{m})
58 phasesz(m) = size(sn.nodeparam{ind}.firingproc{m}{1}, 1);
64 phaseshift = [0,cumsum(phasesz(1:end-1))];
68 space_buf = state_i(:,1:nmodes);
69 space_srv = state_i(:,(nmodes+1):(nmodes+sum(phasesz)));
70 space_var = state_i(:,(nmodes+sum(phasesz)+1):end);
72 sir = zeros(size(state_i,1),R); % class-r jobs in service
73 kir = zeros(size(state_i,1),R,max(phasesz)); % class-r jobs in service in phase k
76 kir(:,r,k) = space_srv(:,phaseshift(r)+k);
77 sir(:,r) = sir(:,r) + kir(:,r,k);
83 ni = sum(state_i(1:(end-sum(sn.nvars(ind,:)))));
84 nir = state_i(1:(end-sum(sn.nvars(ind,:))));
85 sir = nir; % jobs in service
86 kir = sir; % jobs per phase
93ist = sn.nodeToStation(ind);
94isf = sn.nodeToStateful(ind);
97 state_i = sn.state{isf};
101 phasesz = sn.phasessz(ist,:);
102 phaseshift = sn.phaseshift(ist,:);
105if sn.sched(ist) == SchedStrategy.PAS
106 % PAS/OI: the state is the ordered list of class indices (no server split).
107 Vp = sum(sn.nvars(ind,:));
108 listcols = state_i(:,1:(end-Vp));
109 nir = zeros(size(state_i,1),R);
111 nir(:,r) = sum(listcols==r,2);
113 % Jobs in service = positions receiving a nonzero rate increment
114 % Delta_mu(c1..cp) > 0 (the rest wait); utilization derives from this.
115 muFun = sn.nodeparam{ind}.svcRateFun;
116 sir = zeros(size(state_i,1),R);
118 for row=1:size(listcols,1)
119 c = listcols(row, listcols(row,:)>0);
122 muCur = muFun(c(1:p));
123 if muCur - muPrev > 0
124 sir(row,c(p)) = sir(row,c(p)) + 1;
132 kir = zeros(size(state_i,1),R,max(phasesz));
138isExponential = false;
140 isExponential = true;
144 space_var = state_i(:,(end-sum(sn.nvars(ind,:))+1):end); % local variables
145 space_srv = state_i(:,(end-sum(phasesz)-sum(sn.nvars(ind,:))+1):(end-sum(sn.nvars(ind,:)))); % server state
146 space_buf = state_i(:,1:(end-sum(phasesz)-sum(sn.nvars(ind,:)))); % buffer state
153 nir = zeros(size(state_i,1),R);
154 sir = zeros(size(state_i,1),R); % class-r jobs in service
155 kir = zeros(size(state_i,1),R,max(phasesz)); % class-r jobs in service in phase k
158 kir(:,r,k) = space_srv(:,phaseshift(r)+k);
159 sir(:,r) = sir(:,r) + kir(:,r,k);
165 case SchedStrategy.INF
167 nir(:,r) = sir(:,r); % class-r jobs in station
169 case {SchedStrategy.PS, SchedStrategy.PSPRIO}
171 nir(:,r) = sir(:,r) ; % class-r jobs in station
173 case SchedStrategy.EXT
177 case {SchedStrategy.FCFS, SchedStrategy.FCFSPRIO, SchedStrategy.HOL}
179 nir(:,r) = sir(:,r) + sum(space_buf==r,2); % class-r jobs in station
181 case {SchedStrategy.DPS, SchedStrategy.DPSPRIO}
183 nir(:,r) = sir(:,r) ; % class-r jobs in station
185 case {SchedStrategy.GPS, SchedStrategy.GPSPRIO}
187 nir(:,r) = sir(:,r) ; % class-r jobs in station
189 case {SchedStrategy.LCFS, SchedStrategy.LCFSPRIO}
191 nir(:,r) = sir(:,r) + sum(space_buf==r,2); % class-r jobs in station
193 case {SchedStrategy.FCFSPI, SchedStrategy.FCFSPIPRIO, SchedStrategy.FCFSPR, SchedStrategy.FCFSPRPRIO, SchedStrategy.LCFSPI, SchedStrategy.LCFSPIPRIO, SchedStrategy.LCFSPR, SchedStrategy.LCFSPRPRIO}
194 if length(space_buf)>1
195 space_buf = space_buf(1:2:end);
196 %space_bufphase = space_buf(2:2:end);
198 nir(:,r) = sir(:,r) + sum(space_buf==r,2); % class-r jobs in station
203 case {SchedStrategy.SIRO, SchedStrategy.POLLING}
205 nir(:,r) = sir(:,r) + space_buf(:,r); % class-r jobs in station
207 case SchedStrategy.SEPT
209 nir(:,r) = sir(:,r) + space_buf(:,r); % class-r jobs in station
211 case SchedStrategy.LEPT
213 nir(:,r) = sir(:,r) + space_buf(:,r); % class-r jobs in station
215 case SchedStrategy.SRPT
217 nir(:,r) = sir(:,r) + space_buf(:,r); % class-r jobs in station
219 case SchedStrategy.SRPTPRIO
221 nir(:,r) = sir(:,r) + space_buf(:,r); % class-r jobs in station
223 otherwise % possibly other stateful nodes
225 nir(:,r) = sir(:,r) ; % class-r jobs in station
229if sn.nodetype(ind) == NodeType.Place
230 % A Place is a token container: the [buffer, server] split in its state
231 % encoding is an artifact (a transition FIRE relocates the surviving
232 % tokens from the server slot into the buffer slot). For the INF-family
233 % scheduling used by Places the switch above set nir = sir (server slot
234 % only), so fold the buffer slot back in. This makes the marginal token
235 % count agree with State.toMarginalAggr (buf + srv), which drives the
236 % transition enabling degree; without it the QLen measurement collapses
237 % to the oscillating server slot while the dynamics stay correct.
239 case {SchedStrategy.INF, SchedStrategy.PS, SchedStrategy.PSPRIO, ...
240 SchedStrategy.DPS, SchedStrategy.DPSPRIO, ...
241 SchedStrategy.GPS, SchedStrategy.GPSPRIO, SchedStrategy.LPS}
242 if ~isempty(space_buf) && size(space_buf,2) >= R
244 nir(:,r) = sir(:,r) + space_buf(:,r);
248 % set all service and phase data to 0
250 if isnan(sn.rates(ist,r)) % if disabled station
259 if isnan(sn.rates(ist,r)) % if disabled station
269ni = sum(nir,2); % total jobs in station