1%{ @file sn_refresh_visits.m
2 % @brief Solves traffic equations to compute visit ratios
4 % @author LINE Development Team
5 % Copyright (c) 2012-2026, Imperial College London
10 % @brief Solves traffic equations to compute visit ratios
13 % This function solves
the traffic equations to compute
the average number
23 % <tr><th>Name<th>Description
24 % <tr><td>sn<td>Network structure
25 % <tr><td>chains<td>Chain definitions
26 % <tr><td>rt<td>
Station routing matrix
27 % <tr><td>rtnodes<td>Node routing matrix
32 % <tr><th>Name<th>Description
33 % <tr><td>
visits<td>Cell array of visit ratios at stations per chain
35 % <tr><td>sn<td>Updated network structure with visit fields populated
38function [
visits, nodevisits, sn] = sn_refresh_visits(sn, chains, rt, rtnodes)
44nchains = size(chains,1);
46%% obtain chain characteristics
49 if sum(refstat(inchain{c}) == refstat(inchain{c}(1))) ~= length(inchain{c})
50 refstat(inchain{c}) = refstat(inchain{c}(1));
51 % line_error(mfilename,sprintf(
'Classes in chain %d have different reference stations. Chain %d classes: %s', c, c, int2str(inchain{c})));
58 cols = zeros(1,M*length(inchain{c}));
60 nIC = length(inchain{c});
62 cols(1,(ist-1)*nIC+ik) = (ist-1)*K+inchain{c}(ik);
66 Pchain = rt(cols,cols); % routing probability of
the chain
68 % Handle NaN values in routing matrix (e.g., from Cache
class switching)
69 % For
visits calculation, replace NaN with equal probabilities
70 for row = 1:size(Pchain,1)
71 nan_cols = isnan(Pchain(row,:));
73 % Get
the non-NaN sum for this row
74 non_nan_sum = sum(Pchain(row, ~nan_cols));
75 % Distribute remaining probability equally among NaN entries
76 remaining_prob = max(0, 1 - non_nan_sum);
77 n_nan = sum(nan_cols);
78 if n_nan > 0 && remaining_prob > 0
79 Pchain(row, nan_cols) = remaining_prob / n_nan;
81 Pchain(row, nan_cols) = 0;
86 visited = sum(Pchain,2) > 0;
88 % Normalize routing matrix for Fork-containing models
89 % Fork
nodes have row sums > 1 (sending to all branches with prob 1 each)
90 % which causes dtmc_solve_reducible to fail. Normalize to make stochastic.
91 % Record original row sums to correct visit ratios after DTMC solve.
92 row_sums = ones(size(Pchain,1), 1);
93 if any(sn.nodetype == NodeType.Fork)
94 for row = 1:size(Pchain,1)
95 rs = sum(Pchain(row,:));
97 if rs > GlobalConstants.FineTol
98 Pchain(row,:) = Pchain(row,:) / rs;
103 % Use dtmc_solve as primary, fallback to dtmc_solve_reducible for chains with transient states
104 Pchain_visited = Pchain(visited,visited);
105 % dtmc_solve now throws (via ctmc_solve) on a genuinely reducible chain
106 % instead of returning zeros, so
the primary call
is guarded: a throw or a
107 % zero/NaN return both route to
the reducible solver. Mirrors
the JAR/Python
108 % sn_refresh_visits, which wrap
the primary solve
the same way. Do NOT change
109 % this order -- dtmc_solve stays primary, dtmc_solve_reducible
the fallback.
111 alpha_visited = dtmc_solve(Pchain_visited);
112 if all(alpha_visited == 0) || any(isnan(alpha_visited))
113 [alpha_visited, ~, ~, ~, ~] = dtmc_solve_reducible(Pchain_visited, [], struct('tol', GlobalConstants.FineTol));
116 [alpha_visited, ~, ~, ~, ~] = dtmc_solve_reducible(Pchain_visited, [], struct('tol', GlobalConstants.FineTol));
118 alpha = zeros(1,M*K); alpha(visited) = alpha_visited;
119 if max(alpha)>=1-GlobalConstants.FineTol
120 %disabled because a self-looping customer
is an absorbing chain
121 %line_error(mfilename,'One chain has an absorbing state.');
124 % SPN-based fork correction: population-preserving SPN analysis proves
125 % that all visited entries have uniform visit ratios in fork-join models.
126 % This replaces
the transitive closure correction.
127 if any(sn.nodetype == NodeType.Fork) && any(row_sums > 1 + GlobalConstants.FineTol)
128 for idx = 1:length(alpha)
129 if alpha(idx) > GlobalConstants.FineTol
137 for k=1:length(inchain{c})
138 visits{c}(ist,inchain{c}(k)) = alpha((ist-1)*length(inchain{c})+k);
141 normSum = sum(
visits{c}(sn.stationToStateful(refstat(inchain{c}(1))),inchain{c}));
142 if normSum > GlobalConstants.FineTol
151 nodes_cols = zeros(1,I*length(inchain{c}));
153 nIC = length(inchain{c});
155 nodes_cols(1,(ind-1)*nIC+ik) = (ind-1)*K+inchain{c}(ik);
158 nodes_Pchain = rtnodes(nodes_cols, nodes_cols); % routing probability of
the chain
160 % Handle NaN values in routing matrix (e.g., from Cache
class switching)
161 % For
visits calculation, replace NaN with equal probabilities
162 for row = 1:size(nodes_Pchain,1)
163 nan_cols = isnan(nodes_Pchain(row,:));
165 % Get
the non-NaN sum for this row
166 non_nan_sum = sum(nodes_Pchain(row, ~nan_cols));
167 % Distribute remaining probability equally among NaN entries
168 remaining_prob = max(0, 1 - non_nan_sum);
169 n_nan = sum(nan_cols);
170 if n_nan > 0 && remaining_prob > 0
171 nodes_Pchain(row, nan_cols) = remaining_prob / n_nan;
173 nodes_Pchain(row, nan_cols) = 0;
178 nodes_visited = sum(nodes_Pchain,2) > 0;
180 % Normalize routing matrix for Fork-containing models
181 % Record original row sums to correct visit ratios after DTMC solve.
182 nodes_row_sums = ones(size(nodes_Pchain,1), 1);
183 if any(sn.nodetype == NodeType.Fork)
184 for row = 1:size(nodes_Pchain,1)
185 rs = sum(nodes_Pchain(row,:));
186 nodes_row_sums(row) = rs;
187 if rs > GlobalConstants.FineTol
188 nodes_Pchain(row,:) = nodes_Pchain(row,:) / rs;
193 % Use dtmc_solve as primary, fallback to dtmc_solve_reducible for chains with transient states
194 nodes_Pchain_visited = nodes_Pchain(nodes_visited,nodes_visited);
195 % Guard
the primary solve against a reducible-chain throw, as above.
197 nodes_alpha_visited = dtmc_solve(nodes_Pchain_visited);
198 if all(nodes_alpha_visited == 0) || any(isnan(nodes_alpha_visited))
199 [nodes_alpha_visited, ~, ~, ~, ~] = dtmc_solve_reducible(nodes_Pchain_visited, [], struct('tol', GlobalConstants.FineTol));
202 [nodes_alpha_visited, ~, ~, ~, ~] = dtmc_solve_reducible(nodes_Pchain_visited, [], struct('tol', GlobalConstants.FineTol));
204 nodes_alpha = zeros(1,I*K); nodes_alpha(nodes_visited) = nodes_alpha_visited;
206 % SPN-based fork correction for node
visits: population-preserving SPN
207 % analysis gives visit=1 for stations/Fork
nodes and visit=n_sources
208 % for Join
nodes (one per incoming branch).
209 if any(sn.nodetype == NodeType.Fork) && any(nodes_row_sums > 1 + GlobalConstants.FineTol)
210 nIC = length(inchain{c});
211 for idx = 1:length(nodes_alpha)
212 if nodes_alpha(idx) > GlobalConstants.FineTol
213 nd = floor((idx-1) / nIC) + 1;
214 if sn.nodetype(nd) == NodeType.Join
215 r = inchain{c}(mod(idx-1, nIC) + 1);
217 n_sources = sum(sn.rtnodes(:, col) > GlobalConstants.FineTol);
218 nodes_alpha(idx) = n_sources;
220 nodes_alpha(idx) = 1;
228 for k=1:length(inchain{c})
229 nodevisits{c}(ind,inchain{c}(k)) = nodes_alpha((ind-1)*length(inchain{c})+k);
232 nodeNormSum = sum(
nodevisits{c}(sn.statefulToNode(refstat(inchain{c}(1))),inchain{c}));
233 if nodeNormSum > GlobalConstants.FineTol