在这个模拟退火算法中,我已经得出了距离d矩阵,然后利用其进行蒙特罗算法和模拟退火算法。代码是一本书上的,对于自己的数据进行了部分修改,一直出现索引大于矩阵维度的错误。代码如下:
d=load('data.txt');
path=[];long=inf;
rand('state',sum(clock));
for j=1:1000
path0=[1 1+randperm(50),52]; temp=0;
for i=1:51
temp=temp+d(path0(i),path0(i+1));
end
if temp<long
path=path0; long=temp;
end
end
e=0.1^30;L=20000;at=0.999;T=1;
for k=1:L
c=2+floor(50*rand(1,2));
c=sort(c); c1=c(1);c2=c(2);
df=d(path(c1-1),path(c2))+d(path(c1),path(c2+1))-d(path(c1-1),path(c1))-d(path(c2),path(c2+1));
if df
path=[path(1:c1-1),path(c2:-1:c1),path(c2+1:52)]; long=long+df;
elseif exp(-df/T)>rand
path=[path(1:c1-1),path(c2:-1:c1),path(c2+1:52)]; long=long+df;
end
T=T*at;
if T<e
break;
end
end
path, long
xx=sj(path,1);yy=sj(path,2);
plot(xx,yy,'-*')
其中d为52×52矩阵,大部分数是inf,其它的是实数。求解答