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
14 % of
visits to
nodes and stations
for each chain in the network.
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})));
56visits = cell(nchains,1); %
visits{c}(i,j)
is the number of
visits that a chain-c job pays at node i in
class j
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 alpha_visited = dtmc_solve(Pchain_visited);
106 % Fallback to dtmc_solve_reducible if dtmc_solve fails (e.g., reducible chain)
107 if all(alpha_visited == 0) || any(isnan(alpha_visited))
108 [alpha_visited, ~, ~, ~, ~] = dtmc_solve_reducible(Pchain_visited, [], struct('tol', GlobalConstants.FineTol));
110 alpha = zeros(1,M*K); alpha(visited) = alpha_visited;
111 if max(alpha)>=1-GlobalConstants.FineTol
112 %disabled because a self-looping customer
is an absorbing chain
113 %line_error(mfilename,'One chain has an absorbing state.');
116 % Apply Fork fanout correction: multiply all downstream station
visits by fanout
117 % This corrects for the normalization of rows with sum > 1 (caused by Fork
118 % routing being collapsed into the stateful routing matrix).
119 % The correction must propagate transitively through all reachable
nodes.
120 if any(sn.nodetype == NodeType.Fork)
121 % Compute transitive closure of the routing matrix to find all reachable states
123 % Use binary reachability (Pchain > 0 gives adjacency)
124 adj = Pchain > GlobalConstants.FineTol;
126 % Iterate to compute transitive closure (Warshall's algorithm style)
127 for iter = 1:ceil(log2(n))
128 reachable = reachable | (reachable * reachable > 0);
131 % For each fork row, multiply all transitively reachable states by fanout
133 fanout = row_sums(row);
134 if fanout > 1 + GlobalConstants.FineTol
135 % Scale all states reachable from this fork's destinations
137 if reachable(row, col)
138 alpha(col) = alpha(col) * fanout;
147 for k=1:length(inchain{c})
148 visits{c}(ist,inchain{c}(k)) = alpha((ist-1)*length(inchain{c})+k);
151 normSum = sum(
visits{c}(sn.stationToStateful(refstat(inchain{c}(1))),inchain{c}));
152 if normSum > GlobalConstants.FineTol
161 nodes_cols = zeros(1,I*length(inchain{c}));
163 nIC = length(inchain{c});
165 nodes_cols(1,(ind-1)*nIC+ik) = (ind-1)*K+inchain{c}(ik);
168 nodes_Pchain = rtnodes(nodes_cols, nodes_cols); % routing probability of the chain
170 % Handle NaN values in routing matrix (e.g., from Cache
class switching)
171 % For
visits calculation, replace NaN with equal probabilities
172 for row = 1:size(nodes_Pchain,1)
173 nan_cols = isnan(nodes_Pchain(row,:));
175 % Get the non-NaN sum for this row
176 non_nan_sum = sum(nodes_Pchain(row, ~nan_cols));
177 % Distribute remaining probability equally among NaN entries
178 remaining_prob = max(0, 1 - non_nan_sum);
179 n_nan = sum(nan_cols);
180 if n_nan > 0 && remaining_prob > 0
181 nodes_Pchain(row, nan_cols) = remaining_prob / n_nan;
183 nodes_Pchain(row, nan_cols) = 0;
188 nodes_visited = sum(nodes_Pchain,2) > 0;
190 % Normalize routing matrix for Fork-containing models
191 % Record original row sums to correct visit ratios after DTMC solve.
192 nodes_row_sums = ones(size(nodes_Pchain,1), 1);
193 if any(sn.nodetype == NodeType.Fork)
194 for row = 1:size(nodes_Pchain,1)
195 rs = sum(nodes_Pchain(row,:));
196 nodes_row_sums(row) = rs;
197 if rs > GlobalConstants.FineTol
198 nodes_Pchain(row,:) = nodes_Pchain(row,:) / rs;
203 % Use dtmc_solve as primary, fallback to dtmc_solve_reducible for chains with transient states
204 nodes_Pchain_visited = nodes_Pchain(nodes_visited,nodes_visited);
205 nodes_alpha_visited = dtmc_solve(nodes_Pchain_visited);
206 % Fallback to dtmc_solve_reducible if dtmc_solve_reducible fails (e.g., reducible chain)
207 if all(nodes_alpha_visited == 0) || any(isnan(nodes_alpha_visited))
208 [nodes_alpha_visited, ~, ~, ~, ~] = dtmc_solve_reducible(nodes_Pchain_visited, [], struct('tol', GlobalConstants.FineTol));
210 nodes_alpha = zeros(1,I*K); nodes_alpha(nodes_visited) = nodes_alpha_visited;
212 % Apply Fork fanout correction for node
visits
213 % The correction must propagate transitively through all reachable
nodes.
214 if any(sn.nodetype == NodeType.Fork)
215 % Compute transitive closure of the routing matrix to find all reachable states
216 n_nodes = size(nodes_Pchain, 1);
217 % Use binary reachability (nodes_Pchain > 0 gives adjacency)
218 nodes_adj = nodes_Pchain > GlobalConstants.FineTol;
219 nodes_reachable = nodes_adj;
220 % Iterate to compute transitive closure (Warshall's algorithm style)
221 for iter = 1:ceil(log2(n_nodes))
222 nodes_reachable = nodes_reachable | (nodes_reachable * nodes_reachable > 0);
225 % For each fork row, multiply all transitively reachable states by fanout
227 fanout = nodes_row_sums(row);
228 if fanout > 1 + GlobalConstants.FineTol
229 % Scale all states reachable from this fork's destinations
231 if nodes_reachable(row, col)
232 nodes_alpha(col) = nodes_alpha(col) * fanout;
241 for k=1:length(inchain{c})
242 nodevisits{c}(ind,inchain{c}(k)) = nodes_alpha((ind-1)*length(inchain{c})+k);
245 nodeNormSum = sum(
nodevisits{c}(sn.statefulToNode(refstat(inchain{c}(1))),inchain{c}));
246 if nodeNormSum > GlobalConstants.FineTol