1function self = link(self,
P)
4% Copyright (c) 2012-2026, Imperial College London
7if ~isempty(self.connections)
8 line_error(mfilename,'The Network.link method cannot be used after calling the addLink() method. Use Node.setProbRouting instead to configure routing probabilities.');
11if isa(
P,'RoutingMatrix')
19 self.resetNetwork; % remove artificial class switch
nodes
21K = self.getNumberOfClasses;
22I = self.getNumberOfNodes;
25 line_error(mfilename,'Multiclass model: the linked routing matrix
P must be a cell array, e.g.,
P = model.initRoutingMatrix;
P{1} = Pclass1;
P{2} = Pclass2.
');
29if size(P,1) == size(P,2)
38 % in this case it is possible that P is linear but just because the
39 % routing is state-dependent and therefore some zero entries are
40 % actually unspecified
41 %cacheNodes = find(cellfun(@(c) isa(c,'Cache
'), self.getStatefulNodes));
43 switch class(self.nodes{ind})
45 % note that since a cache needs to distinguish hits and
46 % misses, it needs to do class-switch unless the model is
49 if self.nodes{ind}.server.hitClass == self.nodes{ind}.server.missClass
50 line_warning(mfilename,'Ambiguous use of hitClass and missClass at cache, it
is recommended to use different
classes.\n
');
56% This block is to make sure that P = model.initRoutingMatrix; P{2} writes
57% into P{2,2} rather than being interpreted as P{2,1}.
75% assign routing for self-looping jobs
77 if isa(self.classes{r},'SelfLoopingClass
')
81 P{r,r}(self.classes{r}.refstat, self.classes{r}.refstat) = 1.0;
85% link virtual sinks automatically to sink
86ispool = cellisa(self.nodes,'Sink
');
88 line_error(mfilename,'The model can have at most one sink node.
');
91if sum(cellisa(self.nodes,'Source
')) > 1
92 line_error(mfilename,'The model can have at most one source node.
');
94ispool_nnz = find(ispool)';
107 P((ind-1)*K+1:ind*K,:)=0;
116 P{r,s}(ind,jnd) = Pmat((ind-1)*K+r,(jnd-1)*K+s);
128 P{r}((ind-1)*K+1:ind*K,:)=0;
142isemptyP =
false(K,K);
156csnodematrix = cell(I,I);
159 csnodematrix{ind,jnd} = zeros(K,K);
166 [If,Jf] = find(
P{r,s});
168 csnodematrix{If(k),Jf(k)}(r,s) =
P{r,s}(If(k),Jf(k));
176% Psum=cellsum({
P{r,:}})*ones(M,1);
177%
if min(Psum)<1-GlobalConstants.CoarseTol
178% line_error(mfilename,
'Invalid routing probabilities (Node %d departures, switching from class %d).',minpos(Psum),r);
180%
if max(Psum)>1+GlobalConstants.CoarseTol
181% line_error(mfilename,sprintf(
'Invalid routing probabilities (Node %d departures, switching from class %d).',maxpos(Psum),r));
187% As we will now create a CS
for each link i->j,
188% we now condition on the job going from node i to j
192 S = sum(csnodematrix{ind,jnd}(r,:));
194 csnodematrix{ind,jnd}(r,:)=csnodematrix{ind,jnd}(r,:)/S;
196 csnodematrix{ind,jnd}(r,r)=1.0;
203% eye(K)
is because any job that travels to an autoAdded
class
204%
switch stays in the same
class prior to reaching the ClassSwitch
205% and anyway the diagonal of csMatrix
is irrelevant
for the chains
207nodeNames = self.getNodeNames;
210 csMatrix = csMatrix + csnodematrix{ind,jnd};
211 if ~isdiag(csnodematrix{ind,jnd})
212 self.nodes{end+1} = ClassSwitch(self, sprintf(
'CS_%s_to_%s',nodeNames{ind},nodeNames{jnd}),csnodematrix{ind,jnd});
213 self.nodes{end}.autoAdded =
true;
214 csid(ind,jnd) = length(self.nodes);
220 %
this is to ensure that also stateful cs like caches
222 if isa(self.nodes{ind},
'Cache')
223 for r=find(self.
nodes{ind}.server.hitClass)
224 csMatrix(r,self.nodes{ind}.server.hitClass(r)) = 1.0;
226 for r=find(self.nodes{ind}.server.missClass)
227 csMatrix(r,self.
nodes{ind}.server.missClass(r)) = 1.0;
229 elseif isa(self.nodes{ind},
'ClassSwitch')
230 if isempty(self.
nodes{ind}.server.csMatrix )
231 line_error(mfilename,
'Uninitialized ClassSwitch node, use the setClassSwitchingMatrix method.');
233 csMatrix = csMatrix | self.nodes{ind}.server.csMatrix > 0.0;
237self.csMatrix = csMatrix~=0;
239Ip = length(self.nodes); % number of
nodes after addition of cs
nodes
244 P{r,s}((I+1):Ip,(I+1):Ip)=0;
255 P{r,r}(ind,csid(ind,jnd)) =
P{r,r}(ind,csid(ind,jnd)) +
P{r,s}(ind,jnd);
258 P{s,s}(csid(ind,jnd),jnd) = 1;
265connected = zeros(Ip);
268 [If,Jf,S] = find(
P{r,r});
270 if connected(If(k),Jf(k)) == 0
272 connected(If(k),Jf(k)) = 1;
274 nodes{If(k)}.setProbRouting(self.classes{r},
nodes{Jf(k)}, S(k));
279% check
if the probability out of any node sums to >1.0
281isAboveOne = pSum > 1.0 + GlobalConstants.FineTol;
283 for ind=find(isAboveOne)
284 if SchedStrategy.toId(self.nodes{ind}.schedStrategy) ~= SchedStrategy.FORK
285 line_error(mfilename,sprintf(
'The total routing probability for jobs leaving node %s in class %s is greater than 1.0.',self.nodes{ind}.name,self.classes{r}.name));
287 % elseif pSum < 1.0 - GlobalConstants.FineTol % we cannot check
this case as
class r may not reach station i, in which case its outgoing routing prob
is zero
288 %
if self.nodes{i}.schedStrategy ~= SchedStrategy.EXT %
if not a sink
289 % line_error(mfilename,
'The total routing probability for jobs leaving node %s in class %s is less than 1.0.',self.nodes{i}.name,self.classes{r}.name);
295 if isa(self.nodes{ind},
'Place')
296 self.
nodes{ind}.init;
300if isReset && ~isempty(self.sn) && isfield(self.sn,
'rates')
301 self.refreshChains; % without this exception with linkAndLog
304%% Check for reducible routing (absorbing states)
306 % Pass routing matrix directly to avoid getStruct() call during link()
307 [isErg, ergInfo] = self.isRoutingErgodic(self.sn.rtorig);
308 if ~isErg && ~isempty(ergInfo.absorbingStations)
309 % Build warning message
310 absNames = strjoin(ergInfo.absorbingStations, ', ');
311 line_warning(mfilename, 'Reducible network topology detected, results may be unreliable.\n');
317 % create java version of the network
319 jnetwork = JLINE.from_line_network(self);
323 % compare sn data structures
324 jsn = JLINE.from_jline_struct(jnetwork);
325 fprintf(1,'* Comparison with Java NetworkStruct: ');
326 bool = testJavaStruct(self.getName(),self.getStruct(),jsn);