1function [QN,UN,RN,TN,CN,XN,actualMethod] = solver_qns(sn, options)
2% [Q,U,R,T,C,X] = SOLVER_QNS(QN, OPTIONS)
4% Copyright (c) 2012-2026, Imperial College London
7M = sn.nstations; % number of stations
8K = sn.nclasses; % number of
classes
17filePath = lineTempName(
'qns');
19fname = [fileName,
'.jmva'];
20logFileName =
'console';
21logfname = [logFileName,
'.out'];
22outputFileName = [filePath,filesep,fname];
23outputFileName = SolverJMT.writeJMVA(sn, outputFileName, options);
25ofname = [fileName,
'.jmva'];
26resultFileName = [filePath,filesep,ofname];
27%stationames={sn.nodenames{sn.isstation}};
29% Track the actual method that will be used
30actualMethod = options.method;
32line_debug(
'QNS solver starting: nstations=%d, nclasses=%d, method=%s', M, K, options.method);
36 options.config.multiserver=
'conway';
38 options.config.multiserver=
'reiser';
40 options.config.multiserver=
'rolia';
42 options.config.multiserver=
'zhou';
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');
51 line_debug('Using Conway multiserver approximation');
53 cmd=['qnsolver -l ',outputFileName,' -mconway -o ',resultFileName,' > ',logfname];
55 cmd=['qnsolver -l ',outputFileName,' -mconway -o ',resultFileName,' > ',logfname,' 2>&1'];
57 actualMethod = 'conway';
59 line_debug('Using Reiser multiserver approximation');
61 cmd=['qnsolver -l ',outputFileName,' -mreiser -o ',resultFileName,' > ',logfname];
63 cmd=['qnsolver -l ',outputFileName,' -mreiser -o ',resultFileName,' > ',logfname,' 2>&1'];
65 actualMethod = 'reiser';
67 line_debug('Using Rolia multiserver approximation');
69 cmd=['qnsolver -l ',outputFileName,' -mrolia -o ',resultFileName,' > ',logfname];
71 cmd=['qnsolver -l ',outputFileName,' -mrolia -o ',resultFileName,' > ',logfname,' 2>&1'];
73 actualMethod = 'rolia';
75 line_debug('Using Zhou multiserver approximation');
77 cmd=['qnsolver -l ',outputFileName,' -mzhou -o ',resultFileName,' > ',logfname];
79 cmd=['qnsolver -l ',outputFileName,' -mzhou -o ',resultFileName,' > ',logfname,' 2>&1'];
81 actualMethod = 'zhou';
85 cmd=['qnsolver -l ',outputFileName,' -o ',resultFileName,' > ',logfname];
87 cmd=['qnsolver -l ',outputFileName,' -o ',resultFileName,' > ',logfname,' 2>&1'];
91% MATLAB prepends its bundled runtime libraries to LD_LIBRARY_PATH before
92% spawning child processes. Its libstdc++ (e.g. GLIBCXX_3.4.28 in R2024a)
is
93% older than the GLIBCXX_3.4.29+ required by qnsolver's liblqio/liblqx
94% dependencies, so qnsolver aborts before writing its result file and the
95% parse below would then silently yield all-zero metrics. Strip
96% LD_LIBRARY_PATH for the child so it resolves the system libstdc++ (the same
97% fix applied to lqns/lqsim in SolverLQNS/runAnalyzer.m).
99 cmd = ['env -u LD_LIBRARY_PATH ', cmd];
101if GlobalConstants.Verbose == VerboseLevel.DEBUG
102 line_printf('SolverQNS command:\n');
106name = cell(sn.nstations*(sn.nchains+1),0);
107Uchain = zeros(0,sn.nchains);
108Qchain = zeros(0,sn.nchains);
109Wchain = zeros(0,sn.nchains);
110Tchain = zeros(0,sn.nchains);
111[Lchain,STchain,Vchain,alpha,~,~,~] = sn_get_demands_chain(sn);
112%Lchain = zeros(sn.nstations,sn.nchains); % uncomment for starred output
116 fid=fopen(resultFileName,
'r');
119 strline = fgetl(fid);
121 [Uchain, Qchain, Wchain, Tchain, statlabel] = parse_dollar_output_singleclass(strline, Uchain, Qchain, Wchain, Tchain);
123 [Uchain, Qchain, Wchain, Tchain, statlabel] = parse_dollar_output(strline, Uchain, Qchain, Wchain, Tchain);
125 if ~isempty(statlabel)
126 statName{end+1} = statlabel;
128 %[Lchain, Uchain, Qchain, Wchain, Tchain] = parse_starred_output(strline, Lchain, Uchain, Qchain, Wchain, Tchain);
132 line_warning(mfilename,
'Failed execution: cannot open the qnsolver output file at: ');
133 line_warning(mfilename,resultFileName)
143% Build reorder mapping: maps model station index to qnsolver output row
144% Initialize with zeros (will remain 0
for stations not found in qnsolver output)
145stationToOutputRow = zeros(1, sn.nnodes);
146for i=1:length(statName)
147 idx = find(cellfun(@(x)strcmp(x,statName{i}),sn.nodenames));
149 stationToOutputRow(idx) = i;
153% Create reordered arrays
for stations only
154% Stations are the first sn.nstations
nodes
155UchainReordered = zeros(sn.nstations, sn.nchains);
156QchainReordered = zeros(sn.nstations, sn.nchains);
157WchainReordered = zeros(sn.nstations, sn.nchains);
158TchainReordered = zeros(sn.nstations, sn.nchains);
161 outputRow = stationToOutputRow(i);
163 UchainReordered(i,:) = Uchain(outputRow,:);
164 QchainReordered(i,:) = Qchain(outputRow,:);
165 WchainReordered(i,:) = Wchain(outputRow,:);
166 TchainReordered(i,:) = Tchain(outputRow,:);
170Uchain = UchainReordered;
171Qchain = QchainReordered;
172Wchain = WchainReordered;
173Tchain = TchainReordered;
175ref= zeros(sn.nchains,1);
177 chain = find(sn.chains(c,:));
178 Xchain(c)=Tchain(sn.refstat(c),c);
180 % For open chains where refstat
is Source (not in qnsolver output),
181 % recover Xchain from any station with valid throughput:
182 % Xchain(c) = Tchain(i,c) / Vchain(i,c)
184 if Vchain(i,c) > 0 && Tchain(i,c) > 0
185 Xchain(c) = Tchain(i,c) / Vchain(i,c);
191%Rchain=Wchain./Vchain; % needs to be reinstated
for starred output
193Rchain(isnan(Rchain))=0;
195 if ~isinf(sn.nservers(i))
196 Uchain(i,:) = Uchain(i,:) / sn.nservers(i);
200[QN,UN,RN,TN,CN,XN] = sn_deaggregate_chain_results(sn, Lchain, [], STchain, Vchain, alpha, Qchain, Uchain, Rchain, Tchain, [], Xchain);
203function [Uchain, Qchain, Wchain, Tchain, statName] = parse_dollar_output(strline, Uchain, Qchain, Wchain, Tchain)
205if any(find(strline==
','))
206 if any(find(strline=='$'))
211 str=strrep(strline,' ','');
212 str=strsplit(str,',');
213 Uchain(end+1,1:R) = 0;
214 Qchain(end+1,1:R) = 0;
215 Wchain(end+1,1:R) = 0;
216 Tchain(end+1,1:R) = 0;
218 statName{end+1} = str{1};
220 Qchain(end,r)=str2num(str{ptr+r});
222 ptr = ptr + 1 + R; % skip aggregate value
224 Wchain(end,r)=str2num(str{ptr+r});
226 ptr = ptr + 1 + R; % skip aggregate value
228 Uchain(end,r)=str2num(str{ptr+r});
230 ptr = ptr + 1 + R; % skip aggregate value
232 Tchain(end,r)=str2num(str{ptr+r});
236%Station, $Q(Chain01), $Q(Chain02), $Q, $R(Chain01), $R(Chain02), $R, $U(Chain01), $U(Chain02), $U, $X(Chain01), $X(Chain02), $X
237%Delay1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2.63033, 7.40631, 10.0366
238%Queue1, 4, 4, 8, 1.52072, 0.54008, 0.797079, 1, 1, 2, 2.63033, 7.40631, 10.0366
241function [Uchain, Qchain, Wchain, Tchain, statName] = parse_dollar_output_singleclass(strline, Uchain, Qchain, Wchain, Tchain)
243if any(find(strline==
','))
244 if any(find(strline=='$'))
249 str=strrep(strline,' ','');
250 str=strsplit(str,',');
251 Uchain(end+1,1:R) = 0;
252 Qchain(end+1,1:R) = 0;
253 Wchain(end+1,1:R) = 0;
254 Tchain(end+1,1:R) = 0;
256 statName{end+1} = str{1};
258 Qchain(end,r)=str2num(str{ptr+r});
262 Wchain(end,r)=str2num(str{ptr+r});
266 Uchain(end,r)=str2num(str{ptr+r});
270 Tchain(end,r)=str2num(str{ptr+r});
274%Station, $Q(Chain01), $Q(Chain02), $Q, $R(Chain01), $R(Chain02), $R, $U(Chain01), $U(Chain02), $U, $X(Chain01), $X(Chain02), $X
275%Delay1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2.63033, 7.40631, 10.0366
276%Queue1, 4, 4, 8, 1.52072, 0.54008, 0.797079, 1, 1, 2, 2.63033, 7.40631, 10.0366
279function [Lchain, Uchain, Qchain, Wchain, Tchain] = parse_starred_output(strline, Lchain, Uchain, Qchain, Wchain, Tchain)
280if any(find(strline==
'.'))
282 str=strrep(str,' ','');
283 %[name,service,busypct,custnb,response,thruput]
284 str=strsplit(str,'*');
287 chainnum = strrep(name,
'(Chain',
'');
288 chainnum = str2num(strrep(chainnum,
')',
''));
289 Lchain(i,chainnum) = str2num(str{2});
290 Uchain(i,chainnum) = str2num(str{3});
291 Qchain(i,chainnum) = str2num(str{4});
292 Wchain(i,chainnum) = str2num(str{5});
293 Tchain(i,chainnum) = str2num(str{6});
295 i=find(cellfun(@any,strfind(stationames,name)));