LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
FluidQueue.m
1% Ret = FluidQueue(Q, Rin, Rout, ...)
2%
3% Returns various performane measures of a fluid queue.
4%
5% In a fluid queue there is a background continuous time
6% Markov chain (given by generator Q), and diagonal
7% matrix Rin (Rout) whose ith entry provides the
8% fluid rate at which fluid enters the queue (can be
9% served) while the background process is in state i.
10%
11% Parameters
12% ----------
13% Q : matrix, shape (N,N)
14% The generator of the background Markov chain
15% Rin : matrix, shape (N,N)
16% Diagonal matrix containing the fluid input rates
17% associated to the states of the background process
18% Rout : matrix, shape (N,N)
19% Diagonal matrix containing the fluid output rates
20% associated to the states of the background process
21% further parameters :
22% The rest of the function parameters specify the options
23% and the performance measures to be computed.
24%
25% The supported performance measures and options in this
26% function are:
27%
28% +----------------+--------------------+--------------------------------------+
29% | Parameter name | Input parameters | Output |
30% +================+====================+======================================+
31% | "flMoms" | Number of moments | The moments of the fluid level |
32% +----------------+--------------------+--------------------------------------+
33% | "flDistr" | A vector of points | The fluid level distribution at |
34% | | | the requested points (cdf) |
35% +----------------+--------------------+--------------------------------------+
36% | "flDistrME" | None | The vector-matrix parameters of the |
37% | | | matrix-exponentially distributed |
38% | | | fluid level distribution |
39% +----------------+--------------------+--------------------------------------+
40% | "flDistrPH" | None | The vector-matrix parameters of the |
41% | | | matrix-exponentially distributed |
42% | | | fluid level distribution, converted |
43% | | | to a PH representation |
44% +----------------+--------------------+--------------------------------------+
45% | "stMoms" | Number of moments | The sojourn time moments of fluid |
46% | | | drops |
47% +----------------+--------------------+--------------------------------------+
48% | "stDistr" | A vector of points | The sojourn time distribution at the |
49% | | | requested points (cummulative, cdf) |
50% +----------------+--------------------+--------------------------------------+
51% | "stDistrME" | None | The vector-matrix parameters of the |
52% | | | matrix-exponentially distributed |
53% | | | sojourn time distribution |
54% +----------------+--------------------+--------------------------------------+
55% | "stDistrPH" | None | The vector-matrix parameters of the |
56% | | | matrix-exponentially distributed |
57% | | | sojourn time distribution, converted |
58% | | | to a PH representation |
59% +----------------+--------------------+--------------------------------------+
60% | "prec" | The precision | Numerical precision to check if the |
61% | | | input is valid and it is also used |
62% | | | as a stopping condition when solving |
63% | | | the Riccati equation |
64% +----------------+--------------------+--------------------------------------+
65% | "Q0" | Matrix, shape(N,N) | The generator of the background |
66% | | | Markov chain when the fluid level is |
67% | | | zero. If not given, Q0=Q is assumed |
68% +----------------+--------------------+--------------------------------------+
69%
70% Returns
71% -------
72% Ret : list of the performance measures
73% Each entry of the list corresponds to a performance
74% measure requested. If there is just a single item,
75% then it is not put into a list.
76%
77% Notes
78% -----
79% "flDistrME" and "stDistrME" behave much better numerically than
80% "flDistrPH" and "stDistrPH".
81
82function varargout = FluidQueue(Q, Rin, Rout, varargin)
83
84 % parse options
85 prec = 1e-14;
86 needST = 0;
87 Q0 = [];
88 eaten = [];
89 for i=1:length(varargin)
90 if strcmp(varargin{i},'prec')
91 prec = varargin{i+1};
92 eaten = [eaten, i, i+1];
93 elseif strcmp(varargin{i},'Q0')
94 Q0 = varargin{i+1};
95 eaten = [eaten, i, i+1];
96 elseif length(varargin{i})>2 && strcmp(varargin{i}(1:2),'st')
97 needST = 1;
98 end
99 end
100
101 global BuToolsCheckInput;
102
103 if isempty(BuToolsCheckInput)
104 BuToolsCheckInput = true;
105 end
106
107 global BuToolsCheckPrecision;
108
109 if BuToolsCheckInput && ~CheckGenerator(Q,false)
110 error('FluidQueue: Generator matrix Q is not Markovian!');
111 end
112 if BuToolsCheckInput && ~isempty(Q0) && ~CheckGenerator(Q0,false)
113 error('FluidQueue: Generator matrix Q0 is not Markovian!');
114 end
115 if BuToolsCheckInput && (any(diag(Rin)<-BuToolsCheckPrecision) || any(diag(Rout)<-BuToolsCheckPrecision))
116 error('FluidQueue: Fluid rates Rin and Rout must be non-negative !');
117 end
118
119 [mass0, ini, K, clo] = GeneralFluidSolve (Q, Rin-Rout, Q0, prec);
120 if needST
121 N = size(Q,1);
122 iniKi = linsolve(K',-ini')'; % iniki = ini*inv(-K);
123 lambda = sum(mass0*Rin + iniKi*clo*Rin);
124 end
125
126 Ret = {};
127 argIx = 1;
128 while argIx<=length(varargin)
129 if any(ismember(eaten, argIx))
130 argIx = argIx + 1;
131 continue;
132 elseif strcmp(varargin{argIx},'flDistrPH')
133 % transform it to PH
134 Delta = diag(linsolve(K',-ini')); % Delta = diag (ini*inv(-K));
135 A = inv(Delta)*K'*Delta;
136 alpha = sum(clo,2)'*Delta;
137 Ret{end+1} = alpha;
138 Ret{end+1} = A;
139 elseif strcmp(varargin{argIx},'flDistrME')
140 % transform it to ME
141 B = SimilarityMatrixForVectors(inv(-K)*sum(clo,2), ones(size(K,1),1));
142 Bi = inv(B);
143 alpha = ini*Bi;
144 A = B*K*Bi;
145 Ret{end+1} = alpha;
146 Ret{end+1} = A;
147 elseif strcmp(varargin{argIx},'flMoms')
148 numOfMoms = varargin{argIx+1};
149 argIx = argIx + 1;
150 moms = zeros(1,numOfMoms);
151 iK = inv(-K);
152 for m=1:numOfMoms
153 moms(m) = factorial(m)*sum(ini*iK^(m+1)*clo);
154 end
155 Ret{end+1} = moms;
156 elseif strcmp(varargin{argIx},'flDistr')
157 points = varargin{argIx+1};
158 argIx = argIx + 1;
159 values = zeros(size(points));
160 iK = inv(-K);
161 for p=1:length(points)
162 values(p) = sum(mass0) + sum(ini*(eye(size(K,1))-expm(K*points(p)))*iK*clo);
163 end
164 Ret{end+1} = values;
165 elseif strcmp(varargin{argIx},'stDistrPH')
166 % transform it to PH
167 Delta = diag(iniKi/lambda);
168 alpha = reshape(clo*Rin,1,N*length(ini))*kron(eye(N),Delta);
169 A = kron(Rout, inv(Delta)*K'*Delta) + kron(Q, eye(size(K)));
170 Ret{end+1} = alpha;
171 Ret{end+1} = A;
172 elseif strcmp(varargin{argIx},'stDistrME')
173 B = SimilarityMatrixForVectors(reshape(inv(-K)*clo*Rin,N*length(ini),1),ones(N*length(ini),1));
174 Bi = inv(B);
175 alpha = kron(ones(1,N), ini/lambda)*Bi;
176 A = B*(kron(sparse(Q'),speye(size(K))) + kron(sparse(Rout),sparse(K)))*Bi;
177 Ret{end+1} = alpha;
178 Ret{end+1} = A;
179 elseif strcmp(varargin{argIx},'stMoms')
180 numOfMoms = varargin{argIx+1};
181 argIx = argIx + 1;
182 moms = zeros(1,numOfMoms);
183 Z = kron(sparse(Q'),speye(size(K))) + kron(sparse(Rout),sparse(K));
184 iZ = inv(-Z);
185 kini = kron(ones(1,N), ini/lambda);
186 kclo = reshape(inv(-K)*clo*Rin,N*length(ini),1);
187 for m=1:numOfMoms
188 moms(m) = factorial(m)*sum(kini*iZ^(m+1)*(-Z)*kclo);
189 end
190 Ret{end+1} = moms;
191 elseif strcmp(varargin{argIx},'stDistr')
192 points = varargin{argIx+1};
193 argIx = argIx + 1;
194 values = zeros(size(points));
195 Z = kron(sparse(Q'),speye(size(K))) + kron(sparse(Rout),sparse(K));
196 kini = kron(ones(1,N), ini/lambda);
197 kclo = reshape(inv(-K)*clo*Rin,N*length(ini),1);
198 for p=1:length(points)
199 values(p) = 1-sum(kini*expm(Z*points(p))*kclo);
200 end
201 Ret{end+1} = values;
202 else
203 error (['FluidQueue: Unknown parameter ' varargin{argIx}])
204 end
205 argIx = argIx + 1;
206 end
207 varargout = Ret;
208end
209