• Remix
  • Share
  • New Entry

on 23 Oct 2021
  • 2
  • 56
  • 0
  • 0
  • 276
% This is a traveling salesman problem
% Use the cities defined by x
n=40;
x=rand(n,1)+1i*rand(n,1);
% Circuit length calculation function
f=@(r)sum(abs(diff(x(r))));
% Improve my algorithm to find a shorter route r
r=1:n;
t=inf;
for i=1:1e5
s=r;
k=sort(randi(n-2,2,1)+1);
m=k(1):k(2);
s(m)=s(flip(m));
u=f(s);
if u<t
r=s;
t=u;
end
end
plot(x(r),'yp:',MarkerS=12,LineW=2)
hold on; plot(rand(2*n,1),rand(2*n,1),'y.'); hold off
axis off
set(gcf,Color=[0.1 0.2 0.4])
Remix Tree
Load full remix tree