What's wrong with this Simulated Annealing code ?
Info
Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.
Ältere Kommentare anzeigen
This is code for SA, to solve TSP. Can someone check it, where it go wrong ? I want: 500 particles to be used; 1000 iterations
function:
function [c] = cost(x,y)
n=length(x);
c=0;
for i=1:n-1
c =c+ dist(x(i), x(i+1))
end
Program:
n=20;
x=rand(1,n); y=rand(1,n);
x(end+1) = x(1);
y(end+1) = y(1);
figure(1);
hold off;
g=plot(x,y,'.r');
set(g,'MarkerSize',20);
c0=cost(x,y);
k=1;
nt=50; nr=200;
for i=1:nt
T=1.0 -(i-1)/nt
for j=1:nr
ic1 = randi([2,n]);
ic2 = randi([2,n]);
xs=x(ic1); ys=y(ic1);
x(ic1)=x(ic2); y(ic1)=y(ic2);
x(ic2)=xs; y(ic2)=ys;
p=rand();
c=cost(x,y);
if (c < c0 | p < exp(-(c-c0)/(k*T)))
c0=c;
else
xs=x(ic1); ys=y(ic1);
x(ic1)=x(ic2); y(ic1)=y(ic2);
x(ic2)=xs; y(ic2)=ys;
end
cp(j,i)=c0;
end
figure(2);
plot(cp(:));
drawnow;
figure(1); hold off;
g=plot(x,y,'.r');
set(g,'MarkerSize',20); hold on;
plot(x,y,'b');
g=plot(x(1),y(1),'.g');
set(g,'MarkerSize',30);
drawnow;
end
Antworten (0)
Diese Frage ist geschlossen.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!