
function s=normInf(x)
    s=0
    for i=1:length(x)
        if s < abs(x(i))
            s=abs(x(i))
        end 
    end
endfunction

function L=ini(nbPop,n)
    L=[]
    for i=1:nbPop
        L=[L,2*rand(n,1)-ones(n,1)]
    end
endfunction


function y=croisement(x1,x2)
    y=[(x1+x2)/2,(3*x1-x2)/2]
endfunction

function M=mutation(L,taux)
    M=[]
    n=size(L)
    for i=1:n(2)
        if i <= n(2)/2 | rand() > taux
            M=[M,L(:,i)]
        else
            M=[M,L(:,i)+0.01*normInf(L(:,i))*(2*rand(n(1),1)-ones(n(1),1))]
        end            
    end
endfunction

function c=critere(A,b,x)
    c=normInf(A*x-b)
endfunction

function s=evaluation(A,b,L,eps)
    s=0
    n=size(L)
    for i=1:n(2)
        if normInf(A*L(:,i)-b) < eps
            s=i
        end
    end
endfunction

function T=triBulle(L,f)
    T=L
    n=size(L)
    for i=1:n(2)
        for j=1:(n(2)-1)
            if f(T(:,j)) > f(T(:,j+1))
                a=T(:,j)
                T(:,j)=T(:,j+1)
                T(:,j+1)=a
            end
        end
    end
endfunction

function S=selection(L,A,b)
    
    deff('s=f(x)','s=critere(A,b,x)')
    T=triBulle(L,f)
    n=size(L)
    S=[]
    for i=1:n(2)/2
        S=[S,T(:,i)]
    end
    
endfunction

function x=algoEvo(A,b,eps,nbPop,taux)
    
    L=ini(nbPop,length(b))
    
    while(evaluation(A,b,L,eps) == 0)
        
        L=selection(L,A,b)
        //disp(critere(A,b,L(:,1)))
                
        for i=1:2:(nbPop/2-1)
            T=croisement(L(:,i),L(:,i+1))
            L=[L,T(:,1),T(:,2)]
        end
        
        L=mutation(L,taux)
    end
    
    x=L(:,evaluation(A,b,L,eps))
    
endfunction


function S=algoEvo2(A,b,eps,nbPop,taux)
    S=[]
    L=ini(nbPop,length(b))
    
    while(evaluation(A,b,L,eps) == 0)
        
        L=selection(L,A,b)
        S=[S,L(:,1)]
                
        for i=1:2:(nbPop/2-1)
            T=croisement(L(:,i),L(:,i+1))
            L=[L,T(:,1),T(:,2)]
        end
        
        L=mutation(L,taux)
    end    
endfunction

function x=jacobi(A,b,eps)
    x0=zeros(length(b),1)
    x1=x0
    while critere(A,b,x0) > eps
        for i=1:length(b)
            s=0
            for j=1:length(b)
                if i <> j
                    s=s+A(i,j)*x0(j)
                end
            end
            x1(i)=(1/A(i,i))*(b(i)-s)
        end
        x0=x1
        //disp(critere(A,b,x0))
    end
    x=x0
endfunction
