LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
getAvg.m
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)
3%
4% Compute steady-state average metrics (queue length, utilization, response time,
5% throughput, arrival rate, residence time) for all stations and job classes.
6%
7% Copyright (c) 2012-2026, Imperial College London
8% All rights reserved.
9
10sn = self.model.getStruct();
11
12if strcmp(self.options.lang,'java') && ~strcmp(self.name,'SolverLDES')
13 T0=tic;
14 M = sn.nstations;
15 R = sn.nclasses;
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()
19 self.model.obj = [];
20 self.setLang();
21 end
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
33 for ind = 1:sn.nnodes
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);
44 for k = 1:nk
45 if hitClass(k) > 0 && k <= length(hitRatioVec)
46 hitprob(k) = hitRatioVec(k);
47 missprob(k) = missRatioVec(k);
48 end
49 end
50 self.model.nodes{ind}.setResultHitProb(hitprob);
51 self.model.nodes{ind}.setResultMissProb(missprob);
52 end
53 end
54 if any(sn.nodetype == NodeType.Cache)
55 self.model.refreshStruct(true);
56 end
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);
64 return
65end
66
67%%
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
74 end
75 [Q,U,R,T,A,W] = self.getAvgHandles;
76elseif nargin == 2
77 handlers = Q;
78 [Q,U,R,T,A,W] = deal(handlers{:}); % set Q=handlers{1}, U=handlers{2}, ...
79end
80
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.');
84 end
85else
86 self.options.timespan = [0,Inf];
87end
88
89if ~self.hasAvgResults() || ~self.options.cache
90 runAnalyzer(self);
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.');
96 end
97end % else return cached value
98
99
100M = sn.nstations;
101K = sn.nclasses;
102
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);
105
106if ~isempty(R)
107 RNclass = filterMetric(R, self.result.Avg.R, [], sn, K, M);
108else
109 RNclass = [];
110end
111
112if ~isempty(Q)
113 % For SPNs, don't zero Q based on R because Places don't have response times
114 if hasSPN
115 zeroMaskQ = [];
116 else
117 zeroMaskQ = RNclass < 10 * GlobalConstants.FineTol;
118 end
119 QNclass = filterMetric(Q, self.result.Avg.Q, zeroMaskQ, sn, K, M);
120else
121 QNclass = [];
122end
123
124if ~isempty(U)
125 % For SPNs, don't zero U based on R because Places don't have response times
126 if hasSPN
127 zeroMaskU = [];
128 else
129 zeroMaskU = RNclass < 10 * GlobalConstants.FineTol;
130 end
131 UNclass = filterMetric(U, self.result.Avg.U, zeroMaskU, sn, K, M);
132else
133 UNclass = [];
134end
135
136if ~isempty(T)
137 TNclass = filterMetric(T, self.result.Avg.T, [], sn, K, M);
138else
139 TNclass = [];
140end
141
142if ~isempty(A)
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);
146else
147 ANclass = [];
148end
149
150if ~isempty(W)
151 WNclass = sn_get_residt_from_respt(sn, RNclass, W);
152else
153 WNclass = [];
154end
155
156if ~isempty(UNclass)
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')
160 end
161end
162end
163
164function outData = filterMetric(handle, metric, zeroMask, sn, K, M)
165% post-process Avg measure
166outData = zeros(M, K);
167for k = 1:K
168 for i = 1:M
169 if ~handle{i,k}.disabled && ~isempty(metric)
170 outData(i,k) = metric(i,k);
171 else
172 outData(i,k) = NaN;
173 end
174 end
175end
176
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;
183
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);
191
192if sn.nchains > 0 && ~hasSPN % Only check reachability if chains are defined and not SPN
193 for k = 1:K
194 c = sn.chains(:, k)>0;
195 if any(c) % Only check if class k belongs to a chain
196 for i = 1:M
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
203 end
204 outData(i,k) = 0;
205 end
206 end
207 end
208 end
209end
210end
Definition mmt.m:124