
clear all;clc;
% load data
addpath([cd '/utilits']);
load(['batchs.mat']);

K=50; % 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)/50));
A3=batch3_p(:,4:2:end);
[model_3,test_3]=kenstone(A3,K);%kenstone(A3,floor(size(batch3_p,1)/50));
A4=batch4_p(:,4:2:end);
[model_4,test_4]=kenstone(A4,K);%kenstone(A4,floor(size(batch4_p,1)/20));
A5=batch5_p(:,4:2:end);
[model_5,test_5]=kenstone(A5,K);%kenstone(A5,floor(size(batch5_p,1)/50));
A6=batch6_p(:,4:2:end);
[model_6,test_6]=kenstone(A6,K);%kenstone(A6,floor(size(batch6_p,1)/50));
A7=batch7_p(:,4:2:end);
[model_7,test_7]=kenstone(A7,K);%kenstone(A7,floor(size(batch7_p,1)/50));
A8=batch8_p(:,4:2:end);
[model_8,test_8]=kenstone(A8,K);%kenstone(A8,floor(size(batch8_p,1)/50));
A9=batch9_p(:,4:2:end);
[model_9,test_9]=kenstone(A9,K);%kenstone(A9,floor(size(batch9_p,1)/50));
A10=batch10_p(:,4:2:end);
[model_10,test_10]=kenstone(A10,K);%kenstone(A10,floor(size(batch10_p,1)/50));

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,:);
%}
%% Parameter setting
NumberofHiddenNeurons=1000;% 100
ActivationFunction='radbas';%'sigmoid';
Cs=0.001; % regularization
Ct=0.1; % regularization
NL=0;% model selection
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);

               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);

                   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);

                   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);

                   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);

                   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);

                   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);

                   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);

                   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);

                   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
            TrainingData_File=[Ts,Xs];
            TrainingData_File_tardomain=[Tt,Xt];
            TestingData_File=[TestT,X_te];
            T=TrainingData_File(:,1);

          %% Parameter init
            Elm_Type=1;
            NumberofOutputNeurons=length(unique(T));
            NumberofInputNeurons=size(Xs,2);

            InputWeight=rand(NumberofHiddenNeurons,NumberofInputNeurons)*2-1;
            BiasofHiddenNeurons=rand(NumberofHiddenNeurons,1);
            [MissClassificationRate_Training,MissClassificationRate_Testing] = DAELMS(TrainingData_File,TrainingData_File_tardomain, TestingData_File, Elm_Type, NumberofHiddenNeurons, ActivationFunction,Cs,Ct,NL);
            acc_te(i1,i2)=(1-MissClassificationRate_Testing/length(TestT))*100;
        end
    
    end

    Acc{time}=acc_te; % testing accuracy of ten times running 
end
