function [favg,f]=testsvm(S,y,n,m,c,b,c1,b1,tt,thrs,maxCl,k,knnthrs) %#ok<INUSD>

if ~exist('k','var')
    k=40; %#ok<NASGU>
end
if ~exist('knnthrs','var')
    knnthrs=5; %#ok<NASGU>
end
if ~exist('thrs','var')
    thrs=0;
end

f=zeros(n,1);

y=double(y);
y(y==0)=-1;

%S(:,sum(S)<1)=[];
S=normalize2(S);

param=sprintf('-c %f -B %f -q',c,b);

for i=1:n
    [X,Y,Xt,Yt]=splitData(S,y,m,i-1);
    %learning
    [model,firstClass]=svmlearn(Xt,Yt,param);
        
    %solve2
    [~,w]=solvesvm(Xt,Yt,model);
    w=bsxfun(@times,w,firstClass);
    l=w>0;
    param2=sprintf('-c %f -B %f -q',c1,b1);
    [model,firstClass]=svmlearn2(Xt,Yt,param2,l,w,tt);
    %solve2
    
    %solve
    [~,weights]=solvesvm(X,Y,model);
    weights=bsxfun(@times,weights,firstClass);

    label=weights>thrs;
    
    %idx=sum(label)>maxCl;
    %w2=weights(:,idx);
    %l2=label(:,idx);
    %[~,idx2]=sort(w2);
    %idx2(1:maxCl,:)=[];
    %l2(idx2,:)=0;
    %label(:,idx)=l2;
    
    %zeros (max weight)
    label=weighted_zeros(X,label,weights);
    %label=knn_zeros(Xt,Yt,X,label,k,knnthrs);
    label=label';
    
    %scoring
    f(i)=fscore(label,Y);
    if (n > 1)
        fprintf('F: %.3f\n',f(i));
    end
end

favg=sum(f)/n;
fprintf('F1-macro: %.4f\n',favg);
