function nested_perf(T,n,logplot)
%   Nested Performace profiles
%   Using Dolan and More's matlab code described in
%   Benchmarking optimization software with performance profiles, E.D. Dolan and J.J. More',
%   Mathematical Programming, 91 (2002), 201--213.

%   nested_pref(T,n,logplot)-- produces a nested performace profile as described in
%   Nested Performance Profiles for Benchmarking  Software, R. Hekmati and
%   H.Mirhajianmoghadam , arXiv:1809.06270v1
%   


% Each column of the matrix T defines the performance data for a solver.
% Failures on a given problem are represented by a NaN.
% The optional argument logplot is used to produce a  logplot (base 2) .
% let ns= the number of solvers
% n is the number of solvers that we want to compare pairwise , 1 < n < n_s

%EXAMPLE :
% nested_perf(T,ns-1,1)  


if (nargin < 2) logplot = 0; end
colors  = ['m' 'b' 'r' 'g' 'c' 'k' 'y'];
lines   = [':', '-', '--'  ,':', '-', '--',':' ];
markers = ['x' '*' 's' 'd' 'v' '^' 'o' ];
[np,ns] = size(T);
TT=T;
ind_of_best=[];%The index of best solvers that will be eliminated
ind=1:ns;%The whole indices
rr=[];
if n>ns-1
    fprintf('The number of nested comparisions should be less than the number of solvers ns, lets set n=ns-1')
    n=ns-1;
end
for k=1:n
    minperf = min(TT,[],2);% Minimal performance per solver
    r = zeros(size(TT,1),size(TT,2));
    ind=setdiff(ind,ind_of_best);%Index of remaining solvers
    for p = 1: np % Compute ratios and divide by smallest element in each row
        r(p,ind) = TT(p,:)/minperf(p);
    end
    The_best_ratios=rr(:,ind_of_best);%The eliminated solvers ratios
    for j=1:np%  Check the eliminated solver(s) 
        if size(The_best_ratios,1)~=0
        if The_best_ratios(j)~=1% if the eliminated solver is not the best for new set op problems then update it
            The_best_ratios(j)=The_best_ratios(j)/minperf(j);
        end
        end
    end    
    r(:,ind_of_best)=The_best_ratios;
    Nested_PP{k}=r;%save the nested performance profiles in a cell
    rr=r;
    rr(isnan(r))=0;% remove the nan to find the best solver
    ss=rr(:,ind);
    for l=1:size(ss,2)
    number_of_ones(l)=size(find(ss(:,l)==1),1);%The number of problems that solver l can solve by least Iter/time
    end
    [g,max_n]=max(number_of_ones);
    t=max_n(1);%The solver with maximum number of ratios=1;
    ind_of_best=[ind_of_best,ind(t)];%Update the best solver
    TT(:,t)=[];%Elimination of the best solver
end
su=zeros(np,ns);
for k=1:n
    su=su+Nested_PP{k};%sum of the nested performance profiles
end
r=su/n;%The overall performance profile

if (logplot) r = log2(r); end
max_ratio = max(max(r));
% Replace all NaN's with twice the max_ratio and sort.
r(find(isnan(r))) = 2*max_ratio;
r = sort(r);
% Plot stair graphs with markers.
for s = 1: ns
    [xs,ys] = stairs(r(:,s),[1:np]/np);
    option = [lines(s) colors(s) markers(s)];
    hold on
    %subplot(2,2,4)
    plot(xs,ys,option,'MarkerSize',4);
    hold on;
end

axis([ 0 1.1*max_ratio 0 1 ]);
