LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
spaceGenerator.m
1function [SS,SSh,sn,Adj,ST] = spaceGenerator(sn, cutoff, options)
2% SPACEGENERATOR Generate complete state space for queueing network analysis
3%
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)
13%
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.
18%
19% Copyright (c) 2012-2026, Imperial College London
20% All rights reserved.
21N = sn.njobs';
22Np = N;
23
24% Draft SPN support
25Adj = [];
26ST = [];
27
28%%
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.');
31end
32
33if isscalar(cutoff)
34 cutoff = cutoff * ones(sn.nstations, sn.nclasses);
35end
36
37[~, sn, capacityc] = State.spaceGeneratorNodes(sn, cutoff, options);
38
39%%
40isOpenClass = isinf(Np);
41isClosedClass = ~isOpenClass;
42for r=1:sn.nclasses %cut-off open classes to finite capacity
43 if isOpenClass(r)
44 Np(r) = max(capacityc(:,r)); % if replaced by sum stateMarg_i can exceed capacity
45 end
46end
47
48nstatefulp = sn.nstateful - sum(sn.nodetype == NodeType.Source); % M without sources
49
50% see _kb/04-networkstruct.md (initDefault.m/spaceGenerator.m) for rationale
51if isfield(options,'ctmc_max_states') && ~isempty(options.ctmc_max_states)
52 maxStates = options.ctmc_max_states;
53else
54 maxStates = 3e6;
55end
56
57n = pprod(Np);
58chainStationPos=[];
59while n>=0
60 % Cooperative wall-clock budget checkpoint (options.timeout): state-space
61 % enumeration can dominate the solve time, and a partial space would give
62 % silently wrong results, so exceeding the budget must abort the solve.
63 if nargin>=3 && lineTimeoutExceeded(options)
64 line_error(mfilename,'State space generation exceeded the wall-clock time budget (options.timeout=%gs).', options.timeout);
65 end
66 % this is rather inefficient since n ignores chains and thus it can
67 % generate state spaces such as
68 % J =
69 %
70 % 0 0 0 0
71 % 0 0 0 1
72 % 0 0 1 0
73 % 0 1 0 0
74 % 1 0 0 0
75 % 0 0 0 1
76 % 0 0 1 0
77 % 0 1 0 0
78 % 1 0 0 0
79 % 0 0 0 2
80 % 0 0 1 1
81 % 0 0 2 0
82 % 0 1 0 1
83 % 1 0 0 1
84 % 0 1 1 0
85 % 1 0 1 0
86 % 0 2 0 0
87 % 1 1 0 0
88 % 2 0 0 0
89 % that are then in the need for a call to unique
90 if all(isOpenClass) | (Np(isClosedClass) == n(isClosedClass)) %#ok<OR2>
91 chainStationPos = [chainStationPos; State.spaceClosedMultiCS(nstatefulp,n,sn.chains)];
92 if size(chainStationPos,1) > maxStates
93 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);
94 end
95 end
96 n = pprod(n,Np);
97end
98
99chainStationPos = unique(chainStationPos,'rows');
100
101netstates = cell(size(chainStationPos,1), sn.nstateful);
102for j=1:size(chainStationPos,1)
103 % Cooperative wall-clock budget checkpoint: the per-marginal local state
104 % enumeration below (State.fromMarginal per node) dominates for large
105 % buffers, so the budget must also be enforced at this granularity.
106 if nargin>=3 && lineTimeoutExceeded(options)
107 line_error(mfilename,'State space generation exceeded the wall-clock time budget (options.timeout=%gs).', options.timeout);
108 end
109 for ind=1:sn.nnodes
110 if sn.nodetype(ind) == NodeType.Source
111 isf = sn.nodeToStateful(ind);
112 state_i = State.fromMarginal(sn,ind,[]);
113 netstates{j,isf} = State.getHash(sn,ind,state_i);
114 elseif sn.isstation(ind)
115 isf = sn.nodeToStateful(ind);
116 stateMarg_i = chainStationPos(j,(isf-sum(sn.nodetype(1:ind-1) == NodeType.Source)):nstatefulp:end);
117 if any(stateMarg_i > capacityc(ind,:))
118 netstates{j,isf} = State.getHash(sn,ind,[]);
119 else
120 state_i = State.fromMarginal(sn,ind,stateMarg_i);
121 netstates{j,isf} = State.getHash(sn,ind,state_i);
122 end
123 elseif sn.isstateful(ind)
124 isf = sn.nodeToStateful(ind);
125 stateMarg_i = chainStationPos(j,(isf-sum(sn.nodetype(1:ind-1) == NodeType.Source)):nstatefulp:end);
126 state_i = sn.space{isf};
127 if any(stateMarg_i > capacityc(ind,:))
128 netstates{j,isf} = State.getHash(sn,ind,[]);
129 elseif sn.nodetype(ind) == NodeType.Cache
130 % For cache nodes, we need to handle states with jobs in multiple classes
131 % (InitClass, HitClass, MissClass) since cache nodes support class switching
132 state_i = state_i(findrows(state_i(:,1:length(stateMarg_i)),stateMarg_i),:);
133 np = sn.nodeparam{ind};
134 if isfield(np,'retrievalSystemCapacity') && np.retrievalSystemCapacity > 0 ...
135 && isfield(np,'retrievalClasses') && ~isempty(np.retrievalClasses)
136 % see _kb/04-networkstruct.md (initDefault.m/spaceGenerator.m) for rationale
137 rc = np.retrievalClasses;
138 tcc = np.totalCacheCapacity;
139 lvs = length(stateMarg_i); % per-class server presence width
140 rsqi = np.retrievalSystemQueueIndices;
141 itemsInQueue = []; % 1-based item ids currently at a queue
142 validMarginal = true;
143 if ~isempty(rsqi)
144 aks = keys(rsqi);
145 for ak = 1:numel(aks)
146 arrivalClass = aks{ak}; % int32, 0-based arrival class
147 classCol = double(arrivalClass) + 1;
148 qNodes = rsqi(arrivalClass);
149 for qq = 1:numel(qNodes)
150 qIdx = qNodes(qq);
151 qOff = sn.nodeToStateful(qIdx) - sum(sn.nodetype(1:qIdx-1) == NodeType.Source);
152 for item = 1:size(rc,1)
153 if classCol > size(rc,2), continue; end
154 rClass = rc(item, classCol);
155 if rClass < 1, continue; end
156 col = qOff + (rClass-1)*nstatefulp;
157 if col > size(chainStationPos,2) || chainStationPos(j,col) == 0, continue; end
158 % item at a queue: no retrieval job may be at the cache server,
159 % and the item can be at only one queue
160 if stateMarg_i(rClass) > 0 || any(itemsInQueue == item)
161 validMarginal = false; break;
162 end
163 itemsInQueue(end+1) = item; %#ok<AGROW>
164 end
165 if ~validMarginal, break; end
166 end
167 if ~validMarginal, break; end
168 end
169 end
170 if ~validMarginal
171 state_i = zeros(0, size(state_i,2));
172 else
173 keep = false(size(state_i,1),1);
174 for row = 1:size(state_i,1)
175 st = state_i(row,:);
176 validRow = true;
177 itemsInRSstate = [];
178 for col = (lvs+tcc+1):size(st,2)
179 if st(col) == 0, continue; end
180 item = col - (lvs+tcc);
181 for jac = 1:size(rc,2)
182 rClass = rc(item, jac);
183 if rClass < 1, continue; end
184 % item in the bitmap but not at a queue requires a
185 % retrieval job ready to depart/arrive at the cache server
186 if ~any(itemsInQueue == item) && st(rClass) == 0
187 validRow = false; break;
188 end
189 end
190 if ~validRow, break; end
191 itemsInRSstate(end+1) = item; %#ok<AGROW>
192 end
193 if validRow
194 % every item at a queue must be recorded in the bitmap
195 for it = itemsInQueue
196 if ~any(itemsInRSstate == it)
197 validRow = false; break;
198 end
199 end
200 end
201 keep(row) = validRow;
202 end
203 state_i = state_i(keep,:);
204 end
205 end
206 netstates{j,isf} = State.getHash(sn,ind,state_i);
207 elseif sn.nodetype(ind) == NodeType.Transition
208 % Transition states are per-mode, not per-class; include all states
209 netstates{j,isf} = State.getHash(sn,ind,state_i);
210 else
211 state_i = state_i(findrows(state_i(:,1:length(stateMarg_i)),stateMarg_i),:);
212 netstates{j,isf} = State.getHash(sn,ind,state_i);
213 end
214 end
215 end
216end
217
218ctr = 0;
219%SS = sparse([]);
220SS = [];
221SSh = [];
222tochk = 0;
223for j=1:size(chainStationPos,1)
224 % for each network state
225 v = {netstates{j,:}};
226 % cycle over lattice
227 vN = cellfun(@length,v)-1;
228 n = pprod(vN);
229 while n >=0
230 % Cooperative wall-clock budget checkpoint: this cartesian composition
231 % over the per-node state lattice is the combinatorial hot loop, so the
232 % budget is checked here too (amortized every 8192 iterations).
233 tochk = tochk + 1;
234 if tochk >= 1024
235 tochk = 0;
236 if nargin>=3 && lineTimeoutExceeded(options)
237 line_error(mfilename,'State space generation exceeded the wall-clock time budget (options.timeout=%gs).', options.timeout);
238 end
239 end
240 u={}; h={};
241 skip = false;
242 for isf=1:length(n)
243 h{isf} = v{isf}(1+n(isf));
244 if h{isf} < 0
245 skip = true;
246 break
247 end
248 u{isf} = sn.space{isf}(v{isf}(1+n(isf)),:);
249 end
250 if skip == false
251 ctr = ctr + 1; % do not move
252 if ctr > maxStates
253 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);
254 end
255 SS(ctr,:)=cell2mat(u);
256 SSh(ctr,:)=cell2mat(h);
257 end
258 n = pprod(n,vN);
259 end
260end
261[SS,IA] = unique(SS,'rows');
262SSh = SSh(IA,:);
263end