1function [QNclass,UNclass,RNclass,TNclass,ANclass,WNclass] = getAvg(self,Q,U,R,T,A,W)
2% [QNCLASS,UNCLASS,RNCLASS,TNCLASS,ANCLASS,WNCLASS] = GETAVG(SELF,Q,U,R,T,A,W)
4% Compute steady-state average metrics (queue length, utilization, response time,
5% throughput, arrival rate, residence time)
for all stations and job
classes.
7% Copyright (c) 2012-2026, Imperial College London
10sn = self.model.getStruct();
12if strcmp(self.options.lang,
'java') && ~strcmp(self.name,
'SolverLDES')
16 % Force fresh Java model conversion each time to ensure current state
17 % Only re-create the Java model if the model
is not already Java-native
18 if ~self.model.isJavaNative()
22 self.obj.getOptions.verbose = jline.VerboseLevel.STD;
23 SolverResult = self.obj.getAvg();
24 QN = JLINE.from_jline_matrix(SolverResult.QN);
25 UN = JLINE.from_jline_matrix(SolverResult.UN);
26 RN = JLINE.from_jline_matrix(SolverResult.RN);
27 TN = JLINE.from_jline_matrix(SolverResult.TN);
28 AN = JLINE.from_jline_matrix(SolverResult.AN);
29 WN = JLINE.from_jline_matrix(SolverResult.WN);
30 runtime=SolverResult.runtime;
31 method=SolverResult.method;
32 % Extract cache hit/miss probabilities from Java model
34 if sn.nodetype(ind) == NodeType.Cache
35 jnode = self.model.obj.getNodeByIndex(ind-1);
36 hitRatioVec = JLINE.from_jline_matrix(jnode.getHitRatio());
37 missRatioVec = JLINE.from_jline_matrix(jnode.getMissRatio());
38 % Store per-class hit/miss probabilities matching MATLAB format:
39 % only parent
classes with hitClass>0 have non-zero entries
40 hitClass = self.model.
nodes{ind}.getHitClass;
41 nk = length(hitClass);
42 hitprob = zeros(1, nk);
43 missprob = zeros(1, nk);
45 if hitClass(k) > 0 && k <= length(hitRatioVec)
46 hitprob(k) = hitRatioVec(k);
47 missprob(k) = missRatioVec(k);
50 self.model.nodes{ind}.setResultHitProb(hitprob);
51 self.model.nodes{ind}.setResultMissProb(missprob);
54 if any(sn.nodetype == NodeType.Cache)
55 self.model.refreshStruct(true);
57 self.setAvgResults(QN,UN,RN,TN,AN,WN,[],[],runtime,method,1);
58 QNclass = reshape(QN,M,R);
59 UNclass = reshape(UN,M,R);
60 RNclass = reshape(RN,M,R);
61 TNclass = reshape(TN,M,R);
62 ANclass = reshape(AN,M,R);
63 WNclass = reshape(WN,M,R);
68if nargin == 1 % no parameter
69 if isempty(self.model.handles) || ~isfield(self.model.handles,'Q') || ...
70 ~isfield(self.model.handles,'U') || ~isfield(self.model.handles,'R') || ...
71 ~isfield(self.model.handles,'T') || ~isfield(self.model.handles,'A') || ...
72 ~isfield(self.model.handles,'W')
73 reset(self); % reset results in case there are partial results saved
75 [Q,U,R,T,A,W] = self.getAvgHandles;
78 [Q,U,R,T,A,W] = deal(handlers{:}); % set Q=handlers{1}, U=handlers{2}, ...
81if isfield(self.options,
'timespan')
82 if isfinite(self.options.timespan(2))
83 line_error(mfilename,'The getAvg method does not support the timespan option, use the getTranAvg method instead.');
86 self.options.timespan = [0,Inf];
89if ~self.hasAvgResults() || ~self.options.cache
91 % the next line
is required because getAvg can alter the chain
92 % structure in the presence of caches so we need to reload sn
93 sn = self.model.getStruct;
94 if ~self.hasAvgResults
95 line_error(mfilename,'Unable to return results for this model.');
97end % else return cached value
103% Check if this
is an SPN model (Places don't have response times)
104hasSPN = any(sn.nodetype == NodeType.Place) || any(sn.nodetype == NodeType.Transition);
107 RNclass = filterMetric(R, self.result.Avg.R, [], sn, K, M);
113 % For SPNs, don't zero Q based on R because Places don't have response times
117 zeroMaskQ = RNclass < 10 * GlobalConstants.FineTol;
119 QNclass = filterMetric(Q, self.result.Avg.Q, zeroMaskQ, sn, K, M);
125 % For SPNs, don't zero U based on R because Places don't have response times
129 zeroMaskU = RNclass < 10 * GlobalConstants.FineTol;
131 UNclass = filterMetric(U, self.result.Avg.U, zeroMaskU, sn, K, M);
137 TNclass = filterMetric(T, self.result.Avg.T, [], sn, K, M);
143 zeroMask = false(size(RNclass));
144 zeroMask(sn.nodeToStation(sn.nodetype==NodeType.Source),:) = true;
145 ANclass = filterMetric(A, self.result.Avg.A, zeroMask, sn, K, M);
151 WNclass = sn_get_residt_from_respt(sn, RNclass, W);
157 unstableQueues = find(sum(UNclass,2)>0.99 * sn.nservers);
158 if any(unstableQueues) && any(isinf(sn.njobs))
159 line_warning(mfilename,'The model has unstable queues, performance metrics may grow unbounded.\n')
164function outData = filterMetric(handle, metric, zeroMask, sn, K, M)
165% post-process Avg measure
166outData = zeros(M, K);
169 if ~handle{i,k}.disabled && ~isempty(metric)
170 outData(i,k) = metric(i,k);
177% NaN values indicate that a metric
is disabled
178outData(isnan(outData)) = 0;
179% set to zero entries associated to immediate transitions
180outData(zeroMask) = 0;
181% round to zero numerical perturbations
182outData(outData < GlobalConstants.FineTol) = 0;
184% set to zero metrics
for classes that are unreachable
185% but skip
this check
for fork-join models where the
visits calculation
186% doesn
't correctly capture the parent class visiting Join and downstream nodes
187% also skip when no chains are defined (routing not specified)
188% also skip for SPN models where Places don't have traditional
visits
189hasForkJoin = any(sn.nodetype == NodeType.Fork) && any(sn.nodetype == NodeType.Join);
190hasSPN = any(sn.nodetype == NodeType.Place) || any(sn.nodetype == NodeType.Transition);
192if sn.nchains > 0 && ~hasSPN % Only check reachability
if chains are defined and not SPN
194 c = sn.chains(:, k)>0;
195 if any(c) % Only check
if class k belongs to a chain
197 if sn.visits{c}(i,k) == 0
198 % For fork-join models, don
't zero out if the metric has a non-zero value
199 % from simulation - the visits calculation doesn't capture fork-join semantics
200 % where Join outputs the parent
class
201 if hasForkJoin && ~isempty(metric) && metric(i,k) > GlobalConstants.FineTol
202 continue; % Trust the simulation result