
clear all;clc;
% load data
addpath([cd '/utilits_1']);
load(['batchs.mat']);
%
K=5;% number of labeled samples in target domain
A2=batch2_p(:,4:2:end);
[model_2,test_2]=kenstone(A2,K);%kenstone(A2,floor(size(batch2_p,1)/40));
A3=batch3_p(:,4:2:end);
[model_3,test_3]=kenstone(A3,K);%kenstone(A3,floor(size(batch3_p,1)/40));
A4=batch4_p(:,4:2:end);
[model_4,test_4]=kenstone(A4,K);%kenstone(A4,floor(size(batch4_p,1)/40));
A5=batch5_p(:,4:2:end);
[model_5,test_5]=kenstone(A5,K);%kenstone(A5,floor(size(batch5_p,1)/40));
A6=batch6_p(:,4:2:end);
[model_6,test_6]=kenstone(A6,K);%kenstone(A6,floor(size(batch6_p,1)/40));
A7=batch7_p(:,4:2:end);
[model_7,test_7]=kenstone(A7,K);%kenstone(A7,floor(size(batch7_p,1)/40));
A8=batch8_p(:,4:2:end);
[model_8,test_8]=kenstone(A8,K);%kenstone(A8,floor(size(batch8_p,1)/40));
A9=batch9_p(:,4:2:end);
[model_9,test_9]=kenstone(A9,K);%kenstone(A9,floor(size(batch9_p,1)/40));
A10=batch10_p(:,4:2:end);
[model_10,test_10]=kenstone(A10,K);%kenstone(A10,floor(size(batch10_p,1)/40));

% labeled data in target domain (guide samples)
batch2_d=batch2_p(model_2,:);batch3_d=batch3_p(model_3,:);
batch4_d=batch4_p(model_4,:);batch5_d=batch5_p(model_5,:);
batch6_d=batch6_p(model_6,:);batch7_d=batch7_p(model_7,:);
batch8_d=batch8_p(model_8,:);batch9_d=batch9_p(model_9,:);
batch10_d=batch10_p(model_10,:);

% unlabeled data in target domain
batch2_du=batch2_p(test_2,:);batch3_du=batch3_p(test_3,:);
batch4_du=batch4_p(test_4,:);batch5_du=batch5_p(test_5,:);
batch6_du=batch6_p(test_6,:);batch7_du=batch7_p(test_7,:);
batch8_du=batch8_p(test_8,:);batch9_du=batch9_p(test_9,:);
batch10_du=batch10_p(test_10,:);

