N=20;
C=N;
T=100000;
valeurs=zeros(1,T);
/////////////////////////////////
//villes=grand(2,N,'unf',0,1);
//fprintfMat("villes.txt",villes);
V=fscanfMat("villes.txt");

function z=mouv(traj)
    i=grand(2,1,'uin',1,N);
    //disp(i(1),i(2));
    l=traj(i(1));
    traj(i(1))=traj(i(2));
    traj(i(2))=l;
    z=traj;
endfunction

function z=dd(x,y)
    z=sqrt((x(1)-y(1))^2+(x(2)-y(2))^2);
endfunction

function z=distance(traj)
    s=0;
    for i=1:N-1
        s=s+dd(V(:,traj(i)),V(:,traj(i+1)));
    end
    s=s+dd(V(:,traj(N)),V(:,traj(1)));
    z=s;
endfunction

function z=pos_part(x)
    z=max(0,x);
endfunction

V=fscanfMat("villes.txt");
trajet=grand(1,'prm',[1:1:N]');
for i=1:T
    bet=sqrt(i+exp(1))/C;
    traj_bis=mouv(trajet);
    u=grand(1,1,'unf',0,1);
    //disp(pos_part(distance(traj_bis)-distance(trajet)));
    if (u<exp(-bet*pos_part(distance(traj_bis)-distance(trajet)))) then
        trajet=traj_bis;
        if (distance(traj_bis)-distance(trajet)>0) then
            disp("accepte");
        end
    end
    disp(i,distance(trajet));
    valeurs(i)=distance(trajet);
end

clf();
for i=1:N-1
    plot(V(1,trajet(i)),V(2,trajet(i)),'rd');
    plot([V(1,trajet(i)),V(1,trajet(i+1))],[V(2,trajet(i)),V(2,trajet(i+1))]);
end
plot(V(1,trajet(N)),V(2,trajet(N)),'rd');
plot([V(1,trajet(N)),V(1,trajet(1))],[V(2,trajet(N)),V(2,trajet(1))]);
scf();
plot([1:1:T],valeurs);
