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
91% see _kb/06-solver-catalog.md (Wrappers: LQNS/lqsim/qnsolver LD_LIBRARY_PATH GLIBCXX strip)
92if isunix
93 cmd = ['env -u LD_LIBRARY_PATH ', cmd];
94end
95if GlobalConstants.Verbose == VerboseLevel.DEBUG
96 line_printf('SolverQNS command:\n');
97 disp(cmd)
98end
99system(cmd);
100name = cell(sn.nstations*(sn.nchains+1),0);
101Uchain = zeros(0,sn.nchains);
102Qchain = zeros(0,sn.nchains);
103Wchain = zeros(0,sn.nchains);
104Tchain = zeros(0,sn.nchains);
105[Lchain,STchain,Vchain,alpha,~,~,~] = sn_get_demands_chain(sn);
106%Lchain = zeros(sn.nstations,sn.nchains); % uncomment for starred output
107i = 1;
108statName = {};
109try
110 fid=fopen(resultFileName,'r');
111 strline = 1;
112 while strline>0
113 strline = fgetl(fid);
114 if sn.nclasses==1
115 [Uchain, Qchain, Wchain, Tchain, statlabel] = parse_dollar_output_singleclass(strline, Uchain, Qchain, Wchain, Tchain);
116 else
117 [Uchain, Qchain, Wchain, Tchain, statlabel] = parse_dollar_output(strline, Uchain, Qchain, Wchain, Tchain);
118 end
119 if ~isempty(statlabel)
120 statName{end+1} = statlabel;
121 end
122 %[Lchain, Uchain, Qchain, Wchain, Tchain] = parse_starred_output(strline, Lchain, Uchain, Qchain, Wchain, Tchain);
123 end
124 fclose(fid);
125catch
126 line_warning(mfilename,'Failed execution: cannot open the qnsolver output file at: ');
127 line_warning(mfilename,resultFileName)
128 QN = nan(M,K);
129 UN = nan(M,K);
130 RN = nan(M,K);
131 TN = nan(M,K);
132 CN = nan(1,K);
133 XN = nan(1,K);
134 return
135end
136
137% Build reorder mapping: maps model station index to qnsolver output row
138% Initialize with zeros (will remain 0 for stations not found in qnsolver output)
139stationToOutputRow = zeros(1, sn.nnodes);
140for i=1:length(statName)
141 idx = find(cellfun(@(x)strcmp(x,statName{i}),sn.nodenames));
142 if ~isempty(idx)
143 stationToOutputRow(idx) = i;
144 end
145end
146
147% Create reordered arrays for stations only
148% Stations are the first sn.nstations nodes
149UchainReordered = zeros(sn.nstations, sn.nchains);
150QchainReordered = zeros(sn.nstations, sn.nchains);
151WchainReordered = zeros(sn.nstations, sn.nchains);
152TchainReordered = zeros(sn.nstations, sn.nchains);
153
154for i=1:sn.nstations
155 outputRow = stationToOutputRow(i);
156 if outputRow > 0
157 UchainReordered(i,:) = Uchain(outputRow,:);
158 QchainReordered(i,:) = Qchain(outputRow,:);
159 WchainReordered(i,:) = Wchain(outputRow,:);
160 TchainReordered(i,:) = Tchain(outputRow,:);
161 end
162end
163
164Uchain = UchainReordered;
165Qchain = QchainReordered;
166Wchain = WchainReordered;
167Tchain = TchainReordered;
168
169ref= zeros(sn.nchains,1);
170for c=1:sn.nchains
171 chain = find(sn.chains(c,:));
172 Xchain(c)=Tchain(sn.refstat(c),c);
173 if Xchain(c) == 0
174 % For open chains where refstat is Source (not in qnsolver output),
175 % recover Xchain from any station with valid throughput:
176 % Xchain(c) = Tchain(i,c) / Vchain(i,c)
177 for i=1:sn.nstations
178 if Vchain(i,c) > 0 && Tchain(i,c) > 0
179 Xchain(c) = Tchain(i,c) / Vchain(i,c);
180 break;
181 end
182 end
183 end
184end
185%Rchain=Wchain./Vchain; % needs to be reinstated for starred output
186Rchain=Wchain;
187Rchain(isnan(Rchain))=0;
188for i=1:sn.nstations
189 if ~isinf(sn.nservers(i))
190 Uchain(i,:) = Uchain(i,:) / sn.nservers(i);
191 end
192end
193
194[QN,UN,RN,TN,CN,XN] = sn_deaggregate_chain_results(sn, Lchain, [], STchain, Vchain, alpha, Qchain, Uchain, Rchain, Tchain, [], Xchain);
195end
196
197function [Uchain, Qchain, Wchain, Tchain, statName] = parse_dollar_output(strline, Uchain, Qchain, Wchain, Tchain)
198statName = {};
199if any(find(strline==','))
200 if any(find(strline=='$'))
201 % skip
202 else
203 R = size(Uchain,2);
204 %strline
205 str=strrep(strline,' ','');
206 str=strsplit(str,',');
207 Uchain(end+1,1:R) = 0;
208 Qchain(end+1,1:R) = 0;
209 Wchain(end+1,1:R) = 0;
210 Tchain(end+1,1:R) = 0;
211 ptr = 1;
212 statName{end+1} = str{1};
213 for r=1:R
214 Qchain(end,r)=str2num(str{ptr+r});
215 end
216 ptr = ptr + 1 + R; % skip aggregate value
217 for r=1:R
218 Wchain(end,r)=str2num(str{ptr+r});
219 end
220 ptr = ptr + 1 + R; % skip aggregate value
221 for r=1:R
222 Uchain(end,r)=str2num(str{ptr+r});
223 end
224 ptr = ptr + 1 + R; % skip aggregate value
225 for r=1:R
226 Tchain(end,r)=str2num(str{ptr+r});
227 end
228 end
229end
230%Station, $Q(Chain01), $Q(Chain02), $Q, $R(Chain01), $R(Chain02), $R, $U(Chain01), $U(Chain02), $U, $X(Chain01), $X(Chain02), $X
231%Delay1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2.63033, 7.40631, 10.0366
232%Queue1, 4, 4, 8, 1.52072, 0.54008, 0.797079, 1, 1, 2, 2.63033, 7.40631, 10.0366
233end
234
235function [Uchain, Qchain, Wchain, Tchain, statName] = parse_dollar_output_singleclass(strline, Uchain, Qchain, Wchain, Tchain)
236statName = {};
237if any(find(strline==','))
238 if any(find(strline=='$'))
239 % skip
240 else
241 R = size(Uchain,2);
242 %strline
243 str=strrep(strline,' ','');
244 str=strsplit(str,',');
245 Uchain(end+1,1:R) = 0;
246 Qchain(end+1,1:R) = 0;
247 Wchain(end+1,1:R) = 0;
248 Tchain(end+1,1:R) = 0;
249 ptr = 1;
250 statName{end+1} = str{1};
251 for r=1:R
252 Qchain(end,r)=str2num(str{ptr+r});
253 end
254 ptr = ptr + 1;
255 for r=1:R
256 Wchain(end,r)=str2num(str{ptr+r});
257 end
258 ptr = ptr + 1;
259 for r=1:R
260 Uchain(end,r)=str2num(str{ptr+r});
261 end
262 ptr = ptr + 1;
263 for r=1:R
264 Tchain(end,r)=str2num(str{ptr+r});
265 end
266 end
267end
268%Station, $Q(Chain01), $Q(Chain02), $Q, $R(Chain01), $R(Chain02), $R, $U(Chain01), $U(Chain02), $U, $X(Chain01), $X(Chain02), $X
269%Delay1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2.63033, 7.40631, 10.0366
270%Queue1, 4, 4, 8, 1.52072, 0.54008, 0.797079, 1, 1, 2, 2.63033, 7.40631, 10.0366
271end
272
273function [Lchain, Uchain, Qchain, Wchain, Tchain] = parse_starred_output(strline, Lchain, Uchain, Qchain, Wchain, Tchain)
274if any(find(strline=='.'))
275 str=strline(2:end);
276 str=strrep(str,' ','');
277 %[name,service,busypct,custnb,response,thruput]
278 str=strsplit(str,'*');
279 name = str{1};
280 if name(1)=='('
281 chainnum = strrep(name,'(Chain','');
282 chainnum = str2num(strrep(chainnum,')',''));
283 Lchain(i,chainnum) = str2num(str{2});
284 Uchain(i,chainnum) = str2num(str{3});
285 Qchain(i,chainnum) = str2num(str{4});
286 Wchain(i,chainnum) = str2num(str{5});
287 Tchain(i,chainnum) = str2num(str{6});
288 else
289 i=find(cellfun(@any,strfind(stationames,name)));
290 end
291end
292end
Definition fjtag.m:161