LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
link.m
1function self = link(self, P)
2% SELF = LINK(P)
3
4% Copyright (c) 2012-2026, Imperial College London
5% All rights reserved.
6
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.');
9end
10
11if isa(P,'RoutingMatrix')
12 P = P.getCell();
13end
14sanitize(self);
15
16isReset = false;
17if ~isempty(self.sn)
18 isReset = true;
19 self.resetNetwork; % remove artificial class switch nodes
20end
21K = self.getNumberOfClasses;
22I = self.getNumberOfNodes;
23
24if ~iscell(P) && K>1
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.');
26end
27
28isLinearP = true;
29if size(P,1) == size(P,2)
30 for s=2:K
31 for r=1:K
32 if nnz(P{r,s})>0
33 isLinearP = false;
34 break;
35 end
36 end
37 end
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));
42 for ind=1:I
43 switch class(self.nodes{ind})
44 case 'Cache'
45 % note that since a cache needs to distinguish hits and
46 % misses, it needs to do class-switch unless the model is
47 % degenerate
48 isLinearP = false;
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');
51 end
52 end
53 end
54end
55
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}.
58if isLinearP
59 Ptmp = P;
60 P = cell(K,K);
61 for r=1:K
62 if iscell(Ptmp)
63 P{r,r} = Ptmp{r};
64 else
65 P{r,r} = Ptmp;
66 end
67 for s=1:K
68 if s~=r
69 P{r,s} = 0*Ptmp{r};
70 end
71 end
72 end
73end
74
75% assign routing for self-looping jobs
76for r=1:K
77 if isa(self.classes{r},'SelfLoopingClass')
78 for s=1:K
79 P{r,s} = 0 * P{r,s};
80 end
81 P{r,r}(self.classes{r}.refstat, self.classes{r}.refstat) = 1.0;
82 end
83end
84
85% link virtual sinks automatically to sink
86ispool = cellisa(self.nodes,'Sink');
87if sum(ispool) > 1
88 line_error(mfilename,'The model can have at most one sink node.');
89end
90
91if sum(cellisa(self.nodes,'Source')) > 1
92 line_error(mfilename,'The model can have at most one source node.');
93end
94ispool_nnz = find(ispool)';
95
96
97if ~iscell(P)
98 if K>1
99 newP = cell(1,K);
100 for r=1:K
101 newP{r} = P;
102 end
103 P = newP;
104 else %R==1
105 % single class
106 for ind=ispool_nnz
107 P((ind-1)*K+1:ind*K,:)=0;
108 end
109 Pmat = P;
110 P = cell(K,K);
111 for r=1:K
112 for s=1:K
113 P{r,s} = zeros(I);
114 for ind=1:I
115 for jnd=1:I
116 P{r,s}(ind,jnd) = Pmat((ind-1)*K+r,(jnd-1)*K+s);
117 end
118 end
119 end
120 end
121 end
122end
123
124if numel(P) == K
125 % 1 matrix per class
126 for r=1:K
127 for ind=ispool_nnz
128 P{r}((ind-1)*K+1:ind*K,:)=0;
129 end
130 end
131 Pmat = P;
132 P = cell(K,K);
133 for r=1:K
134 P{r,r} = Pmat{r};
135 for s=setdiff(1:K,r)
136 P{r,s} = zeros(I);
137 end
138 end
139end
140
141
142isemptyP = false(K,K);
143for r=1:K
144 for s=1:K
145 if isempty(P{r,s})
146 isemptyP(r,s)= true;
147 P{r,s} = zeros(I);
148 else
149 for ind=ispool_nnz
150 P{r,s}(ind,:)=0;
151 end
152 end
153 end
154end
155
156csnodematrix = cell(I,I);
157for ind=1:I
158 for jnd=1:I
159 csnodematrix{ind,jnd} = zeros(K,K);
160 end
161end
162
163for r=1:K
164 for s=1:K
165 if ~isemptyP(r,s)
166 [If,Jf] = find(P{r,s});
167 for k=1:size(If,1)
168 csnodematrix{If(k),Jf(k)}(r,s) = P{r,s}(If(k),Jf(k));
169 end
170 end
171 end
172end
173
174
175% for r=1:R
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);
179% end
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));
182% end
183% end
184
185self.sn.rtorig = P;
186
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
189for ind=1:I
190 for jnd=1:I
191 for r=1:K
192 S = sum(csnodematrix{ind,jnd}(r,:));
193 if S>0
194 csnodematrix{ind,jnd}(r,:)=csnodematrix{ind,jnd}(r,:)/S;
195 else
196 csnodematrix{ind,jnd}(r,r)=1.0;
197 end
198 end
199 end
200end
201
202csid = zeros(I);
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
206csMatrix = eye(K);
207nodeNames = self.getNodeNames;
208for ind=1:I
209 for jnd=1:I
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);
215 end
216 end
217end
218
219for ind=1:I
220 % this is to ensure that also stateful cs like caches
221 % are accounted
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;
225 end
226 for r=find(self.nodes{ind}.server.missClass)
227 csMatrix(r,self.nodes{ind}.server.missClass(r)) = 1.0;
228 end
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.');
232 end
233 csMatrix = csMatrix | self.nodes{ind}.server.csMatrix > 0.0;
234 end
235end
236
237self.csMatrix = csMatrix~=0;
238
239Ip = length(self.nodes); % number of nodes after addition of cs nodes
240
241% resize matrices
242for r=1:K
243 for s=1:K
244 P{r,s}((I+1):Ip,(I+1):Ip)=0;
245 end
246end
247
248for ind=1:I
249 for jnd=1:I
250 if csid(ind,jnd)>0
251 % re-route
252 for r=1:K
253 for s=1:K
254 if P{r,s}(ind,jnd)>0
255 P{r,r}(ind,csid(ind,jnd)) = P{r,r}(ind,csid(ind,jnd)) + P{r,s}(ind,jnd);
256 P{r,s}(ind,jnd) = 0;
257 end
258 P{s,s}(csid(ind,jnd),jnd) = 1;
259 end
260 end
261 end
262 end
263end
264
265connected = zeros(Ip);
266nodes = self.nodes;
267for r=1:K
268 [If,Jf,S] = find(P{r,r});
269 for k=1:length(If)
270 if connected(If(k),Jf(k)) == 0
271 self.addLink(nodes{If(k)}, nodes{Jf(k)});
272 connected(If(k),Jf(k)) = 1;
273 end
274 nodes{If(k)}.setProbRouting(self.classes{r}, nodes{Jf(k)}, S(k));
275 end
276end
277self.nodes = nodes;
278
279% check if the probability out of any node sums to >1.0
280pSum = cellsum(P);
281isAboveOne = pSum > 1.0 + GlobalConstants.FineTol;
282if any(isAboveOne)
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));
286 end
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);
290 % end
291 end
292end
293
294for ind=1:I
295 if isa(self.nodes{ind},'Place')
296 self.nodes{ind}.init;
297 end
298end
299
300if isReset && ~isempty(self.sn) && isfield(self.sn,'rates')
301 self.refreshChains; % without this exception with linkAndLog
302end
303
304%% Check for reducible routing (absorbing states)
305if self.enableChecks
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');
312 end
313end
314
315%% DEBUGGING
316if false
317 % create java version of the network
318 if isempty(self.obj)
319 jnetwork = JLINE.from_line_network(self);
320 else
321 jnetwork = self.obj;
322 end
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);
327end
328
329end
Definition mmt.m:92