clear;close all;
sj=[111.38 40.48
103.13 38.28
103.50 38.03
104.04 30.39
106.33 29.33
108.54 34.16
112.33 37.51
116.28 39.54
117.10 39.10
114.26 38.03
117.02 36.40
113.42 34.44
117.17 31.52
114.20 30.37
118.26 32.03
121.26 31.12
120.10 30.15];
rng default;
pc = 0.9;
pm = 0.5;
d1=sj(1,:);
sj0=[sj;d1];
num = size(sj0,1);
sj=sj0*pi/180;
d=zeros(num);
for i=1:num-1
for j=i+1:num
temp=cos(sj(i,1)-sj(j,1))*cos(sj(i,2))*cos(sj(j,2))+sin(sj(i,2))*sin(sj(j,2));
d(i,j)=6370*acos(temp);
end
end
d=d+d';
w=50;
T=200;
L=num;
for k=1:w
c=randperm(num-2);
c1=[1,c+1,num];
J(k,c1)=1:num;
end
J=J/num;J(:,1)=0;J(:,num)=1;
rand('state',sum(clock));
rng default;
A=J;
for k=1:T
B=A;
bb=find(rand(1,w)
B=A(bb,:);
L3=length(bb);
for i=1:L3
c=randperm(L3);
Ind = randperm(L3,2);
F=2+floor((num-2)*rand(1));
B(i,:)=[B(Ind(1),1:F-1),B(Ind(2),F:num)];
end
by=find(rand(1,w)
if isempty(by)
by=floor(w*rand(1))+1;
end
C=A(by,:);
L4=length(by);
for j=1:L4
[~,C0] = sort(C(j,:));
bw=randperm(num-2,2)+1;
flag=1;
while flag>0
flag=0;
for m=bw(1):bw(2)-3
for n=m+2:bw(2)-1
if d(C0(m),C0(n))+d(C0(m+1),C0(n+1))
flag=1;
C0(m+1:n)=C0(n:-1:m+1);
end
end
end
end
C(j,C0)=1:num;
C(j,:)=C(j,:)/num;
C(j,1)=0;
C(j,num)=1;
end
G=[A;B;C];
TL=size(G,1);
[~,IX]=sort(G,2);
temp = [];temp(1:TL)=0;
for j=1:TL
for i=1:num-1
temp(j)=temp(j)+d(IX(j,i),IX(j,i+1));
end
end
[temp,I] = unique(temp);
[DZ,IZ]=sort(temp);
cost(k) = DZ(1);
A=G(I(IZ(1:w)),:);
end
figure; plot(cost);
xlabel('迭代次数');ylabel('目标函数');
path=IX(IZ(1),:)
long=DZ(1);
x=sj0(path,1);y=sj0(path,2);
figure;plot(x,y,'-o')
xlabel('经度(^oE)');ylabel('纬度(^oN)');
for i = 1:size(sj0,1)-1
text(sj0(i,1)+0.2,sj0(i,2)+0.2,num2str(i))
end
单点交叉遇到重复的基因,怎么把重复的删去并且将缺失的添加回来。