%}
%% Parameter setting
NumberofHiddenNeurons=1000;% 100
ActivationFunction='radbas';%'sigmoid';%;
Ct=0.001; % penalty regularization
Cs=100;%0.1; % penalty regularization 
NL=0;
for time=1:10
    for i1=1:9
        if i1==1
           Ts=batch1_p(:,1);% source domain
           Xs=batch1_p(:,4:2:end);if length(Ts)>NumberofHiddenNeurons; NL=1;else NL=0;end
        elseif i1==2
           Ts=batch2_p(:,1);% source domain
           Xs=batch2_p(:,4:2:end);if length(Ts)>NumberofHiddenNeurons; NL=1;else NL=0;end
        elseif i1==3
           Ts=batch3_p(:,1);% source domain
           Xs=batch3_p(:,4:2:end);if length(Ts)>NumberofHiddenNeurons; NL=1;else NL=0;end
        elseif i1==4
               Ts=batch4_p(:,1);% source domain
               Xs=batch4_p(:,4:2:end);if length(Ts)>NumberofHiddenNeurons; NL=1;else NL=0;end
        elseif i1==5
               Ts=batch5_p(:,1);% source domain
               Xs=batch5_p(:,4:2:end);if length(Ts)>NumberofHiddenNeurons; NL=1;else NL=0;end
        elseif i1==6
               Ts=batch6_p(:,1);% source domain
               Xs=batch6_p(:,4:2:end);if length(Ts)>NumberofHiddenNeurons; NL=1;else NL=0;end
        elseif i1==7
               Ts=batch7_p(:,1);% source domain
               Xs=batch7_p(:,4:2:end);if length(Ts)>NumberofHiddenNeurons; NL=1;else NL=0;end
        elseif i1==8
               Ts=batch8_p(:,1);% source domain
               Xs=batch8_p(:,4:2:end);if length(Ts)>NumberofHiddenNeurons; NL=1;else NL=0;end
        elseif i1==9
               Ts=batch9_p(:,1);% source domain
               Xs=batch9_p(:,4:2:end);if length(Ts)>NumberofHiddenNeurons; NL=1;else NL=0;end
        end
        for i2=i1+1:10
            if i2==2
               Xt=batch2_d(:,4:2:end); % target domain training
               Tt=batch2_d(:,1);if length(Tt)>NumberofHiddenNeurons; NT=1;else NT=0;end
           
               Xtu=batch2_du(:,4:2:end);% unlabeled target domain training

               TestT=batch2_p(:,1); % target domain testing data
               X_te=batch2_p(:,4:2:end);
            elseif i2==3
                   Xt=batch3_d(:,4:2:end); % target domain training
                   Tt=batch3_d(:,1);if length(Tt)>NumberofHiddenNeurons; NT=1;else NT=0;end
           
                   Xtu=batch3_du(:,4:2:end);% unlabeled target domain training

                   TestT=batch3_p(:,1); % target domain testing data
                   X_te=batch3_p(:,4:2:end);
            elseif i2==4
                   Xt=batch4_d(:,4:2:end); % target domain training
                   Tt=batch4_d(:,1);if length(Tt)>NumberofHiddenNeurons; NT=1;else NT=0;end
           
                   Xtu=batch4_du(:,4:2:end);% unlabeled target domain training

                   TestT=batch4_p(:,1); % target domain testing data
                   X_te=batch4_p(:,4:2:end);
            elseif i2==5
                   Xt=batch5_d(:,4:2:end); % target domain training
                   Tt=batch5_d(:,1);if length(Tt)>NumberofHiddenNeurons; NT=1;else NT=0;end
           
                   Xtu=batch5_du(:,4:2:end);% unlabeled target domain training

                   TestT=batch5_p(:,1); % target domain testing data
                   X_te=batch5_p(:,4:2:end);
            elseif i2==6
                   Xt=batch6_d(:,4:2:end); % target domain training
                   Tt=batch6_d(:,1);if length(Tt)>NumberofHiddenNeurons; NT=1;else NT=0;end
           
                   Xtu=batch6_du(:,4:2:end);% unlabeled target domain training

                   TestT=batch6_p(:,1); % target domain testing data
                   X_te=batch6_p(:,4:2:end);
            elseif i2==7
                   Xt=batch7_d(:,4:2:end); % target domain training
                   Tt=batch7_d(:,1);if length(Tt)>NumberofHiddenNeurons; NT=1;else NT=0;end
           
                   Xtu=batch7_du(:,4:2:end);% unlabeled target domain training

                   TestT=batch7_p(:,1); % target domain testing data
                   X_te=batch7_p(:,4:2:end);
            elseif i2==8
                   Xt=batch8_d(:,4:2:end); % target domain training
                   Tt=batch8_d(:,1);if length(Tt)>NumberofHiddenNeurons; NT=1;else NT=0;end
           
                   Xtu=batch8_du(:,4:2:end);% unlabeled target domain training

                   TestT=batch8_p(:,1); % target domain testing data
                   X_te=batch8_p(:,4:2:end);
            elseif i2==9
                   Xt=batch9_d(:,4:2:end); % target domain training
                   Tt=batch9_d(:,1);if length(Tt)>NumberofHiddenNeurons; NT=1;else NT=0;end
           
                   Xtu=batch9_du(:,4:2:end);% unlabeled target domain training

                   TestT=batch9_p(:,1); % target domain testing data
                   X_te=batch9_p(:,4:2:end);
            elseif i2==10
                   Xt=batch10_d(:,4:2:end); % target domain training
                   Tt=batch10_d(:,1);if length(Tt)>NumberofHiddenNeurons; NT=1;else NT=0;end
           
                   Xtu=batch10_du(:,4:2:end);% unlabeled target domain training

                   TestT=batch10_p(:,1); % target domain testing data
                   X_te=batch10_p(:,4:2:end);
            end
        for i=1:size(Xs,2) % data preprocessing: scaling
            Xs(:,i)=Xs(:,i)/max(abs(Xs(:,i)));
            Xt(:,i)=Xt(:,i)/max(abs(Xt(:,i)));
            X_te(:,i)=X_te(:,i)/max(abs(X_te(:,i)));
        end
       %% Parameter init for first stage (ELM) Base classifier training
        % Ts=[Ts;Tt];Xs=[Xs;Xt];
        TrainingData_File=[Ts,Xs];
        TestingData_File=[Ts,Xs];
        T=TrainingData_File(:,1);
        Elm_Type=1;
        C=0.001;
        NumberofOutputNeurons=length(unique(T));
        NumberofInputNeurons=size(Xs,2);
        InputWeight=rand(NumberofHiddenNeurons,NumberofInputNeurons)*2-1;
        BiasofHiddenNeurons=rand(NumberofHiddenNeurons,1);
        BetaS = baseClassifier(TrainingData_File, TestingData_File, Elm_Type, NumberofHiddenNeurons, ActivationFunction,C,NL);

       %% Parameter init for second stage (TdaTELM)
        TrainingData_File1=[Tt,Xt]; % labeled data in target domain
        Ttu=zeros(size(Xtu,1),1);
        TrainingData_File1_tardomain=[Ttu,Xtu]; % unlabeled data in target domain
        TestingData_File1=[TestT,X_te];
        T=TrainingData_File1(:,1);
 
        Elm_Type=1;
        NumberofOutputNeurons=6;%length(unique(T));
        NumberofInputNeurons=size(Xs,2);
        InputWeight=rand(NumberofHiddenNeurons,NumberofInputNeurons)*2-1;
        BiasofHiddenNeurons=rand(NumberofHiddenNeurons,1);
        [MissClassificationRate_Training,MissClassificationRate_Testing] = DAELM_T(TrainingData_File1,TrainingData_File1_tardomain, TestingData_File1, Elm_Type, NumberofHiddenNeurons, ActivationFunction,Cs,Ct,NL,NT,BetaS);
        acc_te(i1,i2)=(1-MissClassificationRate_Testing/length(TestT))*100;
        end
    end   
    Acc{time}=acc_te;
end
