%
% Lorg means Self Organization target stage number.
%
Lorg = 2;

%
% Setup Initial parameters
%
InitParams;
%
% Load Input Pattern
%
P = PatLoad( 'PatLearn.dat' );

%
% Connections from Uc ---> Us
% Connection named A, B, C
%
for l=1:LH, A{l} = {};, end;
for l=1:LH, B{l} = [];, end;
for l=1:Lorg-1
    [ A{l}, B{l} ] = ReadConnect( 'connect.dat', l-1 );
end
for l=1:LH
    [ Aref{l}, Bref{l} ] = ReadConnect( 'connect.dat', l-1 );
end

%
% Test for Sorg for pattern P(:,:,1:400)
% 

%
% show reference
%
display_coef( Aref{Lorg}, Bref{Lorg}, 1 );


%
% Training and show development
%
for t=1:400 
  Ug = OnOff( P(:,:,t) );
  for l=1:Lorg-1
    if( l == 1 )
      UcPrev = Ug;
    else
      UcPrev = Uc{l-1};
    end
    Us{l} =  Sres( UsSize(l), UcPrev, A{l}, B{l}, C{l}, Thr(l) );
    if( l ~= LH )
      Uc{l} = Cres( UcSize(l), Us{l}, D{l} );
    end
  end
  [A{Lorg} B{Lorg}] = Sorg( UsSize(Lorg), Uc{1}, A{Lorg}, B{Lorg}, C{Lorg}, Thl(Lorg) );
  display_coef( A{Lorg}, B{Lorg}, 2 );
end


