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;
141% Default per-item retrieval routing inherited from
the read
class. A user may
142% draw
the retrieval topology once as ordinary
P{readClass,readClass} edges over
143%
the retrieval-system queues (and cache<->queue); each item
's auto-generated
144% retrieval class then inherits that routing on a miss by default. Explicit
145% per-item setItem* entries (injected in the loop below) override these defaults.
146% The read class itself switches to a retrieval class at the cache and never
147% traverses the queues, so its template edges over the queue set are consumed
148% from P afterwards to leave the read class's own flow unchanged.
150 if isa(self.nodes{ind},
'Cache') && isprop(self.nodes{ind},
'retrievalSystemQueueIndices') ...
151 && ~isempty(keys(self.nodes{ind}.retrievalSystemQueueIndices))
152 cacheNode = self.
nodes{ind};
154 nItems = cacheNode.items.nitems;
155 rcMap = cacheNode.retrievalSystemQueueIndices;
158 r = double(ks{kk}) + 1; % read
class index (1-based; key
is index-1)
159 Q = rcMap(ks{kk}); % retrieval-system queue node indices
166 rClass = cacheNode.server.retrievalClasses(it, r);
170 if isempty(
P{rClass,rClass})
171 P{rClass,rClass} = zeros(I);
174 if Pr(c, Q(q)) > 0 % cache -> queue (entry)
175 P{rClass,rClass}(c, Q(q)) = Pr(c, Q(q));
177 if Pr(Q(q), c) > 0 % queue -> cache (exit)
178 P{rClass,rClass}(Q(q), c) = Pr(Q(q), c);
180 for dq=1:nQ % queue -> queue
181 if Pr(Q(q), Q(dq)) > 0
182 P{rClass,rClass}(Q(q), Q(dq)) = Pr(Q(q), Q(dq));
187 % consume
the read
class's template edges over the queue set
192 P{r,r}(Q(q), Q(dq)) = 0;
199% Inject deferred retrieval-system routing entries registered by
200% Cache.setRetrievalSystem. The auto-generated retrieval-pending / -complete
201% classes are not part of the user-supplied P, so their routing edges
202% (cache->queue, queue->queue, queue->cache with the pending->complete class
203% switch) are recorded on the Cache node and merged into P here, before the
204% routing matrix is processed. link() then auto-creates the artificial
205% class-switch node for the pending->complete edge like any other P-encoded
208 if isa(self.nodes{ind}, 'Cache
') && isprop(self.nodes{ind}, 'retrievalRoutingEntries
') ...
209 && ~isempty(self.nodes{ind}.retrievalRoutingEntries)
210 rre = self.nodes{ind}.retrievalRoutingEntries;
212 ent = rre{e}; % [fromCls, toCls, srcNode, dstNode, prob]
213 if isempty(P{ent(1),ent(2)})
214 P{ent(1),ent(2)} = zeros(I);
216 P{ent(1),ent(2)}(ent(3),ent(4)) = ent(5);
221isemptyP = false(K,K);
235csnodematrix = cell(I,I);
238 csnodematrix{ind,jnd} = zeros(K,K);
245 [If,Jf] = find(P{r,s});
247 csnodematrix{If(k),Jf(k)}(r,s) = P{r,s}(If(k),Jf(k));
255% Psum=cellsum({P{r,:}})*ones(M,1);
256% if min(Psum)<1-GlobalConstants.CoarseTol
257% line_error(mfilename,'Invalid routing probabilities (Node %d departures, switching from
class %d).
',minpos(Psum),r);
259% if max(Psum)>1+GlobalConstants.CoarseTol
260% line_error(mfilename,sprintf('Invalid routing probabilities (Node %d departures, switching from
class %d).
',maxpos(Psum),r));
266% As we will now create a CS for each link i->j,
267% we now condition on the job going from node i to j
271 S = sum(csnodematrix{ind,jnd}(r,:));
273 csnodematrix{ind,jnd}(r,:)=csnodematrix{ind,jnd}(r,:)/S;
275 csnodematrix{ind,jnd}(r,r)=1.0;
282% eye(K) is because any job that travels to an autoAdded class
283% switch stays in the same class prior to reaching the ClassSwitch
284% and anyway the diagonal of csMatrix is irrelevant for the chains
286nodeNames = self.getNodeNames;
289 csMatrix = csMatrix + csnodematrix{ind,jnd};
290 if ~isdiag(csnodematrix{ind,jnd})
291 self.nodes{end+1} = ClassSwitch(self, sprintf('CS_%s_to_%s
',nodeNames{ind},nodeNames{jnd}),csnodematrix{ind,jnd});
292 self.nodes{end}.autoAdded = true;
293 csid(ind,jnd) = length(self.nodes);
299 % this is to ensure that also stateful cs like caches
301 if isa(self.nodes{ind},'Cache
')
302 for r=find(self.nodes{ind}.server.hitClass)
303 csMatrix(r,self.nodes{ind}.server.hitClass(r)) = 1.0;
305 for r=find(self.nodes{ind}.server.missClass)
306 csMatrix(r,self.nodes{ind}.server.missClass(r)) = 1.0;
308 elseif isa(self.nodes{ind},'ClassSwitch
')
309 if isempty(self.nodes{ind}.server.csMatrix )
310 line_error(mfilename,'Uninitialized ClassSwitch node, use
the setClassSwitchingMatrix method.
');
312 csMatrix = csMatrix | self.nodes{ind}.server.csMatrix > 0.0;
316self.csMatrix = csMatrix~=0;
318Ip = length(self.nodes); % number of nodes after addition of cs nodes
323 P{r,s}((I+1):Ip,(I+1):Ip)=0;
334 P{r,r}(ind,csid(ind,jnd)) = P{r,r}(ind,csid(ind,jnd)) + P{r,s}(ind,jnd);
336 P{s,s}(csid(ind,jnd),jnd) = 1;
344connected = zeros(Ip);
346% Clear non-station nodes' outputStrategy before setting
new routing.
347% This prevents accumulation from previous link() calls,
348% since setProbRouting appends entries and resetNetwork only
349% clears station
nodes (e.g., Queue, Delay), not non-station
350% stateful
nodes (e.g., Router).
352% Use
the "PreservingRouted" variant when available so that classes already
353% configured upstream (e.g. by Cache.setRetrievalSystem on its auto-generated
354% ClassSwitch and Queues for retrieval-pending classes that don't appear in
355%
P) survive
the clearing step. Falls back to
the legacy clearing for
356% Dispatcher subclasses (Forker/Firing/Linkage) that don't define
the variant.
358 if ~isa(
nodes{ind},
'Station') && ismethod(
nodes{ind}.output,
'initDispatcherJobClasses')
359 if ismethod(
nodes{ind}.output,
'initDispatcherJobClassesPreservingRouted')
360 nodes{ind}.output.initDispatcherJobClassesPreservingRouted(self.classes);
362 nodes{ind}.output.initDispatcherJobClasses(self.classes);
365 % won
't try PROB routing for classes whose entry was cleared, while
366 % preserved entries continue to advertise their routing strategy.
367 if ~isempty(self.sn) && isfield(self.sn, 'routing
') && ind <= size(self.sn.routing, 1)
369 if k <= numel(nodes{ind}.output.outputStrategy) && ~isempty(nodes{ind}.output.outputStrategy{k})
370 entry = nodes{ind}.output.outputStrategy{k};
371 self.sn.routing(ind,k) = RoutingStrategy.fromText(entry{2});
373 self.sn.routing(ind,k) = RoutingStrategy.DISABLED;
380 [If,Jf,S] = find(P{r,r});
382 if connected(If(k),Jf(k)) == 0
383 self.addLink(nodes{If(k)}, nodes{Jf(k)});
384 connected(If(k),Jf(k)) = 1;
386 nodes{If(k)}.setProbRouting(self.classes{r}, nodes{Jf(k)}, S(k));
391% Refresh sn.routing from outputStrategy for non-station nodes.
392% The initDispatcherJobClasses call above set sn.routing to DISABLED,
393% but setProbRouting has since repopulated outputStrategy for routed classes.
394if ~isempty(self.sn) && isfield(self.sn, 'routing
')
396 if ~isa(nodes{ind}, 'Station') && ind <= size(self.sn.routing, 1)
398 if isempty(nodes{ind}.output.outputStrategy{k})
399 self.sn.routing(ind,k) = RoutingStrategy.DISABLED;
401 self.sn.routing(ind,k) = RoutingStrategy.fromText(nodes{ind}.output.outputStrategy{k}{2});
408% Validate the routing probabilities.
410% Pcheck normalizes the two accepted shapes (a plain matrix for a
411% single-class model, a K x K class-pair cell otherwise) so both are checked
419% (1) Nonnegativity. This is an unconditional invariant, unlike the row-sum
420% LOWER bound left commented out below: a class that never reaches a node has
421% entries exactly zero there, never negative, so this check has no false
422% positives. It must be done separately from the row-sum test because a
423% negative entry can only LOWER a row sum and therefore passes that test
424% silently; the traffic equations then solve over a matrix that is not a
425% stochastic kernel and every mean-value solver reports the resulting visit
426% ratios without complaint.
428 for r=1:size(Pcheck,1)
429 for s=1:size(Pcheck,2)
430 if ~isempty(Pcheck{r,s})
431 [Ineg,Jneg] = find(Pcheck{r,s} < -GlobalConstants.FineTol);
433 if size(Pcheck,1) > 1 || size(Pcheck,2) > 1
434 line_error(mfilename,sprintf('Negative routing probability %g from node %s to node %s (
class %s to
class %s). Routing probabilities must be nonnegative.
', full(Pcheck{r,s}(Ineg(1),Jneg(1))), self.nodes{Ineg(1)}.name, self.nodes{Jneg(1)}.name, self.classes{r}.name, self.classes{s}.name));
436 line_error(mfilename,sprintf('Negative routing probability %g from node %s to node %s. Routing probabilities must be nonnegative.
', full(Pcheck{r,s}(Ineg(1),Jneg(1))), self.nodes{Ineg(1)}.name, self.nodes{Jneg(1)}.name));
444% (2) The total routing probability leaving a node in a given class may not
445% exceed 1. Summing over the destination j AND over the arrival class s is
446% what makes this correct under class switching, where a job leaving node i
447% in class r may arrive as any class s.
449% This replaces a cellsum(P) formulation that did not compute a row sum at
450% all: cellsum indexes its argument linearly over length(C), so on a K x K
451% cell it walked only the FIRST COLUMN, P{1,1}..P{K,1}, and compared
452% individual entries (never their sum over j) against 1.0. Its find() then
453% produced linear indices into an I x I matrix, which indexed self.nodes out
454% of range as soon as a violation occurred anywhere but the first column.
455% FORK nodes are exempt, as before: a fork legitimately emits on several
456% output links at once, so its outgoing total exceeds 1 by design.
459 % Not every node carries schedStrategy (Join does not, for one), so
460 % the FORK exemption must be guarded. The previous formulation only
461 % ever reached this property for nodes it had already flagged, which
462 % is why the gap went unnoticed.
463 % Nodes whose outgoing entries are structural indicators rather than
464 % a probability distribution are exempt. An SPN Place/Transition
465 % carries incidence arcs: spn_inhibiting sets P2->T2 and P2->T3 both
466 % to 1.0, and T1->P2, T1->P3 likewise. A Router is exempt because the
467 % established idiom declares connectivity with 1.0 entries and only
468 % then calls setRouting(class, RROBIN), AFTER link() has run, so the
469 % routing strategy is not yet knowable here.
470 if isa(self.nodes{ind},'Place
') || isa(self.nodes{ind},'Transition
') || isa(self.nodes{ind},'Router
')
473 % A fork legitimately emits on several output links at once, so its
474 % outgoing total exceeds 1 by design. Not every node carries
475 % schedStrategy (Join does not), so this must be guarded.
476 if isprop(self.nodes{ind},'schedStrategy
') && ~isempty(self.nodes{ind}.schedStrategy) && SchedStrategy.toId(self.nodes{ind}.schedStrategy) == SchedStrategy.FORK
479 for r=1:size(Pcheck,1)
481 for s=1:size(Pcheck,2)
482 if ~isempty(Pcheck{r,s})
483 pOut = pOut + sum(Pcheck{r,s}(ind,:));
486 if pOut > 1.0 + GlobalConstants.FineTol
487 if size(Pcheck,1) > 1
488 line_error(mfilename,sprintf('The total routing probability
for jobs leaving node %s in
class %s
is %g, which
is greater than 1.0.
',self.nodes{ind}.name,self.classes{r}.name,full(pOut)));
490 line_error(mfilename,sprintf('The total routing probability
for jobs leaving node %s
is %g, which
is greater than 1.0.
',self.nodes{ind}.name,full(pOut)));
494 % elseif pOut < 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
495 % if self.nodes{i}.schedStrategy ~= SchedStrategy.EXT % if not a sink
496 % 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);
502 if isa(self.nodes{ind},'Place
')
503 self.nodes{ind}.init;
507if isReset && ~isempty(self.sn) && isfield(self.sn,'rates
')
508 self.refreshChains; % without this exception with linkAndLog
511%% Check for reducible routing (absorbing states)
513 % Pass routing matrix directly to avoid getStruct() call during link()
514 [isErg, ergInfo] = self.isRoutingErgodic(self.sn.rtorig);
515 if ~isErg && ~isempty(ergInfo.absorbingStations)
516 % Build warning message
517 absNames = strjoin(ergInfo.absorbingStations, ',
');
518 line_warning(mfilename, 'Reducible network topology detected, results may be unreliable.\n
');
522%% Check that order-independent (OI) stations have a permutation-invariant rate
524 Nvec = zeros(1, self.getNumberOfClasses);
525 for r = 1:numel(Nvec)
526 if isa(self.classes{r}, 'ClosedClass
')
527 Nvec(r) = self.classes{r}.population;
529 Nvec(r) = Inf; % open classes have infinite population
533 nd = self.nodes{ind};
534 if isa(nd, 'Queue
') && SchedStrategy.toId(nd.schedStrategy) == SchedStrategy.OI ...
535 && ~isempty(nd.svcRateFun)
536 [ok, badc, partial] = nd.checkPermInvariance(Nvec, nd.cap);
538 line_error(mfilename, 'Order-independent (OI) station
''%s
'' has a service rate function that
is not permutation-invariant: mu(c) differs
for a reordering of
the microstate %s. Use SchedStrategy.PAS
for order-dependent service, or disable
this check with model.setChecks(
false).
', nd.getName(), mat2str(badc));
540 line_warning(mfilename, 'Order-independent (OI) station
''%s
'':
the permutation-invariance check was only partial because
the reachable population
is large; a subset of microstates was verified. To skip
this check, call model.setChecks(
false) before link().\n
', nd.getName());
548 % create java version of the network
550 jnetwork = JLINE.from_line_network(self);
554 % compare sn data structures
555 jsn = JLINE.from_jline_struct(jnetwork);
556 fprintf(1,'* Comparison with Java NetworkStruct:
');
557 bool = testJavaStruct(self.getName(),self.getStruct(),jsn);