
clear all
%figure(1);clf

figdir = ...
    'C:\Documents and Settings\Bruce Land\My Documents\Matlab\Seeley\SeeleyVersion14\R23\';
figfilebase = 'S0.11667D0.1N6';  

quorum = 50; %number of bees to make a 'win'

%make a time vector
dt = 1/10; %minute
endTime = 30*60; %default end time in minutes
time=0:dt:endTime;

%init the winner counts
nNest=4;
wins(1:nNest)=0;
winRatio(1:nNest)=0;
winTime(1:nNest) = 0;
sumNotWinner(1:nNest) = 0;

for i=1:100
    clear nScouts nDancers nAtNest
    load([figdir,figfilebase,'R',num2str(i),'.mat']);
    
    for nest=1:nNest
        allQuo = find(nAtNest(:,nest)>=quorum);
        if length(allQuo>0)
            Tquorum(nest) = time(allQuo(1)); %the first time it hits quorum
            %Nquorum(nest) = nAtNest(allQuo(1),nest); %actual number
        else
            Tquorum(nest) = inf; %never it hits quorum
        end
    end
    [minTime,Winner] = min(Tquorum);
    if (minTime<inf) %was there an actual winner?
        Winner = Winner(1); %make sure there is just one winner
        wins(Winner) = wins(Winner) + 1;
        notWinner = find(1:nNest~=Winner);
        minQuo = find(time==minTime);
        %winRatio(Winner) = winRatio(Winner) + ...
        %    quorum/sum(nAtNest(minQuo,notWinner));
        sumNotWinner(Winner) = sumNotWinner(Winner) + ...
            sum(nAtNest(minQuo,notWinner));
        winTime(Winner) = winTime(Winner) + minTime;
    end   
end

fprintf(1,'\r\rData Set:%s\r',figfilebase)
fprintf(1,'Number of wins:%5d %5d %5d %5d   sum:%d\r',wins, sum(wins))
fprintf(1,'Avg Win Ratios:%5.2f %5.2f %5.2f %5.2f\r',50./(sumNotWinner./wins))
fprintf(1,'Avg Win Times:%6.2f %5.2f %5.2f %5.2f\r',winTime./wins/60)

