1function [SS,SSh,sn,Adj,ST] = spaceGenerator(sn, cutoff, options)
2% SPACEGENERATOR Generate complete state space
for queueing network analysis
4% @brief Creates
the complete state space including all possible network states
5% @param sn Network structure representing
the queueing network
6% @param cutoff Population cutoff limits
for open classes (scalar or matrix)
7% @param options Optional configuration structure
for state generation
8% @
return SS Complete state space matrix
9% @
return SSh Hashed state space
for efficient lookups
10% @
return sn Updated network structure with state space information
11% @
return Adj Adjacency matrix
for state transitions (SPN support)
12% @
return ST State transition information (SPN support)
14% The state space generator creates all possible states
for the queueing
15% network, including those not reachable from
the initial state. This
is
16% essential
for steady-state analysis and performance metric computation.
17% For open classes, a cutoff parameter limits
the maximum population.
19% Copyright (c) 2012-2026, Imperial College London
29if ~exist('cutoff
','var
') && any(isinf(Np)) % if the model has open classes
30 line_error(mfilename,'Unspecified cutoff
for open classes in state space generator.
');
34 cutoff = cutoff * ones(sn.nstations, sn.nclasses);
37[~, sn, capacityc] = State.spaceGeneratorNodes(sn, cutoff, options);
40isOpenClass = isinf(Np);
41isClosedClass = ~isOpenClass;
42for r=1:sn.nclasses %cut-off open classes to finite capacity
44 Np(r) = max(capacityc(:,r)); % if replaced by sum stateMarg_i can exceed capacity
48nstatefulp = sn.nstateful - sum(sn.nodetype == NodeType.Source); % M without sources
50% Memory guard (analog of Python options.ctmc_max_states, default 3e6): an
51% unbounded state space (e.g. finite-capacity regions whose constraints are
52% applied only after composition) can enumerate to tens of GB and OOM-kill the
53% process. Abort with a recoverable error so an enclosing solver chain
54% (json_solve MVA->MAM->CTMC->LDES) falls through instead of crashing.
55if isfield(options,'ctmc_max_states
') && ~isempty(options.ctmc_max_states)
56 maxStates = options.ctmc_max_states;
64 % Cooperative wall-clock budget checkpoint (options.timeout): state-space
65 % enumeration can dominate the solve time, and a partial space would give
66 % silently wrong results, so exceeding the budget must abort the solve.
67 if nargin>=3 && lineTimeoutExceeded(options)
68 line_error(mfilename,'State space generation exceeded
the wall-clock time budget (options.timeout=%gs).
', options.timeout);
70 % this is rather inefficient since n ignores chains and thus it can
71 % generate state spaces such as
93 % that are then in the need for a call to unique
94 if all(isOpenClass) | (Np(isClosedClass) == n(isClosedClass)) %#ok<OR2>
95 chainStationPos = [chainStationPos; State.spaceClosedMultiCS(nstatefulp,n,sn.chains)];
96 if size(chainStationPos,1) > maxStates
97 line_error(mfilename,'State space too large: population lattice exceeds ctmc_max_states=%g. Increase options.ctmc_max_states or use a different solver.
', maxStates);
103chainStationPos = unique(chainStationPos,'rows
');
105netstates = cell(size(chainStationPos,1), sn.nstateful);
106for j=1:size(chainStationPos,1)
107 % Cooperative wall-clock budget checkpoint: the per-marginal local state
108 % enumeration below (State.fromMarginal per node) dominates for large
109 % buffers, so the budget must also be enforced at this granularity.
110 if nargin>=3 && lineTimeoutExceeded(options)
111 line_error(mfilename,'State space generation exceeded
the wall-clock time budget (options.timeout=%gs).
', options.timeout);
114 if sn.nodetype(ind) == NodeType.Source
115 isf = sn.nodeToStateful(ind);
116 state_i = State.fromMarginal(sn,ind,[]);
117 netstates{j,isf} = State.getHash(sn,ind,state_i);
118 elseif sn.isstation(ind)
119 isf = sn.nodeToStateful(ind);
120 stateMarg_i = chainStationPos(j,(isf-sum(sn.nodetype(1:ind-1) == NodeType.Source)):nstatefulp:end);
121 if any(stateMarg_i > capacityc(ind,:))
122 netstates{j,isf} = State.getHash(sn,ind,[]);
124 state_i = State.fromMarginal(sn,ind,stateMarg_i);
125 netstates{j,isf} = State.getHash(sn,ind,state_i);
127 elseif sn.isstateful(ind)
128 isf = sn.nodeToStateful(ind);
129 stateMarg_i = chainStationPos(j,(isf-sum(sn.nodetype(1:ind-1) == NodeType.Source)):nstatefulp:end);
130 state_i = sn.space{isf};
131 if any(stateMarg_i > capacityc(ind,:))
132 netstates{j,isf} = State.getHash(sn,ind,[]);
133 elseif sn.nodetype(ind) == NodeType.Cache
134 % For cache nodes, we need to handle states with jobs in multiple classes
135 % (InitClass, HitClass, MissClass) since cache nodes support class switching
136 state_i = state_i(findrows(state_i(:,1:length(stateMarg_i)),stateMarg_i),:);
137 np = sn.nodeparam{ind};
138 if isfield(np,'retrievalSystemCapacity
') && np.retrievalSystemCapacity > 0 ...
139 && isfield(np,'retrievalClasses
') && ~isempty(np.retrievalClasses)
140 % Cross-node (cache <-> queue) consistency: an item recorded in the
141 % cache occupancy bitmap must correspond to a retrieval job that is
142 % either at a retrieval queue or ready to depart/arrive at the cache
143 % server, and an item being retrieved at a queue cannot also have a
144 % retrieval job sitting in the cache server. Enforced at the global
145 % composition (the local cache space alone cannot see the queues).
146 rc = np.retrievalClasses;
147 tcc = np.totalCacheCapacity;
148 lvs = length(stateMarg_i); % per-class server presence width
149 rsqi = np.retrievalSystemQueueIndices;
150 itemsInQueue = []; % 1-based item ids currently at a queue
151 validMarginal = true;
154 for ak = 1:numel(aks)
155 arrivalClass = aks{ak}; % int32, 0-based arrival class
156 classCol = double(arrivalClass) + 1;
157 qNodes = rsqi(arrivalClass);
158 for qq = 1:numel(qNodes)
160 qOff = sn.nodeToStateful(qIdx) - sum(sn.nodetype(1:qIdx-1) == NodeType.Source);
161 for item = 1:size(rc,1)
162 if classCol > size(rc,2), continue; end
163 rClass = rc(item, classCol);
164 if rClass < 1, continue; end
165 col = qOff + (rClass-1)*nstatefulp;
166 if col > size(chainStationPos,2) || chainStationPos(j,col) == 0, continue; end
167 % item at a queue: no retrieval job may be at the cache server,
168 % and the item can be at only one queue
169 if stateMarg_i(rClass) > 0 || any(itemsInQueue == item)
170 validMarginal = false; break;
172 itemsInQueue(end+1) = item; %#ok<AGROW>
174 if ~validMarginal, break; end
176 if ~validMarginal, break; end
180 state_i = zeros(0, size(state_i,2));
182 keep = false(size(state_i,1),1);
183 for row = 1:size(state_i,1)
187 for col = (lvs+tcc+1):size(st,2)
188 if st(col) == 0, continue; end
189 item = col - (lvs+tcc);
190 for jac = 1:size(rc,2)
191 rClass = rc(item, jac);
192 if rClass < 1, continue; end
193 % item in the bitmap but not at a queue requires a
194 % retrieval job ready to depart/arrive at the cache server
195 if ~any(itemsInQueue == item) && st(rClass) == 0
196 validRow = false; break;
199 if ~validRow, break; end
200 itemsInRSstate(end+1) = item; %#ok<AGROW>
203 % every item at a queue must be recorded in the bitmap
204 for it = itemsInQueue
205 if ~any(itemsInRSstate == it)
206 validRow = false; break;
210 keep(row) = validRow;
212 state_i = state_i(keep,:);
215 netstates{j,isf} = State.getHash(sn,ind,state_i);
216 elseif sn.nodetype(ind) == NodeType.Transition
217 % Transition states are per-mode, not per-class; include all states
218 netstates{j,isf} = State.getHash(sn,ind,state_i);
220 state_i = state_i(findrows(state_i(:,1:length(stateMarg_i)),stateMarg_i),:);
221 netstates{j,isf} = State.getHash(sn,ind,state_i);
232for j=1:size(chainStationPos,1)
233 % for each network state
234 v = {netstates{j,:}};
236 vN = cellfun(@length,v)-1;
239 % Cooperative wall-clock budget checkpoint: this cartesian composition
240 % over the per-node state lattice is the combinatorial hot loop, so the
241 % budget is checked here too (amortized every 8192 iterations).
245 if nargin>=3 && lineTimeoutExceeded(options)
246 line_error(mfilename,'State space generation exceeded
the wall-clock time budget (options.timeout=%gs).
', options.timeout);
252 h{isf} = v{isf}(1+n(isf));
257 u{isf} = sn.space{isf}(v{isf}(1+n(isf)),:);
260 ctr = ctr + 1; % do not move
262 line_error(mfilename,'State space too large: composed states exceed ctmc_max_states=%g. Increase options.ctmc_max_states or use a different solver.
', maxStates);
264 SS(ctr,:)=cell2mat(u);
265 SSh(ctr,:)=cell2mat(h);
270[SS,IA] = unique(SS,'rows
');