% Incremental Classifier Adpatation
clc;clear all;close all;
load ('batchs.mat');
labels={batch1_p(:,1),batch2_p(:,1),batch3_p(:,1),batch4_p(:,1),batch5_p(:,1),batch6_p(:,1),batch7_p(:,1),batch8_p(:,1),batch9_p(:,1),batch10_p(:,1)};
data={batch1_p(:,4:2:end),batch2_p(:,4:2:end),batch3_p(:,4:2:end),batch4_p(:,4:2:end),batch5_p(:,4:2:end),batch6_p(:,4:2:end),batch7_p(:,4:2:end),...
    batch8_p(:,4:2:end),batch9_p(:,4:2:end),batch10_p(:,4:2:end)};
for i=1:size(data,2)
    for j=1:size(data{i},2)
        data{i}(:,j)=data{i}(:,j)/max(abs(data{i}(:,j)));
    end
end
d=128;
C=6;
DAT{1}=data{1};
Lab{1}=labels{1};
for i=2:size(data,2)
    indx=[];
    for j=1:C-1 
        indj=find(labels{i}==j);
        indx=[indx,indj(1:4)];
    end
    DAT{i}=data{i}(indx,:);
    Lab{i}=labels{i}(indx,:);
end

for i=1:size(Lab,2)
    for j=1:size(Lab{i},1)
        if Lab{i}(j)==1
            Lab_vectors{i}(j,:)=[1 0 0 0 0 0];
        elseif Lab{i}(j)==2
            Lab_vectors{i}(j,:)=[0 1 0 0 0 0];
        elseif Lab{i}(j)==3
            Lab_vectors{i}(j,:)=[0 0 1 0 0 0];
        elseif Lab{i}(j)==4
            Lab_vectors{i}(j,:)=[0 0 0 1 0 0];
        elseif Lab{i}(j)==5
            Lab_vectors{i}(j,:)=[0 0 0 0 1 0];
        elseif Lab{i}(j)==6
            Lab_vectors{i}(j,:)=[0 0 0 0 0 1];
        end
    end
end

lamda=0.1;
miu=10;

W=[];
for i=1:size(Lab,2)
    Wk{i}=rand(d,C);
    W=[W,Wk{i}];
end
t=0;
while 1
    t=t+1;
    for i=1:size(Lab,2)
        v=[];
        for j=1:d
            v=[v,1/(2*norm(Wk{i}(j,:)))];
        end
        U{i}=diag(v);
        B{i}=(ones(size(DAT{i},1),1)'*Lab_vectors{i}-ones(size(DAT{i},1),1)'*DAT{i}*Wk{i})/(ones(size(DAT{i},1),1)'*ones(size(DAT{i},1),1));
        Wk{i}=inv(DAT{i}'*DAT{i}+lamda*U{i}+miu*diag(diag((W*W'+eps).^(-0.5))))*(DAT{i}'*Lab_vectors{i}-DAT{i}'*ones(size(DAT{i},1),1)*B{i});
    end
    W=[];
    for k=1:size(Lab,2)
        W=[W,Wk{i}];
    end
    if t>5
        break
    end
end
for i=2:size(data,2)
    y_pre{i}=data{i}*Wk{1}+repmat(B{1},size(data{i},1),1);
    for j=1:size(data{i},1)
        [v,ind_lab]=max(y_pre{i}(j,:));
        pred_lab{i}(j)=ind_lab;
    end
    ICA_acc(i)=length(find(pred_lab{i}==labels{i}'))/length(labels{i})*100;
end        

% regularize least square
lambda=1;
for i=2:size(data,2)
    P_b=inv((data{1}'*data{1}+lambda*eye(128)))*data{1}'*Lab_vectors{1};
    y_b{i}=data{i}*P_b;
    for j=1:size(y_b{i},1)
        [v,ind_ls_b]=max(y_b{i}(j,:));
        ls_pred_b{i}(j,:)=ind_ls_b;
    end
    ls_acc_b(i)=length(find(ls_pred_b{i}==labels{i}))/length(labels{i})*100;
end
ICA_acc
ls_acc_b