%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%    Copyright (c) 2004.  Takayuki Osogami.  All Rights Reserved.   %
%                                                                   %
% Permission to use, copy, modify, and distribute this source code  %
% and its documentation for any purpose, without fee, and without   %
% a written agreement, is hereby granted, provided that the above   %
% copyright notice, this paragraph and the following two paragraphs %
% appear in all copies, modifications, and distributions.           %
% Created by Takayuki Osogami, Department of Computer Science,      %
% Carnegie Mellon University.                                       %
%                                                                   %
% IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR ANY       %
% DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS   %
% SOURCE CODE AND ITS DOCUMENTATION, EVEN IF THE AUTHOR HAS BEEN    %
% ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                        %
%                                                                   %
% THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,      %
% BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY     %
% AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOURCE CODE AND        %
% ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS         %
% PROVIDED "AS IS."  THE AUTHOR HAS NO OBLIGATION TO PROVIDE        %
% MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.    %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Usage: [B0,B1,B2] =  infinite2finiteA(A0,A1,A2,n,k,epsilon,PH)
%
% Given a QBD process with infinite levels,
% it returns an approximate QBD process with finite levels.
% Here, the generator matrix of the input QBD process is
%
% [ A1{1} A0{1}                                    ]
% [ A2{2} A1{2} A0{2}                              ]
% [       A2{3} A1{3} A0{3}                        ]
% [              :     :                           ]
% [                        A2{n} A1{n} A0{n}       ]
% [                              A2{n} A1{n} A0{n} ]
% [                                     :     :    ]
%
% Note that the QBD process repeats after level n.
%
% The generator matrix of the output QBD process is
%
% [ B1{1} B0{1}                              ]
% [ B2{2} B1{2} B0{2}                        ]
% [       B2{3} B1{3} B0{3}                  ]
% [              :     :                     ]
% [                        B2{k} B1{k} B0{k} ]
%
%
% We define cells, A0, A1, and A2:
% A0 = {A0{1} A0{2} ... A0{n}}
% A1 = {A1{1} A1{2} ... A1{n}}
% A2 = {A2{1} A2{2} ... A2{n}},
%
% INPUT: A0,A1,A2: cells for A0{i}, A1{i}, A2{i}
%        n: level that the QBD process starts repeating
%        k: the number of levels in the output QBD process
%        epsilon: the error bound (epsilon = 1e-6: recommended).
%        PH: number of phases used in approximate PH distributions (1 or 2)
%
% OUTPUT: B0,B1,B2: cells for B0{i}, B1{i}, B2{i}
%
% author: Takayuki Osogami
%         Department of Computer Science
%         Carnegie Mellon University
%         osogami@cs.cmu.edu
% date: August 15, 2004

function [B0,B1,B2] =  infinite2finiteB(A0,A1,A2,n,k,epsilon,PH)

%%%%%%%%%%%%%%%%%%%%%
%% "non-busy" part %%
%%%%%%%%%%%%%%%%%%%%%

for i = 1:k
  if( i <= n )
    B1{i} = A1{i};
    B2{i} = A2{i};
  else
    B1{i} = A1{n};
    B2{i} = A2{n};
  end
end
for i = 1:k-1
  if( i <= n )
    B0{i} = A0{i};
  else
    B0{i} = A0{n};
  end
end


%%%%%%%%%%%%%%%%%
%% "busy" part %%
%%%%%%%%%%%%%%%%%

[R,U,G] = RUGmatrices(A0,A1,A2,n,epsilon);

%% calculate limiting probabilities at level k: pi{k} %%

dim = size(U{1});
M = - U{1};
M(:,dim(1)) = ones(dim(1),1);
vec = zeros(1,dim(1));
vec(1,dim(1)) = 1;
pi{1} = vec * inv(M);

if( k <= n )
  for i = 2:k;
    pi{i} = pi{i-1} * R{i};
  end
else
  for i = 2:n;
    pi{i} = pi{i-1} * R{i};
  end
  for i = n+1:k
    pi{i} = pi{i-1} * R{n};
  end
end

%% calculating moments of busy periods %%

if( k + 1 >= n )
  GG = G{n};
else
  GG = G{k+1};
end

FF = A0{k};
dim = size(FF);
N = dim(1);
for i = 1:N
  total = sum(FF(i,:));
  if( total > 0 )
    FF(i,:) = FF(i,:) / total;
  end
end

PP = FF * GG;

DD = A0{k} * GG;

HH = Neuts3(A0,A1,A2,G,n,k+1,epsilon);

for h = 1:3

 EE{h} = A0{k} * HH{h};

 for i = 1:N
  for j = 1:N
   if( DD(i,j) > 0 )
    MM{h}(i,j) = EE{h}(i,j) / DD(i,j);
   else
    MM{h}(i,j) = 0;
   end
  end
 end
end


%% aggregating MM{h}'s %%

dim = size(A0{k});
lambda = A0{k} * ones(dim(2),1);

for h = 1:3
 MMA(h) = 0;
end

total = 0;
for j = 1:N
 for i = 1:N
  tmp = pi{k}(i) * lambda(i) * PP(i,j);
  for h = 1:3
   MMA(h) = MMA(h) + tmp * MM{h}(i,j);
  end
  total = total + tmp;
 end
end

for h = 1:3
 MMA(h) = MMA(h) / total;
end


%% approximating busy periods by PH distributions %%

if( PH == 2 ) % matching 3 moments
     [tau,T] = coxian2(MMA(1),MMA(2),MMA(3));
     dim = size(T);
     t = - T * ones(dim(1),1);
elseif( PH == 1) % matching mean (1 moment)
     T = - 1 / MMA(1);
     tau = 1;
     t = - T;
else
  not_supported = 1
end


%% specifying B0{k}, B1{k+1}, B2{k+1} %%
 
B1{k+1} = T;

    
dim0 = size(B1{k});
dim1 = size(B1{k+1});
B0{k} = zeros(dim0(1),dim1(2));
B2{k+1} = zeros(dim1(1),dim0(2));

xi = (transpose(lambda) .* pi{k}) * PP;
total = sum(xi);
xi = xi / total;

for j = 1:N
  B2{k+1} = t * xi;
end

for i = 1:N
  B0{k}(i,:) = lambda(i) * tau;
end


