LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
solver_ssa_nrm_space.m
1function [pi, outspace, depRates, sn] = solver_ssa_nrm_space(sn, options)
2% SOLVER_SSA_NRM_SPACE Steady‑state analysis via the Next‑Reaction Method (SSA)
3%
4% [PI, SSQ, ARVRATES, DEPRATES, SN] = SOLVER_SSA_NRM_SPACE(SN, OPTIONS)
5% runs a stochastic simulation of the queueing network described in SN
6% for OPTIONS.samples reaction firings using Gibson & Bruck's
7% Next‑Reaction Method. During the run it:
8% • observes every distinct global state visited and the time spent in it;
9% • accumulates the per‑state propensities of **all** enabled reactions,
10% including *self‑loops* (service completions routed back to the same
11% queue).
12%
13% Outputs
14% PI – 1×S vector of empirical steady‑state probabilities
15% (sojourn‑time fractions) for the S unique states;
16% SSQ – S×(M·R) matrix listing those states row‑by‑row in the
17% flattened (station, class) order;
18% DEPRATES – S×(M·R) matrix of total departure rates from each queue
19% in the corresponding state;
20% SN – (Possibly updated) network structure.
21%
22% See also NEXT_REACTION_METHOD.
23
24% ---------------------------------------------------------------------
25% Parameters & shorthands
26% ---------------------------------------------------------------------
27samples = options.samples;
28R = sn.nclasses;
29I = sn.nnodes;
30state = sn.state;
31% ---------------------------------------------------------------------
32% Stoichiometry & reaction mapping (self‑loops included) ----------------
33% ---------------------------------------------------------------------
34S = zeros(0, I*R); % will transpose at the end
35fromIdx = [];
36toIdx = [];
37fromIR = [];
38
39% this is currently M^2*R^2, it can be lowered to M*R decoupling the
40% routing
41k = 0;
42for ind = 1:I
43 for r = 1:R
44 k = k + 1;
45 fromIR(k,:) = [ind, r];
46 fromIdx(k) = (ind-1)*R + r;
47 probIR{k} = [];
48 toIdx{k} = [];
49 Srow = zeros(1, I*R); % build stoichiometry row
50 if sn.isslc(r)
51 Srow(fromIdx(k)) = -Inf;
52 else
53 Srow(fromIdx(k)) = -1;
54 for jnd = 1:I
55 for s = 1:R
56 if sn.rtnodes((ind-1)*R+r, (jnd-1)*R+s) > 0
57 toIdx{k}(end+1) = (jnd-1)*R + s;
58 p = sn.rtnodes((ind-1)*R+r, (jnd-1)*R+s);
59 probIR{k}(end+1) = p;
60 Srow((jnd-1)*R + s) = Srow((jnd-1)*R + s) + p;
61 end
62 end
63 end
64 end
65 S(k,:) = Srow;
66 end
67end
68S = S.'; % states × reactions
69
70% ---------------------------------------------------------------------
71% Initial state vector --------------------------------------------------
72% ---------------------------------------------------------------------
73nvec0 = zeros(I*R,1); % initial state (aggregate state)
74bufferedSched = [SchedStrategy.FCFS, SchedStrategy.LCFS];
75buffers0 = cell(I,1); % per-node ordered buffer of waiting job classes (FCFS/LCFS)
76for ind=1:I
77 buffers0{ind} = [];
78end
79for ind=1:I
80 if sn.isstateful(ind)
81 state_i = state{sn.nodeToStateful(ind)};
82 [~,nir] = State.toMarginalAggr(sn, ind, state_i);
83 for r = 1:R
84 if isinf(nir(r))
85 if sn.nodetype(ind) == NodeType.Source
86 nir(r) = 1;
87 else
88 line_error(mfilename, 'Infinite population error.');
89 end
90 end
91 nvec0((ind-1)*R + r,1) = nir(r);
92 end
93
94 % Populate buffers for buffered nodes from the raw state vector
95 % (only stations have FCFS/LCFS scheduling; skip non-station
96 % stateful nodes such as RROBIN dispatchers/Routers and Caches)
97 ist = sn.nodeToStation(ind);
98 if ist >= 1 && any(sn.sched(ist) == bufferedSched)
99 sumK = sum(sn.phasessz(ist,:));
100 sumNvars = sum(sn.nvars(ind,:));
101 bufCols = size(state_i,2) - sumK - sumNvars;
102 for pos = 1:bufCols
103 classId = state_i(1,pos);
104 if classId >= 1 && classId <= R
105 buffers0{ind}(end+1) = classId; % addLast
106 end
107 % classId == 0 means empty position, skip
108 end
109 end
110 end
111end
112
113mi = zeros(I,1);
114rates = zeros(I,R);
115for ind=1:I
116 if sn.isstation(ind)
117 for r=1:R
118 ist = sn.nodeToStation(ind);
119 muir = sn.rates(ist,r);
120 if ~isnan(muir)
121 rates(ind,r) = muir;
122 end
123 mi(ind,1) = sn.nservers(ist);
124 end
125 else
126 for r=1:R
127 rates(ind,r) = GlobalConstants.Immediate;
128 mi(ind,1) = GlobalConstants.MaxInt;
129 end
130 end
131 mi(isinf(mi)) = GlobalConstants.MaxInt;
132end
133
134% Propensity function ---------------------------------------------------
135epstol = GlobalConstants.Zero;
136a = {};
137for j=1:length(fromIdx)
138 if sn.isstation(fromIR(j,1))
139 switch sn.sched(sn.nodeToStation(fromIR(j,1)))
140 case SchedStrategy.EXT
141 a{j} = @(X, bufs) rates(fromIR(j,1), fromIR(j,2));
142 case SchedStrategy.INF
143 a{j} = @(X, bufs) rates(fromIR(j,1), fromIR(j,2)) * X(fromIdx(j));
144 case SchedStrategy.PS
145 if R == 1 % single class
146 a{j} = @(X, bufs) rates(fromIR(j,1), fromIR(j,2)) * min( mi(fromIR(j,1)), X(fromIdx(j)));
147 else
148 a{j} = @(X, bufs) rates(fromIR(j,1), fromIR(j,2)) * ( X(fromIdx(j)) ./ ...
149 (epstol+sum( X(((fromIR(j,1)-1)*R + 1):((fromIR(j,1)-1)*R + R)) ) )) * ...
150 min( mi(fromIR(j,1)), ...
151 (epstol+sum( X(((fromIR(j,1)-1)*R + 1):((fromIR(j,1)-1)*R + R)) ) ));
152 end
153 case {SchedStrategy.FCFS, SchedStrategy.LCFS}
154 % Rate proportional to the jobs actually being served, i.e. the
155 % class-r population minus the class-r jobs waiting in buffer.
156 a{j} = @(X, bufs) rates(fromIR(j,1), fromIR(j,2)) * ...
157 max(0, X(fromIdx(j)) - sum(bufs{fromIR(j,1)} == fromIR(j,2)));
158 end
159 else
160 a{j} = @(X, bufs) rates(fromIR(j,1), fromIR(j,2)) * min(1, X(fromIdx(j)));
161 end
162end
163
164% Propensity functions dependencies -----------------------------------
165D = cell(1,size(S,2));
166for k=1:size(D,2)
167 J = find(S(:,k))'; % set of state variables affected by reaction k
168 vecd = [];
169 for j=1:length(J)
170 % (ind-1)*R + r
171 pos = J(j);
172 r = mod(pos-1, R) + 1;
173 ind = ((pos-r)/R) + 1;
174 vecd(end+1:end+R) = ((ind-1)*R + 1) : (ind*R);
175 end
176 % vecd now contains all state variables affected by the firing of
177 % reaction k. We now find the propensity functions that depend
178 % on those variables
179 if ~isempty(vecd)
180 vecd = unique(vecd);
181 vecs = [];
182 for j=1:length(vecd)
183 vecs = [vecs,find(S(vecd(j),:)<0)];
184 end
185 D{k} = unique(vecs);
186 else
187 D{k} = [];
188 end
189end
190
191% Having accounted for them in D, we can now remove self-loops markings
192S(isinf(S))=0;
193% ---------------------------------------------------------------------
194% Run SSA/NRM -----------------------------------------------------------
195% ---------------------------------------------------------------------
196if false %snIsClosedModel(sn)
197 % mixed-radix hashing
198 reactCache = containers.Map('KeyType','uint64','ValueType','any');
199 njobs = sn.njobs;
200 mixedradix = [cumprod(repmat(1+njobs,1,I))];
201 mixedradix = [1,mixedradix(1:end-1)];
202 hashfun = @(v, bufs) uint64(mixedradix*v(:));
203else
204 % buffer size unbounded so use string; the key combines the aggregate
205 % state vector with the ordered contents of every node buffer, so that
206 % FCFS/LCFS states differing only in queueing order remain distinct.
207 reactCache = containers.Map('KeyType','char','ValueType','any');
208 hashfun = @(v, bufs) [mat2str(v(:)'), '|', bufferHashAll(bufs)];
209end
210[t, nvecsim, bufferStates, ~, ~] = next_reaction_method(S, D, a, nvec0, buffers0, samples, options, reactCache, hashfun, fromIR, mi, R, sn);
211
212% ---------------------------------------------------------------------
213% Empirical state probabilities ----------------------------------------
214% ---------------------------------------------------------------------
215dt = diff(t);
216% Find unique states keyed on both the aggregate state and the buffers
217numIntervals = size(nvecsim,2) - 1;
218stateKeys = cell(numIntervals,1);
219for i = 1:numIntervals
220 stateKeys{i} = hashfun(nvecsim(:,i), bufferStates{i});
221end
222[~, ia, ic] = unique(stateKeys, 'stable');
223outspace = nvecsim(:, ia).';
224outspaceBuffers = bufferStates(ia);
225timeAccum = accumarray(ic, dt(:));
226pi = timeAccum / sum(timeAccum);
227
228% ---------------------------------------------------------------------
229% Per‑state arrival / departure rates (self‑loops counted) -------------
230% ---------------------------------------------------------------------
231numStates = size(outspace,1);
232depRates = zeros(numStates, I*R);
233
234for st = 1:numStates
235 a_state = reactCache(hashfun(outspace(st,:)', outspaceBuffers{st}));
236 for j = 1:length(fromIdx)
237 depRates(st, fromIdx(j)) = depRates(st, fromIdx(j)) + a_state(j);
238 end
239end
240end % solver_ssa_nrm_space
241
242% ======================================================================
243% Next-Reaction Method core --------------------------------------------
244% ======================================================================
245function [t, nvec, bufferStates, kfires, rfires] = next_reaction_method(S, D, a, nvec0, buffers0, samples, options, reactcache, hashfun, fromIR, mi, R, sn)
246numReactions = size(S,2);
247rand_pool_size = 1e7;
248buffers = buffers0; % working copy of the per-node ordered buffers
249
250% when a reaction fires, this matrix helps selecting the probability that a
251% particular routing or phase is selected as a result ------------------
252P = S; P(P<0)=P(P<0)+1';
253fromIdx = cell(numReactions,1);
254toIdx = cell(numReactions,1);
255cdfVec = cell(numReactions,1);
256for r=1:numReactions
257 nnzP(r) = nnz(P(:,r));
258 if nnzP(r)>1
259 fromIdx{r} = find(S(:,r)<0);
260 toIdx{r} = find(P(:,r));
261 cdfVec{r} = cumsum(P(toIdx{r},r));
262 end
263end
264
265% initialise Gillespie clocks ------------------------------------------
266t = 0;
267for k=1:size(S,2)
268 Ak(k) = a{k}(nvec0, buffers);
269end
270nvec = nvec0;
271key = hashfun(nvec, buffers);
272reactcache(key) = Ak; % cache first state's propensities
273Pk = -log(rand(1,numReactions));
274Tk = zeros(1,numReactions);
275
276tau = (Pk - Tk) ./ Ak;
277
278% logs -----------------------------------------------------------------
279tout = zeros(samples,1);
280nvecout = zeros(length(nvec0),samples);
281bufferStates = cell(1, samples+1);
282bufferStates{1} = buffers; % buffers in the initial state
283kfires = zeros(samples,1);
284rfires = zeros(samples,1);
285n = 1;
286while n <= samples
287 [dt, kfire] = min(tau);
288 kfires(n) = kfire;
289 if isinf(dt), line_error(mfilename,'Deadlock. Quitting nrm method.'); end
290
291 t = t + dt;
292
293 % update aggregate state
294 destPos = [];
295 if nnzP(kfire)>1
296 r = 1+find(rand>=cdfVec{kfire},1);
297 if isempty(r)
298 r = 1;
299 end
300 rfires(n) = r;
301 nvec(fromIdx{kfire}) = nvec(fromIdx{kfire}) - 1;
302 nvec(toIdx{kfire}(r)) = nvec(toIdx{kfire}(r)) + 1;
303 destPos = toIdx{kfire}(r);
304 else
305 nvec = nvec + S(:,kfire); % zero change for self-loops
306 dpos = find(S(:,kfire) > 0); % deterministic destination (single move)
307 if ~isempty(dpos)
308 destPos = dpos(1);
309 end
310 end
311
312 % maintain FCFS/LCFS buffers given the source/destination of this firing
313 buffers = updateBuffers(kfire, nvec, buffers, fromIR, destPos, mi, R, sn);
314
315 Tk = Tk + Ak * dt;
316
317 % update rates for all reactions dependent on the last fired reaction
318 for k=D{kfire}
319 Ak(k) = a{k}(nvec, buffers);
320 end
321
322 key = hashfun(nvec, buffers);
323 if ~isKey(reactcache,key)
324 reactcache(key) = Ak; % store propensities of new state
325 end
326
327 % maintain random number pool
328 n_mod = mod(n,rand_pool_size);
329 if n_mod == 1
330 rand_pool = rand(1+min(rand_pool_size, samples-n),1);
331 end
332
333 % update clocks
334 Pk(kfire) = Pk(kfire) - log(rand_pool(n_mod));
335 tau = (Pk - Tk) ./ Ak;
336 tau(Ak==0) = inf;
337
338 % update measures
339 tout(n) = t;
340 nvecout(:,n) = nvec;
341 bufferStates{n+1} = buffers;
342
343 % do not count immediate events
344 n = n + 1;
345 print_progress(options, n);
346end
347% Print newline after progress counter
348if isfield(options,'verbose') && options.verbose
349 line_printf('\n');
350end
351
352t = [0; tout];
353nvec = [nvec0, nvecout];
354
355 function print_progress(opt, samples_collected)
356 if ~isfield(opt,'verbose') || ~opt.verbose, return; end
357 if samples_collected == 1e3
358 line_printf('\nSSA samples: %8d', samples_collected);
359 elseif opt.verbose == 2
360 if samples_collected == 0
361 line_printf('\nSSA samples: %9d', samples_collected);
362 else
363 line_printf('\b\b\b\b\b\b\b\b\b%9d', samples_collected);
364 end
365 elseif mod(samples_collected,1e3)==0 || opt.verbose == 2
366 line_printf('\b\b\b\b\b\b\b\b\b%9d', samples_collected);
367 end
368 end
369end % next_reaction_method
370
371% ======================================================================
372% Buffer maintenance and hashing helpers for FCFS/LCFS nodes
373% ======================================================================
374function buffers = updateBuffers(kfire, nvec, buffers, fromIR, destPos, mi, R, sn)
375% Maintain the ordered per-node buffers when reaction KFIRE fires. A
376% departure removes the head/tail job of the source buffer; an arrival at a
377% buffered destination whose servers are all busy joins the buffer head.
378ind = fromIR(kfire,1); % source node of the firing
379
380% Handle departure from FCFS/LCFS source node
381if isFCFS(ind, sn)
382 if ~isempty(buffers{ind})
383 buffers{ind}(end) = []; % pollLast
384 end
385elseif isLCFS(ind, sn)
386 if ~isempty(buffers{ind})
387 buffers{ind}(1) = []; % pollFirst
388 end
389end
390
391% Handle arrival at a buffered destination node
392if ~isempty(destPos) && destPos > 0
393 jnd = floor((destPos-1)/R) + 1;
394 s = mod(destPos-1, R) + 1;
395 if isFCFS(jnd, sn) || isLCFS(jnd, sn)
396 totalAtDest = sum(nvec(((jnd-1)*R + 1):(jnd*R)));
397 if totalAtDest > mi(jnd)
398 % All servers busy - arriving job joins back of buffer
399 buffers{jnd} = [s, buffers{jnd}]; % addFirst
400 end
401 % Otherwise job went straight into service, buffer unchanged
402 end
403end
404end
405
406function s = bufferHashAll(bufs)
407% Buffer hash over all node indices (used to distinguish unique states).
408parts = cell(1, numel(bufs));
409for ind = 1:numel(bufs)
410 parts{ind} = [num2str(ind), ':', mat2str(bufs{ind}(:)')];
411end
412s = strjoin(parts, '|');
413end
414
415function tf = isFCFS(ind, sn)
416tf = false;
417if sn.isstation(ind)
418 ist = sn.nodeToStation(ind);
419 tf = (sn.sched(ist) == SchedStrategy.FCFS);
420end
421end
422
423function tf = isLCFS(ind, sn)
424tf = false;
425if sn.isstation(ind)
426 ist = sn.nodeToStation(ind);
427 tf = (sn.sched(ist) == SchedStrategy.LCFS);
428end
429end
Definition mmt.m:124