LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
solver_qns.m
1function [QN,UN,RN,TN,CN,XN,actualMethod] = solver_qns(sn, options)
2% [Q,U,R,T,C,X] = SOLVER_QNS(QN, OPTIONS)
3
4% Copyright (c) 2012-2026, Imperial College London
5% All rights reserved.
6
7M = sn.nstations; % number of stations
8K = sn.nclasses; % number of classes
9
10QN = zeros(M,K);
11UN = zeros(M,K);
12RN = zeros(M,K);
13TN = zeros(M,K);
14CN = zeros(1,K);
15XN = zeros(1,K);
16
17filePath = lineTempName('qns');
18fileName = 'model';
19fname = [fileName,'.jmva'];
20logFileName = 'console';
21logfname = [logFileName,'.out'];
22outputFileName = [filePath,filesep,fname];
23outputFileName = SolverJMT.writeJMVA(sn, outputFileName, options);
24fileName = 'result';
25ofname = [fileName,'.jmva'];
26resultFileName = [filePath,filesep,ofname];
27%stationames={sn.nodenames{sn.isstation}};
28
29% Track the actual method that will be used
30actualMethod = options.method;
31
32line_debug('QNS solver starting: nstations=%d, nclasses=%d, method=%s', M, K, options.method);
33
34switch options.method
35 case 'conway'
36 options.config.multiserver='conway';
37 case 'reiser'
38 options.config.multiserver='reiser';
39 case 'rolia'
40 options.config.multiserver='rolia';
41 case 'zhou'
42 options.config.multiserver='zhou';
43end
44if any(sn.nservers>1 & sn.nservers<Inf)
45 line_debug('Multi-server queues detected, selecting multiserver method');
46 switch options.config.multiserver
47 case {'default','conway'}
48 if strcmpi(options.config.multiserver, 'default')
49 line_debug('Default method: using Conway multiserver approximation\n');
50 end
51 line_debug('Using Conway multiserver approximation');
52 if ispc
53 cmd=['qnsolver -l ',outputFileName,' -mconway -o ',resultFileName,' > ',logfname];
54 else
55 cmd=['qnsolver -l ',outputFileName,' -mconway -o ',resultFileName,' > ',logfname,' 2>&1'];
56 end
57 actualMethod = 'conway';
58 case 'reiser'
59 line_debug('Using Reiser multiserver approximation');
60 if ispc
61 cmd=['qnsolver -l ',outputFileName,' -mreiser -o ',resultFileName,' > ',logfname];
62 else
63 cmd=['qnsolver -l ',outputFileName,' -mreiser -o ',resultFileName,' > ',logfname,' 2>&1'];
64 end
65 actualMethod = 'reiser';
66 case 'rolia'
67 line_debug('Using Rolia multiserver approximation');
68 if ispc
69 cmd=['qnsolver -l ',outputFileName,' -mrolia -o ',resultFileName,' > ',logfname];
70 else
71 cmd=['qnsolver -l ',outputFileName,' -mrolia -o ',resultFileName,' > ',logfname,' 2>&1'];
72 end
73 actualMethod = 'rolia';
74 case 'zhou'
75 line_debug('Using Zhou multiserver approximation');
76 if ispc
77 cmd=['qnsolver -l ',outputFileName,' -mzhou -o ',resultFileName,' > ',logfname];
78 else
79 cmd=['qnsolver -l ',outputFileName,' -mzhou -o ',resultFileName,' > ',logfname,' 2>&1'];
80 end
81 actualMethod = 'zhou';
82 end
83else
84 if ispc
85 cmd=['qnsolver -l ',outputFileName,' -o ',resultFileName,' > ',logfname];
86 else
87 cmd=['qnsolver -l ',outputFileName,' -o ',resultFileName,' > ',logfname,' 2>&1'];
88 end
89end
90
91if GlobalConstants.Verbose == VerboseLevel.DEBUG
92 line_printf('SolverQNS command:\n');
93 disp(cmd)
94end
95system(cmd);
96name = cell(sn.nstations*(sn.nchains+1),0);
97Uchain = zeros(0,sn.nchains);
98Qchain = zeros(0,sn.nchains);
99Wchain = zeros(0,sn.nchains);
100Tchain = zeros(0,sn.nchains);
101[Lchain,STchain,Vchain,alpha,~,~,~] = sn_get_demands_chain(sn);
102%Lchain = zeros(sn.nstations,sn.nchains); % uncomment for starred output
103i = 1;
104statName = {};
105try
106 fid=fopen(resultFileName,'r');
107 strline = 1;
108 while strline>0
109 strline = fgetl(fid);
110 if sn.nclasses==1
111 [Uchain, Qchain, Wchain, Tchain, statlabel] = parse_dollar_output_singleclass(strline, Uchain, Qchain, Wchain, Tchain);
112 else
113 [Uchain, Qchain, Wchain, Tchain, statlabel] = parse_dollar_output(strline, Uchain, Qchain, Wchain, Tchain);
114 end
115 if ~isempty(statlabel)
116 statName{end+1} = statlabel;
117 end
118 %[Lchain, Uchain, Qchain, Wchain, Tchain] = parse_starred_output(strline, Lchain, Uchain, Qchain, Wchain, Tchain);
119 end
120 fclose(fid);
121catch
122 line_warning(mfilename,'Failed execution: cannot open the qnsolver output file at: ');
123 line_warning(mfilename,resultFileName)
124 QN = nan(M,K);
125 UN = nan(M,K);
126 RN = nan(M,K);
127 TN = nan(M,K);
128 CN = nan(1,K);
129 XN = nan(1,K);
130 return
131end
132
133% Build reorder mapping: maps model station index to qnsolver output row
134% Initialize with zeros (will remain 0 for stations not found in qnsolver output)
135stationToOutputRow = zeros(1, sn.nnodes);
136for i=1:length(statName)
137 idx = find(cellfun(@(x)strcmp(x,statName{i}),sn.nodenames));
138 if ~isempty(idx)
139 stationToOutputRow(idx) = i;
140 end
141end
142
143% Create reordered arrays for stations only
144% Stations are the first sn.nstations nodes
145UchainReordered = zeros(sn.nstations, sn.nchains);
146QchainReordered = zeros(sn.nstations, sn.nchains);
147WchainReordered = zeros(sn.nstations, sn.nchains);
148TchainReordered = zeros(sn.nstations, sn.nchains);
149
150for i=1:sn.nstations
151 outputRow = stationToOutputRow(i);
152 if outputRow > 0
153 UchainReordered(i,:) = Uchain(outputRow,:);
154 QchainReordered(i,:) = Qchain(outputRow,:);
155 WchainReordered(i,:) = Wchain(outputRow,:);
156 TchainReordered(i,:) = Tchain(outputRow,:);
157 end
158end
159
160Uchain = UchainReordered;
161Qchain = QchainReordered;
162Wchain = WchainReordered;
163Tchain = TchainReordered;
164
165ref= zeros(sn.nchains,1);
166for c=1:sn.nchains
167 chain = find(sn.chains(c,:));
168 Xchain(c)=Tchain(sn.refstat(c),c);
169end
170%Rchain=Wchain./Vchain; % needs to be reinstated for starred output
171Rchain=Wchain;
172Rchain(isnan(Rchain))=0;
173for i=1:sn.nstations
174 if ~isinf(sn.nservers(i))
175 Uchain(i,:) = Uchain(i,:) / sn.nservers(i);
176 end
177end
178
179[QN,UN,RN,TN,CN,XN] = sn_deaggregate_chain_results(sn, Lchain, [], STchain, Vchain, alpha, [], [], Rchain, Tchain, [], Xchain);
180end
181
182function [Uchain, Qchain, Wchain, Tchain, statName] = parse_dollar_output(strline, Uchain, Qchain, Wchain, Tchain)
183statName = {};
184if any(find(strline==','))
185 if any(find(strline=='$'))
186 % skip
187 else
188 R = size(Uchain,2);
189 %strline
190 str=strrep(strline,' ','');
191 str=strsplit(str,',');
192 Uchain(end+1,1:R) = 0;
193 Qchain(end+1,1:R) = 0;
194 Wchain(end+1,1:R) = 0;
195 Tchain(end+1,1:R) = 0;
196 ptr = 1;
197 statName{end+1} = str{1};
198 for r=1:R
199 Qchain(end,r)=str2num(str{ptr+r});
200 end
201 ptr = ptr + 1 + R; % skip aggregate value
202 for r=1:R
203 Wchain(end,r)=str2num(str{ptr+r});
204 end
205 ptr = ptr + 1 + R; % skip aggregate value
206 for r=1:R
207 Uchain(end,r)=str2num(str{ptr+r});
208 end
209 ptr = ptr + 1 + R; % skip aggregate value
210 for r=1:R
211 Tchain(end,r)=str2num(str{ptr+r});
212 end
213 end
214end
215%Station, $Q(Chain01), $Q(Chain02), $Q, $R(Chain01), $R(Chain02), $R, $U(Chain01), $U(Chain02), $U, $X(Chain01), $X(Chain02), $X
216%Delay1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2.63033, 7.40631, 10.0366
217%Queue1, 4, 4, 8, 1.52072, 0.54008, 0.797079, 1, 1, 2, 2.63033, 7.40631, 10.0366
218end
219
220function [Uchain, Qchain, Wchain, Tchain, statName] = parse_dollar_output_singleclass(strline, Uchain, Qchain, Wchain, Tchain)
221statName = {};
222if any(find(strline==','))
223 if any(find(strline=='$'))
224 % skip
225 else
226 R = size(Uchain,2);
227 %strline
228 str=strrep(strline,' ','');
229 str=strsplit(str,',');
230 Uchain(end+1,1:R) = 0;
231 Qchain(end+1,1:R) = 0;
232 Wchain(end+1,1:R) = 0;
233 Tchain(end+1,1:R) = 0;
234 ptr = 1;
235 statName{end+1} = str{1};
236 for r=1:R
237 Qchain(end,r)=str2num(str{ptr+r});
238 end
239 ptr = ptr + 1;
240 for r=1:R
241 Wchain(end,r)=str2num(str{ptr+r});
242 end
243 ptr = ptr + 1;
244 for r=1:R
245 Uchain(end,r)=str2num(str{ptr+r});
246 end
247 ptr = ptr + 1;
248 for r=1:R
249 Tchain(end,r)=str2num(str{ptr+r});
250 end
251 end
252end
253%Station, $Q(Chain01), $Q(Chain02), $Q, $R(Chain01), $R(Chain02), $R, $U(Chain01), $U(Chain02), $U, $X(Chain01), $X(Chain02), $X
254%Delay1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2.63033, 7.40631, 10.0366
255%Queue1, 4, 4, 8, 1.52072, 0.54008, 0.797079, 1, 1, 2, 2.63033, 7.40631, 10.0366
256end
257
258function [Lchain, Uchain, Qchain, Wchain, Tchain] = parse_starred_output(strline, Lchain, Uchain, Qchain, Wchain, Tchain)
259if any(find(strline=='.'))
260 str=strline(2:end);
261 str=strrep(str,' ','');
262 %[name,service,busypct,custnb,response,thruput]
263 str=strsplit(str,'*');
264 name = str{1};
265 if name(1)=='('
266 chainnum = strrep(name,'(Chain','');
267 chainnum = str2num(strrep(chainnum,')',''));
268 Lchain(i,chainnum) = str2num(str{2});
269 Uchain(i,chainnum) = str2num(str{3});
270 Qchain(i,chainnum) = str2num(str{4});
271 Wchain(i,chainnum) = str2num(str{5});
272 Tchain(i,chainnum) = str2num(str{6});
273 else
274 i=find(cellfun(@any,strfind(stationames,name)));
275 end
276end
277end
Definition mmt.m:92