退火算法和遗传算法
一.退火算法
退火算法Matlab程序如下:
[W]=xlsread('D:100个目标经度纬度');
>> x=[W(:,1)];
>> y=[W(:,2)];
>> w=[x y];;d1=[70, 40];
>> w=[d1;w;d1]
w=w*pi/180;%角度化成弧度
d=zeros(102);%距离矩阵初始化
for i=1:101
for j=i+1:102
d(i,j)=6370*acos(w(i,1)-w(j,1))*cos(w(i,2))*cos(w(j,2))+sin(w(i,2))*sin(w(j,2));
end
end
d=d+d';
path=[];long=inf;%巡航路径及长度初始化
rand('state',sum(clock));%初始化随机数发生器
for j=1:1000
path0=[1 1+randperm(100),102];temp=0;
for i=1:101
temp=temp+d(path0(i),path0(i+1));
end
if temp=rand
path=[path(1:c1-1),path(c2:-1:c1),path(c2+1:102)];long=long+df;
end
T=T*at;
if T>path;
>>long;
>>xx=w(path,1);
>>yy=w(path,2);
>> plot(xx,yy,'-o')
[W]=load('D:100个目标经度纬度.txt');
二、遗传算法
[E]=xlsread('D:100个目标经度纬度'); %加载敌方 100 个目标的数据, 数据按照表格中的位置保存在纯文本文件 sj.txt 中
x=[E(:,1)];
y=[E(:,2)];
e=[x y]; d1=[70,40];
e=[d1; e;d1]; e= e*pi/180;
d=zeros(102); %距离矩阵 d
for i=1:101
for j=i+1:102
temp=cos(e(i,1)-e(j,1))*cos(e(i,2))*cos(e(j,2))+sin(e(i,2))*sin(e(j,2));
d(i,j)=6370*acos(temp);
end
end
d=d+d';L=102;w=50;dai=100;
%通过改良圈算法选取优良父代 A
for k=1:w
c=randperm(100);
c1=[1,c+1,102];
flag=1;
while flag>0
flag=0;
for m=1:L-3
for n=m+2:L-1
if d(c1(m),c1(n))+d(c1(m+1),c1(n+1))
flag=1;
c1(m+1:n)=c1(n:-1:m+1);
end
end
end
end
J(k,c1)=1:102;
end
J=J/102;
J(:,1)=0;J(:,102)=1;
rand('state',sum(clock));
%遗传算法实现过程
A=J;
for k=1:dai %产生 0~1 间随机数列进行编码
B=A;
c=randperm(w);
%交配产生子代 B
for i=1:2:w
F=2+floor(100*rand(1));
temp=B(c(i),F:102);
B(c(i),F:102)=B(c(i+1),F:102);
B(c(i+1),F:102)=temp;
end
%变异产生子代 C
by=find(rand(1,w)<0.1);
if length(by)==0
by=floor(w*rand(1))+1;
end
C=A(by,:);
L3=length(by);
for j=1:L3
bw=2+floor(100*rand(1,3));
bw=sort(bw);
C(j,:)=C(j,[1:bw(1)-1,bw(2)+1:bw(3),bw(1):bw(2),bw(3)+1:102]);
end
G=[A;B;C];
TL=size(G,1);
%在父代和子代中选择优良品种作为新的父代
[dd,IX]=sort(G,2);temp(1:TL)=0;
for j=1:TL
for i=1:101
temp(j)=temp(j)+d(IX(j,i),IX(j,i+1));
end
end
[DZ,IZ]=sort(temp);
A=G(IZ(1:w),:);
end
path=IX(IZ(1),:);
long=DZ(1);
xx=e(path,1);yy=e(path,2);
path
long
plot(xx,yy,'-o')
三.改进的遗传算法
clc,clear
[E]=xlsread('D:100个目标经度纬度');
>> x=[E(:,1)];
>> y=[E(:,2)];
>> e=[x y];;d1=[70, 40];
>> e=[d1;e;d1]
e=e*pi/180;%角度化成弧度
d=zeros(102); %距离矩阵 d
for i=1:101
for j=i+1:102
temp=cos(e(i,1)-e(j,1))*cos(e(i,2))*cos(e(j,2))+sin(e(i,2))*sin(e(j,2));
d(i,j)=6370*acos(temp);
end
end
d=d+d';L=102;w=50;dai=100;
%通过改良圈算法选取优良父代 A
for k=1:w
c=randperm(100);
c1=[1,c+1,102];
flag=1;
while flag>0
flag=0;
for m=1:L-3
for n=m+2:L-1
if d(c1(m),c1(n))+d(c1(m+1),c1(n+1))
flag=1;
c1(m+1:n)=c1(n:-1:m+1);
end
end
end
end
J(k,c1)=1:102;
end
J=J/102;
J(:,1)=0;J(:,102)=1;
rand('state',sum(clock));
%遗传算法实现过程
A=J;
for k=1:dai %产生 0~1 间随机数列进行编码
B=A;
%交配产生子代 B
for i=1:2:w
ch0=rand;ch(1)=4*ch0*(1-ch0);
for j=2:50
ch(j)=4*ch(j-1)*(1-ch(j-1));
end
ch=2+floor(100*ch);
temp=B(i,ch);
B(i,ch)=B(i+1,ch);
B(i+1,ch)=temp;
end
%变异产生子代 C
by=find(rand(1,w)<0.1);
if length(by)==0
by=floor(w*rand(1))+1;
end
C=A(by,:);
L3=length(by);
for j=1:L3
bw=2+floor(100*rand(1,3));
bw=sort(bw);
C(j,:)=C(j,[1:bw(1)-1,bw(2)+1:bw(3),bw(1):bw(2),bw(3)+1:102]);
end
G=[A;B;C];
TL=size(G,1);
%在父代和子代中选择优良品种作为新的父代
[dd,IX]=sort(G,2);temp(1:TL)=0;
for j=1:TL
for i=1:101
temp(j)=temp(j)+d(IX(j,i),IX(j,i+1));
end
end
[DZ,IZ]=sort(temp);
A=G(IZ(1:w),:);
end
path=IX(IZ(1),:)
long=DZ(1)
xx=e(path,1);yy=e(path,2);
path
long
plot(xx,yy,'-o')