LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
solver_mam.m
1function [QN,UN,RN,TN,CN,XN,totiter,method,runtime] = solver_mam(sn, options)
2%[Q,U,R,T,C,X,totiter] = SOLVER_MAM(QN, PH, OPTIONS)
3
4%Copyright (c) 2012-2026, Imperial College London
5%All rights reserved.
6
7method = options.method;
8config = options.config;
9totiter = NaN;
10PH = sn.proc;
11I = sn.nnodes;
12M = sn.nstations;
13K = sn.nclasses;
14C = sn.nchains;
15N = sn.njobs';
16V = cellsum(sn.visits);
17Tstart=tic;
18QN = zeros(M,K);
19UN = zeros(M,K);
20RN = zeros(M,K);
21TN = zeros(M,K);
22CN = zeros(1,K);
23XN = zeros(1,K);
24
25lambda = zeros(1,K);
26for c=1:C
27 inchain = sn.inchain{c};
28 lambdas_inchain = sn.rates(sn.refstat(inchain(1)),inchain);
29 lambdas_inchain = lambdas_inchain(isfinite(lambdas_inchain));
30 lambda(inchain) = sum(lambdas_inchain);
31end
32
33chain = zeros(1,K);
34for k=1:K
35 chain(k) = find(sn.chains(:,k));
36end
37
38for ist=1:sn.nstations
39 switch sn.sched(ist)
40 case SchedStrategy.EXT
41 % no-op
42 case {SchedStrategy.FCFS, SchedStrategy.HOL, SchedStrategy.FCFSPRPRIO, SchedStrategy.PS}
43 % no-op
44 otherwise
45 if options.verbose
46 line_warning(mfilename,'The dec.mmap method does not support this scheduling strategy.\n');
47 end
48 [QN,UN,RN,TN,CN,XN] = deal([],[],[],[],[],[]);
49 totiter = 0;
50 method = '';
51 runtime = toc(Tstart);
52 return
53 end
54end
55
56if all(isinf(sn.njobs)) % is open
57 % open queueing system (one node is the external world)
58 pie = {};
59 D0 = {};
60 for ist=1:M
61 switch sn.sched(ist)
62 case SchedStrategy.EXT
63 TN(ist,:) = sn.rates(ist,:);
64 TN(ist,isnan(TN(ist,:)))=0;
65 case {SchedStrategy.FCFS, SchedStrategy.HOL, SchedStrategy.FCFSPRPRIO, SchedStrategy.PS}
66 for k=1:K
67 % divide service time by number of servers and put
68 % later a surrogate delay server in tandem to compensate
69 PH{ist}{k} = map_scale(PH{ist}{k}, map_mean(PH{ist}{k})/sn.nservers(ist));
70 pie{ist}{k} = map_pie(PH{ist}{k});
71 D0{ist,k} = PH{ist}{k}{1};
72 if any(isnan(D0{ist,k}))
73 D0{ist,k} = -GlobalConstants.Immediate;
74 pie{ist}{k} = 1;
75 PH{ist}{k} = map_exponential(GlobalConstants.Immediate);
76 end
77 end
78 end
79 end
80
81 % departure-process fixed point (parametric decomposition), driven on
82 % the station queue lengths by the generic DA driver
83 DEP = {};
84 fpopts = options;
85 fpopts.config.da_miniter = 3; % legacy loop tested convergence only from the third sweep
86 fpopts.config.da_norm = @(xn,xr) max(abs(xn(:)-xr(:))./xr(:)); % relative difference
87 [~, totiter] = da_fpi(@mam_dec_sweep, QN, fpopts);
88 if options.verbose
89 line_printf('\nMAM parametric decomposition completed in %d iterations.',totiter);
90 end
91else
92 if options.verbose
93 line_warning(mfilename,'This model is not supported by SolverMAM yet. Returning with no result.\n');
94 end
95end
96runtime = toc(Tstart);
97
98 function [xnew, xref] = mam_dec_sweep(~, itnum)
99 %it
100 % now estimate arrival processes
101 if itnum == 1
102 % initially form departure processes using scaled service; DEP/PH/V
103 % are STATION-indexed; see _kb/06-solver-catalog.md for rationale
104 DEP = PH;
105 for ist=1:M
106 for r=1:K
107 DEP{ist,r} = map_scale(PH{ist}{r}, 1 / (lambda(r) * V(ist,r)) );
108 end
109 end
110 end
111
112 ARV = solver_mam_traffic(sn, DEP, config);
113
114 xref = QN;
115 for ist=1:M
116 ind = sn.stationToNode(ist);
117 finiteCapUsed = false;
118 switch sn.nodetype(ind)
119 case NodeType.Queue
120 if length(ARV{ind}{1}) > config.space_max
121 line_printf('\nArrival process at node %d is now at %d states. Compressing.',ind,length(ARV{ind}{1}));
122 ARV{ind} = mmap_compress(ARV{ind});
123 end
124 TN(ist,:) = mmap_lambda(ARV{ind});
125 switch sn.sched(ist)
126 case {SchedStrategy.FCFS, SchedStrategy.HOL, SchedStrategy.FCFSPRPRIO}
127 isFiniteCap = isfinite(sn.cap(ist));
128 if isFiniteCap
129 capK = sn.cap(ist);
130 [isMmck, muMmck] = mam_detect_mmck(sn, ist, K, ARV{ind});
131 if isMmck
132 aggrLambda_ist = sum(TN(ist,:), 'omitnan');
133 exactRes = qsys_mmck(aggrLambda_ist, muMmck, sn.nservers(ist), capK);
134 meanQ_fc = exactRes.meanQueueLength;
135 lossProb_fc = exactRes.lossProbability;
136 else
137 [meanQ_fc, lossProb_fc, ~] = mam_truncate_renorm( ...
138 {ARV{ind}{[1,3:end]}}, {pie{ist}{:}}, {D0{ist,:}}, capK);
139 end
140 lambdaInflow = TN(ist,:); % per-class rates from mmap_lambda
141 lambdaInflow(isnan(lambdaInflow)) = 0;
142 TN_eff = lambdaInflow * (1 - lossProb_fc);
143 sumTN = sum(TN_eff);
144 % Actual per-class service mean (PH was scaled by 1/c)
145 S_actual = zeros(1, K);
146 for k=1:K
147 S_actual(k) = map_mean(PH{ist}{k}) * sn.nservers(ist);
148 end
149 if sumTN > 0
150 Savg_eff = sum(TN_eff .* S_actual, 'omitnan') / sumTN;
151 Wq = max(0, meanQ_fc / sumTN - Savg_eff);
152 else
153 Wq = 0;
154 end
155 for k=1:K
156 TN(ist,k) = TN_eff(k);
157 UN(ist,k) = TN(ist,k) * map_mean(PH{ist}{k});
158 if TN(ist,k) > 0
159 RN(ist,k) = Wq + S_actual(k);
160 QN(ist,k) = TN(ist,k) * RN(ist,k);
161 else
162 RN(ist,k) = 0;
163 QN(ist,k) = 0;
164 end
165 end
166 finiteCapUsed = true;
167 else
168 [Qret{1:K}, ~] = MMAPPH1FCFS({ARV{ind}{[1,3:end]}}, {pie{ist}{:}}, {D0{ist,:}}, 'ncMoms', 1, 'ncDistr',2);
169 for k=1:K
170 QN(ist,k) = sum(Qret{k});
171 end
172 end
173 case SchedStrategy.PS
174 for k=1:K
175 UN(ist,k) = TN(ist,k) * map_mean(PH{ist}{k});
176 end
177 Uden = min([1-GlobalConstants.FineTol, sum(UN(ist,:))]);
178 for k=1:K
179 QN(ist,k) = UN(ist,k)/(1-Uden);
180 end
181 end
182 end
183 if ~finiteCapUsed
184 for k=1:K
185 UN(ist,k) = TN(ist,k) * map_mean(PH{ist}{k});
186 %add number of jobs at the surrogate delay server
187 QN(ist,k) = QN(ist,k) + TN(ist,k)*(map_mean(PH{ist}{k})*sn.nservers(ist)) * (sn.nservers(ist)-1)/sn.nservers(ist);
188 RN(ist,k) = QN(ist,k) ./ TN(ist,k);
189 end
190 end
191 end
192
193 for ist=1:M
194 ind = sn.stationToNode(ist);
195 switch sn.nodetype(ind)
196 case NodeType.Queue
197 for r=1:K
198 % extract class-r arrival MAP
199 A = mmap_hide(ARV{ind},setdiff(1:K,r));
200 S = PH{ist}{r};
201 na = length(A{1});
202 ns = length(S{1});
203 etaqa_n = config.etaqa_trunc;
204 etaqa_sz = (etaqa_n+1)*na*ns;
205 rho = sum(UN(ist,:));
206 % use ETAQA if state space is manageable and queue is stable
207 if etaqa_sz <= config.space_max && rho < 1-GlobalConstants.FineTol
208 try
209 switch sn.sched(ist)
210 case {SchedStrategy.FCFS, SchedStrategy.HOL, SchedStrategy.FCFSPRPRIO}
211 DEP{ist,r} = qbd_depproc_etaqa(A, S, etaqa_n);
212 case SchedStrategy.PS
213 DEP{ist,r} = qbd_depproc_etaqa_ps(A, S, etaqa_n);
214 end
215 DEP{ist,r} = map_normalize(DEP{ist,r});
216 catch
217 % fall back to scaled service on ETAQA failure
218 DEP{ist,r} = PH{ist}{r};
219 end
220 else
221 DEP{ist,r} = PH{ist}{r};
222 end
223 DEP{ist,r} = map_scale(DEP{ist,r}, 1 / (lambda(r) * V(ist,r)) );
224 SCVd(ist,r) = map_scv(DEP{ist,r});
225 IDCd(ist,r) = map_idc(DEP{ist,r});
226 end
227 end
228 end
229 xnew = QN;
230 end
231end