%Montecarlo simulation to determine the order that maximizes the
%%probability of detection 
lambda1=rand(1,16)*0.3; 
lambda2=rand(1,16)*0.3; 
LAMBDA=[lambda1;lambda2]; 
V=probcorrect(LAMBDA');
%ANTCOLONY STARTS 
MATRIX=zeros(16,16);
BESTSOL=[];
BESTVAL=[];
N=100; 
for i=1:1:N
    [P,Q]=sort(rand(1,16));
    ORDER{i}=Q;
    LAMDAORDER{i}=LAMBDA(:,Q);
end
for iteration=1:1:100
    for i=1:1:N 
        LAMDAORDER{i}=LAMBDA(:,ORDER{i});
        VAL(i)=probcorrect(LAMDAORDER{i}');
    end
    [P2,Q2]=max(VAL); 
    BESTSOL{iteration}=ORDER{Q2};
    BESTVAL(iteration)=P2;
    %UPDATAE THE MATRIX 
    for i=1:1:16 
        for j=1:1:N 
            MATRIX(i,ORDER{j}(i))=VAL(j)+0.5*MATRIX(i,ORDER{j}(i));
        end
    end
    %Selecting the next set of order 
    temp=zeros(1,16); 
    COL=[]; 
    for j=1:1:N 
        COL=[];
        [P,Q]=sort(rand(1,16));
        temp1=MATRIX(Q(1),:);
        [P1,Q1]=max(temp1);
        temp(Q(1))=Q1(1);
        COL=[COL Q1];
        for i=2:1:16 
            temp1=MATRIX(Q(i),:);
            temp1(COL)=-1000;
            [P1,Q1]=max(temp1);
            temp(Q(i))=Q1(1);
            COL=[COL Q1]; 
        end
        ORDER{j}=temp;
    end
end
[U,V]=max(BESTVAL); 
ORDERCHOSEN=BESTSOL{V(1)}; 
plot(BESTVAL) 
xlabel('Iteration') 
ylabel('Cost function value')
