1function [pi, outspace, depRates, sn] = solver_ssa_nrm(sn, options)
2% SOLVER_SSA_NRM Steady‑state analysis via the Next‑Reaction Method (SSA)
4% [PI, SSQ, ARVRATES, DEPRATES, SN] = SOLVER_SSA_NRM(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
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.
22% See also NEXT_REACTION_METHOD.
24% ---------------------------------------------------------------------
25% Parameters & shorthands
26% ---------------------------------------------------------------------
27samples = options.samples;
31% ---------------------------------------------------------------------
32% Stoichiometry & reaction mapping (self‑loops included) ----------------
33% ---------------------------------------------------------------------
34S = zeros(0, I*R); % will transpose at the end
39% this is currently M^2*R^2, it can be lowered to M*R decoupling the
45 fromIR(k,:) = [ind, r];
46 fromIdx(k) = (ind-1)*R + r;
49 Srow = zeros(1, I*R); % build stoichiometry row
51 Srow(fromIdx(k)) = -Inf;
53 Srow(fromIdx(k)) = -1;
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);
60 Srow((jnd-1)*R + s) = Srow((jnd-1)*R + s) + p;
68S = S.'; % states × reactions
70% ---------------------------------------------------------------------
71% Initial state vector --------------------------------------------------
72% ---------------------------------------------------------------------
73nvec0 = zeros(I*R,1); % initial state (aggregate state)
76 [~,nir] = State.toMarginalAggr(sn, ind, state{sn.nodeToStateful(ind)});
79 if sn.nodetype(ind) == NodeType.Source
82 line_error(mfilename,
'Infinite population error.');
85 nvec0((ind-1)*R + r,1) = nir(r);
95 ist = sn.nodeToStation(ind);
96 muir = sn.rates(ist,r);
100 mi(ind,1) = sn.nservers(ist);
104 rates(ind,r) = GlobalConstants.Immediate;
105 mi(ind,1) = GlobalConstants.MaxInt;
108 mi(isinf(mi)) = GlobalConstants.MaxInt;
111% Propensity function ---------------------------------------------------
112epstol = GlobalConstants.Zero;
114for j=1:length(fromIdx)
115 if sn.isstation(fromIR(j,1))
116 switch sn.sched(sn.nodeToStation(fromIR(j,1)))
117 case SchedStrategy.EXT
118 a{j} = @(X) rates(fromIR(j,1), fromIR(j,2));
119 case SchedStrategy.INF
120 a{j} = @(X) rates(fromIR(j,1), fromIR(j,2)) * X(fromIdx(j));
121 case SchedStrategy.PS
122 if R == 1 % single
class
123 a{j} = @(X) rates(fromIR(j,1), fromIR(j,2)) * min( mi(fromIR(j,1)), X(fromIdx(j)));
125 a{j} = @(X) rates(fromIR(j,1), fromIR(j,2)) * ( X(fromIdx(j)) ./ ...
126 (epstol+sum( X(((fromIR(j,1)-1)*R + 1):((fromIR(j,1)-1)*R + R)) ) )) * ...
127 min( mi(fromIR(j,1)), ...
128 (epstol+sum( X(((fromIR(j,1)-1)*R + 1):((fromIR(j,1)-1)*R + R)) ) ));
132 a{j} = @(X) rates(fromIR(j,1), fromIR(j,2)) * min(1, X(fromIdx(j)));
136% Propensity functions dependencies -----------------------------------
137D = cell(1,size(S,2));
139 J = find(S(:,k))'; % set of state variables affected by reaction k
144 r = mod(pos-1, R) + 1;
145 ind = ((pos-r)/R) + 1;
146 vecd(end+1:end+R) = ((ind-1)*R + 1) : (ind*R);
148 % vecd now contains all state variables affected by the firing of
149 % reaction k. We now find the propensity functions that depend
155 vecs = [vecs,find(S(vecd(j),:)<0)];
163% Having accounted
for them in D, we can now remove self-loops markings
165% ---------------------------------------------------------------------
166% Run SSA/NRM -----------------------------------------------------------
167% ---------------------------------------------------------------------
168if false %snIsClosedModel(sn)
169 % mixed-radix hashing
170 reactCache = containers.Map(
'KeyType',
'uint64',
'ValueType',
'any');
172 mixedradix = [cumprod(repmat(1+njobs,1,I))];
173 mixedradix = [1,mixedradix(1:end-1)];
174 hashfun = @(v) uint64(mixedradix*v(:));
176 % buffer size unbounded so use
string
177 reactCache = containers.Map(
'KeyType',
'char',
'ValueType',
'any');
178 hashfun = @(v) mat2str(v(:)
');
180[t, nvecsim, ~, ~] = next_reaction_method(S, D, a, nvec0, samples, options, reactCache, hashfun);
182% ---------------------------------------------------------------------
183% Empirical state probabilities ----------------------------------------
184% ---------------------------------------------------------------------
186[outspace, ~, ic] = unique(nvecsim(:,1:end-1).',
'rows',
'stable');
187timeAccum = accumarray(ic, dt);
188pi = timeAccum / sum(timeAccum);
190% ---------------------------------------------------------------------
191% Per‑state arrival / departure rates (self‑loops counted) -------------
192% ---------------------------------------------------------------------
193numStates = size(outspace,1);
194depRates = zeros(numStates, I*R);
197 a_state = reactCache(hashfun(outspace(st,:)
'));
198 for j = 1:length(fromIdx)
199 depRates(st, fromIdx(j)) = depRates(st, fromIdx(j)) + a_state(j);
204% ======================================================================
205% Next-Reaction Method core --------------------------------------------
206% ======================================================================
207function [t, nvec, kfires, rfires] = next_reaction_method(S, D, a, nvec0, samples, options, reactcache, hashfun)
208numReactions = size(S,2);
211% when a reaction fires, this matrix helps selecting the probability that a
212% particular routing or phase is selected as a result ------------------
213P = S; P(P<0)=P(P<0)+1';
214fromIdx = cell(numReactions,1);
215toIdx = cell(numReactions,1);
216cdfVec = cell(numReactions,1);
218 nnzP(r) = nnz(
P(:,r));
220 fromIdx{r} = find(S(:,r)<0);
221 toIdx{r} = find(
P(:,r));
222 cdfVec{r} = cumsum(
P(toIdx{r},r));
226% initialise Gillespie clocks ------------------------------------------
233reactcache(key) = Ak; % cache first state
's propensities
234Pk = -log(rand(1,numReactions));
235Tk = zeros(1,numReactions);
237tau = (Pk - Tk) ./ Ak;
239% logs -----------------------------------------------------------------
240tout = zeros(samples,1);
241nvecout = zeros(length(nvec0),samples);
242kfires = zeros(samples,1);
243rfires = zeros(samples,1);
247 [dt, kfire] = min(tau);
249 if isinf(dt), line_error(mfilename,'Deadlock. Quitting nrm method.
'); end
253 % update aggregate state
255 r = 1+find(rand>=cdfVec{kfire},1);
260 nvec(fromIdx{kfire}) = nvec(fromIdx{kfire}) - 1;
261 nvec(toIdx{kfire}(r)) = nvec(toIdx{kfire}(r)) + 1;
263 nvec = nvec + S(:,kfire); % zero change for self-loops
268 % update rates for all reactions dependent on the last fired reaction
274 if ~isKey(reactcache,key)
275 reactcache(key) = Ak; % store propensities of new state
278 % maintain random number pool
279 n_mod = mod(n,rand_pool_size);
281 rand_pool = rand(1+min(rand_pool_size, samples-n),1);
285 Pk(kfire) = Pk(kfire) - log(rand_pool(n_mod));
286 tau = (Pk - Tk) ./ Ak;
293 % do not count immediate events
295 print_progress(options, n);
297% Print newline after progress counter
298if isfield(options,'verbose
') && options.verbose
303nvec = [nvec0, nvecout];
305 function print_progress(opt, samples_collected)
306 if ~isfield(opt,'verbose
') || ~opt.verbose, return; end
307 if samples_collected == 1e3
308 line_printf('\nSSA samples: %8d
', samples_collected);
309 elseif opt.verbose == 2
310 if samples_collected == 0
311 line_printf('\nSSA samples: %9d
', samples_collected);
313 line_printf('\b\b\b\b\b\b\b\b\b%9d
', samples_collected);
315 elseif mod(samples_collected,1e3)==0 || opt.verbose == 2
316 line_printf('\b\b\b\b\b\b\b\b\b%9d
', samples_collected);
319end % next_reaction_method