遗传算法单点交叉MATLAB,遗传算法求解旅行商问题里中的单点交叉

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

单点交叉遇到重复的基因,怎么把重复的删去并且将缺失的添加回来。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值