function [Train_accuracy,Test_Accuracy,bestc,pred_label,func_res,obj_results]=linear_train_unbalanced_new2(train_data,train_label,test_label,test_data,...
    split_parts,c_begin,c_step,c_end,tolerance, cut_off)

%
%   Vasileios Plakandaras, 2012 <email: vplakand@ierd.duth.epsilonr>

chunk_cr(train_data,train_label,split_parts);%create the chunk parts
kernel = 0; % linear kernel
svm_type=0; % C-SVM classification
bestacc =0;%initial value
bestc = c_begin;% initial value

t=1;i=1;
iter_acc(split_parts,1)=0;
new_best_c=0;
old_best_c=100;
while (abs(new_best_c-old_best_c)>=tolerance && t <= 10);
    if t==1;
        new_step_c=c_step;
        new_begin_c=c_begin;
        new_end_c=c_end;
    else
        new_begin_c=bestc/2;
        new_end_c=2*bestc;
        new_step_c=(new_end_c-new_begin_c)/(4*t);
        
    end
    all_c=new_begin_c:new_step_c:new_end_c;
    
   
for c = all_c;
    f=length(all_c);
    h = waitbar(0,'1','Name',sprintf('Checking C value %2d of value range %2d',i,f));
        for a=1:1:split_parts;
        waitbar(a/split_parts,h,sprintf('Training chunk %4d',a))
        mat_file=[num2str(a),'_of_',num2str(split_parts)];
        load(mat_file,'-mat');
        cmd = [' -s ', num2str(svm_type),' -t ',num2str(kernel),' -c ',num2str(c), ' -w1 1 -w-1 2 '];
        model = svmtrain(train_label_chunk,train_data_chunk,cmd);
       [ ~, ~, func_res] = svmpredict(test_label_chunk,test_data_chunk,model);
       if train_label_chunk(1,1)==-1;
           func_res=func_res.*-1;
       end
       obj_results=zeros(numel(test_label_chunk),1);
       obj_results(func_res>=cut_off)=1;
       obj_results(func_res<cut_off)=-1;
       temp=zeros(numel(test_label_chunk),1);
       temp(obj_results==test_label_chunk)=1;
      iter_acc(a,1)=100*(sum(temp))/numel(test_label_chunk);
        end
       mean_accuracy=mean(iter_acc);
       disp(['Cross Validation Accuracy=',num2str(mean_accuracy),'%'])
         if (mean_accuracy > bestacc) || ((mean_accuracy == bestacc) && (c < bestc))
             bestacc = mean_accuracy; bestc = c; 
         end
        disp([num2str(c),' ',' (best C=',num2str(bestc),' Train_Accuracy=',num2str(bestacc),' % '])
        
    i=i+1;
    
    close(h)
end
best_step(t,1)=bestc;
new_best_c=bestc;
if t<=2
   old_best_c=2*bestc;
else old_best_c=best_step(t-1,1);
end
t=t+1;
i=1;
end
Train_accuracy=bestacc;

for k=1:split_parts;
    mat_file=[num2str(k),'_of_', num2str(split_parts),'.mat'];
    delete (mat_file);
end

%-----------Solve the problem for the Test sample

options = [' -s ', num2str(svm_type),' -t ', num2str(kernel),' -c ', num2str(bestc), ' -w1 1 -w-1 2 '];

% scale train data columns to [-1,1]
[scale,ps]= mapminmax(train_data');

% scale test data columns with the initial scaling factor
test_scale=mapminmax('apply',test_data',ps);

% --- solve the problem ---
fprintf('Generalize Solution\n');
model = svmtrain(train_label, scale', options);

% --- generalize solution ---
[~,~,func_res_test] = svmpredict(test_label, test_scale', model);
if train_label(1,1)==-1;
   func_res_test=func_res_test.*-1;
end
obj_results=zeros(numel(test_label),1);
obj_results(func_res_test>=cut_off)=1;
obj_results(func_res_test<cut_off)=-1;
temp=zeros(numel(test_label),1);
temp(obj_results==test_label)=1;
pred_label=obj_results;
Test_Accuracy=100*(sum(temp))/numel(test_label);


% ------------Auxiliary function----------------

function [ps]=chunk_cr(instances,labels,split_parts)
    %-----produces the parts for the cross validation traininepsilon-----
    %       input:     instances: a mxn matrix of train data, with n beinepsilon the
    %                  dimension(features)
    %                  labels: a nx1 vector of traininepsilon tarepsilonets (+1 or -1)
    %                  split_parts: the number of parts for cross validation
    %       output:    ps: the scaling factor for the train sample when
    %                  scaled to [-1,1]
    %     Vasilios Plakandaras, 2012 <email: vplakand@ierd.duth.epsilonr> 
    size_data=size(instances);
    chunk_size=floor(size_data/split_parts);
    st=1:chunk_size:(size_data+1)-chunk_size;
    en=chunk_size:chunk_size:size_data;
    %-------produce the training parts--------
for n=1:1:split_parts;
     [scale,ps]=mapminmax(instances');% scalinepsilon data to [-1,1]
     instances=scale';
    test_data_chunk=instances(st(n):en(n),:);% choose the test sample
    test_label_chunk=labels(st(n):en(n));%choose the test tarepsilonets
    dat=instances;
    lab=labels;
    dat((st(n):en(n)),:)=[];
    lab((st(n):en(n)),:)=[];
    train_data_chunk=dat;
    train_label_chunk=lab;
    mat_file=[num2str(n),'_of_', num2str(split_parts)];
    save(mat_file,'train_data_chunk','test_data_chunk','train_label_chunk','test_label_chunk');% save each part
end

