求助 哪有问题啊!!!!!
cities= ;t=30+1; %城市的数目是30个
s=10; %样本的数目是1400个
x=cities(1,:);
y=cities(2,:);
pop=zeros(s,t); %得初始的pop矩阵,矩阵的最后一列表示所在行的样本的路径距离
for i=1:s
pop(i,1:t-1)=randperm(t-1); %随机产生1—(t-1)的t-1个打乱的随机数
end
M=100;
for l=1:1:2for i=1:2:s-1
%随机选择一个交叉点
m=randperm(t-3)+1;
crosspoint=min(m(1),m(2));
%任意两行交叉
x1=pop(i,:);
x2=pop(i+1,:);
k=0;
for K=1:10
while k==0
if sqrt((x(x1(crosspoint))-x(x2(crosspoint)))^2+(y(x1(crosspoint))-y(x2(crosspoint)))^2)<M|sqrt((x(x2(crosspoint))-x(x1(crosspoint)))^2+(y(x2(crosspoint))-y(x1(crosspoint)))^2)<M
pop(i,:)=;
pop(i+1,:)=;
%检查x1左边的重复性并得到x1的左边
for j=1:crosspoint
while find(x1(crosspoint+1:t)==x1(j))
zhi=find(x1(crosspoint+1:t)==x1(j)); %确定重复位置
temp=x2(crosspoint+zhi);
x1(j)=temp;
end
end
for j=1:crosspoint
while find(x2(crosspoint+1:t)==x2(j))
zhi=find(x2(crosspoint+1:t)==x2(j)); %确定重复位置
temp=x1(crosspoint+zhi);
x2(j)=temp;
end
end
k=1;
else
m=randperm(t-3)+1;
crosspoint=min(m(1),m(2));
k=0;
end
end
end
end
endbestL=min(pop(:,t))
J=pop(:,t);
fi=1./J;
=sort(fi); %对于fi进行排序
BestS=pop(Indexfi(s),:); %得到最短路
I=BestS;
for i=1:1:t-1
x1(i)=x(I(i));
y1(i)=y(I(i));
end
x1(t)=x(I(1));
y1(t)=y(I(1));
cities_new=;
disp('Best Route is:');disp(cities_new);
pos=;
lentemp=0;
for i=1:1:t-1
temp=sqrt((pos(1,i)-pos(1,i+1))^2+(pos(2,i)-pos(2,i+1))^2);
lentemp=lentemp+temp;
end
disp('Shortest Length is:');disp(lentemp);
figure(1);
subplot(1,2,1); %窗口分割的左边部分
x(t)=x(1);y(t)=y(1);
plot(x,y,'-or');
xlabel('X axis'), ylabel('Y axis'), title('原始路径');
axis();
axis();
axis on
hold on;
subplot(1,2,2); %窗口分割的右边部分
plot(x1,y1,'-or');
xlabel('X axis'), ylabel('Y axis'), title('最新的路径');
axis();
axis();
axis on
这只是遗传算法的交叉部分,现在有点问题,就是为什么去掉红色字体就可以运行出来,而加上后程序就没反应了呢?
那位高人能找出原因,麻烦看看啊 ,不胜感激啊!
页:
[1]