LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
pfqn_comomrm.m
1%{
2%{
3 % @file pfqn_comomrm.m
4 % @brief CoMoM (Class-Oriented Method of Moments) for finite repairman model.
5%}
6%}
7
8%{
9%{
10 % @brief CoMoM (Class-Oriented Method of Moments) for finite repairman model.
11 % @fn pfqn_comomrm(L, N, Z, m, atol)
12 % @param L Service demand matrix.
13 % @param N Population vector.
14 % @param Z Think time vector.
15 % @param m Replication factor (default: 1).
16 % @param atol Absolute tolerance for numerical computations.
17 % @return lG Logarithm of normalizing constant.
18 % @return lGbasis Logarithm of basis functions.
19%}
20%}
21function [lG,lGbasis]=pfqn_comomrm(L,N,Z,m,atol)
22% comom for a finite repairment model
23[M,R]=size(L);
24if M~=1
25 line_error(mfilename,'The solver accepts at most a single queueing station.')
26end
27if nargin<4
28 m=1;
29end
30% Same omission as pfqn_comom: atol was read by pfqn_nc_sanitize below but
31% never defaulted, so the 3- and 4-argument forms in the header were dead.
32if nargin<5
33 atol=1e-14;
34end
35lambda = 0*N;
36[~,L,N,Z,lG0] = pfqn_nc_sanitize(lambda,L,N,Z,atol);
37% R must be re-read: sanitize DROPS zero-population and zero-demand classes,
38% so the pre-sanitize R overruns the shortened Z, L and N below.
39R = size(L,2);
40zerothinktimes = zeros(1,R);
41numZeroThinkTimes = 0;
42for r = 1:R
43 if Z(r) < GlobalConstants.FineTol
44 % see _kb/03-api-layer.md (pfqn/ family: scaling, log-domain switches, dispatch gates)
45 numZeroThinkTimes = numZeroThinkTimes + 1;
46 zerothinktimes(numZeroThinkTimes) = r;
47 end
48end
49% initialize
50nvec=zeros(1,R);
51if numZeroThinkTimes > 0
52 for z = 1:numZeroThinkTimes
53 idx = zerothinktimes(z);
54 nvec(idx) = N(idx);
55 end
56 lh=zeros(2+2*numZeroThinkTimes,1);
57 lhIdx = 1;
58 % these are trivial models with a single queueing station with demands all equal to one and think time 0
59 lh(lhIdx,1) = (factln(sum(nvec)+m+1-1)-sum(factln(nvec)));
60 lhIdx = lhIdx + 1;
61 for z = 1:numZeroThinkTimes
62 s = zerothinktimes(z);
63 nvec_s = oner(nvec,s);
64 lh(lhIdx,1) = (factln(sum(nvec_s)+m+1-1)-sum(factln(nvec_s)));
65 lhIdx = lhIdx + 1;
66 end
67 lh(lhIdx,1) = (factln(sum(nvec)+m-1)-sum(factln(nvec)));
68 lhIdx = lhIdx + 1;
69 for z = 1:numZeroThinkTimes
70 s = zerothinktimes(z);
71 nvec_s = oner(nvec,s);
72 lh(lhIdx,1) = (factln(sum(nvec_s)+m-1)-sum(factln(nvec_s)));
73 lhIdx = lhIdx + 1;
74 end
75else
76 lh=zeros(2,1);
77end
78h=exp(lh);
79if numZeroThinkTimes==R
80 lGbasis = log(h);
81 lG = lG0 + log(h(end-R));
82 return
83else
84 scale = ones(1,sum(N));
85 nt = sum(nvec);
86 h_1=h;
87 %iterate
88 for r=(numZeroThinkTimes+1):R
89 F1r = zeros(2*r);
90 F2r = zeros(2*r);
91 for Nr=1:N(r)
92 nvec(r)=nvec(r)+1;
93 if Nr==1
94 if r> numZeroThinkTimes+1
95 hr = zeros(2*r,1);
96 hr(1:(r-1)) = h(1:(r-1));
97 hr((r+1):(2*r-1)) = h(((r-1)+1):2*(r-1));
98 h=hr;
99 % update scalings
100 if nt>0
101 h(r)=h_1(1)/scale(nt);
102 h(end)=h_1((r-1)+1)/scale(nt);
103 end
104 end
105 % CE for G+
106 A12 = zeros(r);
107 A12(1,1) = -1;
108 % Class-1..(R-1) PCs for G
109 for s=1:(r-1)
110 A12(1+s,1) = N(s);
111 A12(1+s,1+s) = -Z(s);
112 end
113 % Class-R PCs
114 B2r = [m*L(1,r)*eye(r), Z(r)*eye(r)];
115 % explicit formula for inv(C)
116 iC=-eye(r)/m;
117 iC(1,:)=-1/m;
118 iC(1)=1;
119 % explicit formula for F1r
120 F1r = zeros(2*r); F1r(1,1)=1;
121 % F2r by the definition
122 F2r = [-iC*A12*B2r; B2r];
123 end
124 h_1 = h;
125 h = (F1r+F2r/nvec(r))*h_1;
126 nt = sum(nvec);
127 scale(nt) = abs(sum(sort(h)));
128 h = abs(h)/scale(nt); % rescale so that |h|=1
129 end
130 end
131
132 % unscale and return the log of the normalizing constant
133 lG = lG0 + log(h(end-(R-1))) + sum(log(scale));
134 lGbasis = log(h) + sum(log(scale));
135end
136end