function [Train_accuracy,Test_Accuracy,bestc,bestg,pred_label,func_res,obj_results]=rbf_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,tolerance,cut_off)

%
%   conduct a cross validation train with search within the c,g  boundaries
%   construction based on the rbf kernel e^((gamma*((u-v)^2))

% Inputs
%
%   c:Cost,g:gamma
%   begin,end,step self explanatory
%   split_parts: the cross validation parts (n-fold)
%   train_data is a mxn matrix where m are the observations and n the attributes (features)
%   train_label an mx1 matrix with the class labels (targets)
%   Test_data, test_label same form as train
%Outputs
%
%   Train Accuracy: Test of Cross Validation accuracy (Train accuracy)
%   Test_Accuracy: Test accuracy of the model on the test sample
%   bestc,bestg: best settings
%   Full a matrix with all the results together
%
%   Vasileios Plakandaras, 2012 <email: vplakand@ierd.duth.gr>
chunk_cr(train_data,train_label,split_parts);% create the folds for CV
kernel = 2; %rbf kernel
svm_type=0; % C-SVM 
bestacc = 0; % initial value
bestc = c_begin;% initial value
bestg=g_begin;% initial value in order c,g, chunk accuracies, train accuracy
t=1;i=1;j=1;
iter_acc(split_parts,1)=0;
new_best_c=0;new_best_g=0;
old_best_c=c_end+1;old_best_g=g_end+1;
while (abs(new_best_c-old_best_c)>=tolerance && abs(new_best_g-old_best_g )>=tolerance && t<11);
     if t==1;
        new_step_c=c_step;new_step_g=g_step;
        new_begin_c=c_begin;new_begin_g=g_begin;
        new_end_c=c_end;new_end_g=g_end;
    else
        new_begin_c=bestc/2; new_begin_g=bestg/2;
        new_end_c=2*bestc;new_end_g=2*bestg;
        new_step_c=(new_end_c-new_begin_c)/(4*t);
        new_step_g=(new_end_g-new_begin_g)/(4*t);
       
    end
    all_c=new_begin_c:new_step_c:new_end_c;
    all_g=new_begin_g:new_step_g:new_end_g;
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
%        waitbar(j/length(all_g),h_c,sprintf('Parameter boundaries shift iteration %4d ',t))
        for a=1:1:split_parts;% search all chunks
            mat_file=[num2str(a),'_of_',num2str(split_parts)];
            load(mat_file,'-mat');
            cmd = [' -s ', num2str(svm_type),' -t ',num2str(kernel),' -c ',num2str(c),' -g ',num2str(g), ' -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(1,1) > bestacc) || ((mean_accuracy(1,1) == bestacc) && (c < bestc))
            bestacc = mean_accuracy; bestc = c; bestg=g;
        end
        disp([num2str(c),' ',num2str(g),' ',...
            'Best (gamma=',num2str(bestg),' cost=',num2str(bestc),' Accuracy=',num2str(bestacc),'%)'])
        
   j=j+1;
    end
   j=1;
   i=i+1;
  % close(h_c)
end
best_step(t,1)=bestc;
best_step(t,2)=bestg;
new_best_c=bestc;new_best_g=bestg;
if t<=2
   old_best_c=2*bestc;old_best_g=2*bestg;
else old_best_c=best_step(t-1,1);old_best_g=best_step(t-1,2);
end
t=t+1;
i=1;
end
Train_accuracy=bestacc;
%-----------Solve the problem for the Test sample

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

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

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

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

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

