function [Train_accuracy,Test_Accuracy,bestd,bestc,bestg,bestcoef,pred_label,func_res,obj_results]=poly_train_unbalanced_new(train_data,train_label,test_data,test_label,split_parts,c_begin,c_step,c_end,...
    g_begin,g_step,g_end,coef_begin,coef_step,coef_end,d_begin,d_step,d_end,tolerance,cut_off)

chunk_cr(train_data,train_label,split_parts);% create the folds for CV
kernel = 1; %polynomial kernel
svm_type=0; % e-SVR
bestacc = 0;% initial value
bestc = c_begin;% initial value
bestg=g_begin;% initial value
bestd=d_begin;% initial value
bestcoef=coef_begin;% initial value
t=1;i=1;j=1;
new_best_c=0;new_best_g=0;new_best_coef=0;
old_best_c=c_end+1;old_best_g=g_end+1;old_best_coef=coef_end+1;
while (abs(new_best_c-old_best_c)>=tolerance &&...
        abs(new_best_g-old_best_g )>=tolerance && abs(new_best_coef-old_best_coef )>=tolerance && t<7);
     if t==1;
        new_step_c=c_step;new_step_g=g_step;new_step_coef=coef_step;new_begin_d=d_begin;
        new_begin_c=c_begin;new_begin_g=g_begin;new_begin_coef=coef_begin;new_step_d=d_step;
        new_end_c=c_end;new_end_g=g_end;new_end_coef=coef_end;new_end_d=d_end;
    else
        new_begin_c=bestc/2; new_begin_g=bestg/2;new_begin_coef=bestcoef/2;new_begin_d=bestd;
        new_end_c=2*bestc;new_end_g=2*bestg;new_end_coef=2*bestcoef;new_end_d=bestd;
        new_step_c=(new_end_c-new_begin_c)/(4*t);
        new_step_g=(new_end_g-new_begin_g)/(4*t);new_step_coef=(new_end_coef-new_begin_coef)/(4*t);new_step_d=1;
    end
    all_c=new_begin_c:new_step_c:new_end_c;
    all_g=new_begin_g:new_step_g:new_end_g;
    all_coef=new_begin_coef:new_step_coef:new_end_coef;
    all_d=new_begin_d:new_step_d:new_end_d;
for d=all_d;% degree search
    for coef=all_coef;%coef search
        for c = all_c;%cost search
             f=length(all_c);
       % h_c = waitbar(0,'1','Name',sprintf('Checking C value %2d of value range %2d',i,f));
            for g=all_g;% gamma search
            
               for a=1:1:split_parts;% search all chunks
                  % waitbar(a/split_parts,h_c,sprintf('Parameter boundaries shift iteration %4d, chunk test:',t))
               mat_file=[num2str(a),'_of_',num2str(split_parts)];
               load(mat_file,'-mat');
               cmd = [' -s ',num2str(svm_type),' -t ',num2str(kernel),' -c ',num2str(c),' -d ',num2str(d),' -r ',num2str(coef)...
                ' -g ',num2str(g),' -q ', ' -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);% train accuracy
       disp(['Cross Validation Accuracy=',num2str(mean_accuracy)])
         if (mean_accuracy > bestacc) || ((mean_accuracy == bestacc) && (c < bestc))
            bestacc = mean_accuracy; bestc = c; bestg=g; bestd=d;bestcoef=coef;
            
        end
        disp([num2str(d),' ',num2str(coef),' ',num2str(c),' ',num2str(g),' ',...
            ' (best degree=',num2str(bestd),' coeff=',num2str(bestcoef),' gamma=',num2str(bestg),...
        ' cost=',num2str(bestc),' rate=',num2str(bestacc)])
               end
                j=j+1;
               % close(h_c)
            end
            j=1;
        i=i+1;
        
        end
        i=1;
    
end
best_step(t,1)=bestc;
best_step(t,3)=bestg;
best_step(t,4)=bestcoef;
new_best_c=bestc;new_best_g=bestg;new_best_coef=bestcoef;
if t<=2
   old_best_c=2*bestc;old_best_g=2*bestg;old_best_coef=2*bestcoef;
else old_best_c=best_step(t-1,1);old_best_g=best_step(t-1,3);old_best_coef=best_step(t-1,4);
end
    t=t+1;
end
Train_accuracy=bestacc;
%-----------Solve the problem for the Test sample

options = [' -s ', num2str(svm_type),' -t ', num2str(kernel),' -c ', num2str(bestc),...
    ' -r ',num2str(bestcoef),' -d ',num2str(bestd),' -g ',num2str(bestg), ' -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 ---
[pred_label,~,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);
for i=1:split_parts;
    mat_file=[num2str(i),'_of_', num2str(split_parts),'.mat'];
    delete (mat_file);
end

function [ps]=chunk_cr(instances,labels,split_parts)
    %-----produces the parts for the cross validation training-----
    %       input:     instances: a mxn matrix of train data, with n being the
    %                  dimension(features)
    %                  labels: a nx1 vector of training targets (+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.gr> 
    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');% scaling 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 targets
    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

