投影寻踪 遗传算法MATLAB程序
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
clear
close all
Alpha=0.1;
X=load('d:\data.txt');
for k=1:21
eval(sprintf('syms a%d',k));
end
a=[a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a 20,a21];
[n,p]=size(X);
x=zeros(n,p);
Xjmax=max(X);
Xjmin=min(X);
for i=1:n
x(i,:)=(X(i,:)-Xjmin)./(Xjmax-Xjmin);
end
%构造投影指标
for i=1:n
Z(i)=sum(a.*x(i,:));
end
%计算投影指标函数
meanZ=mean(Z);
Sa=0;
for k=1:n
sa=(Z(i)-meanZ).^2;
Sa=Sa+sa;
Sa=sqrt(Sa/n);
end
R=Alpha*Sa;
Da=0;
for k=1:n
rik=abs(Z(i)-Z(k));
%if R>rik
Da=Da+rik;
%end
end
Qa=Sa*Da;
%选择操作
function ret =select(individuals,sizepop)
individuals.fitness=1./(individuals.fitness);
sumfitness=sum(individuals.fitness);
sumf=individuals./fitness;
index=[];
for i =1:sizepop
pick=rand;
while pick==0
pick=rand;
end
for j =1:sizepop
pick =pick-sumf(j);
if pick<0
index=[index j];
break;
end
end
end
indivlduals.chrom=indivuduals.chrom(index,:); individuals.fitness=individuals.fitness(index);
ret=individuals;
%交叉操作
function ret=Cross(pcross,lenchrom,chrom,sizepop,bound) for i=1:sizepop
pick = rand(1,2);
while prod(pick)==0
pick=rand(1,2);
end
index=ceil(pick.*sizepop);
pick=rand;
while pick==0
pick=rand;
end
if pick>pcross
continue;
end
flag=0;
while flag==0
pick=rand;
while pick==0
pick=rand;
end
pos=ceil(pick.*sum(lenchrom));
pick=rand;
v1=chrom(index(1),pos);
v2=chrom(index(2),pos);
chrom(index(1),pos)=pick*v2+(1-pick)*v1;
chrom(index(2),pos)=pick*v1+(1-pick)*v2;
flag1=test(lenchrom,bound,chrom(index(1),:),fcode);
flag2=test(lenchrom,bound,chrom(index(2),:),fcode);
if flag1*flag2==0
flag=0;
else flag=1;
end
end
end
ret= chrom;
% 变异操作
function ret=Mutation(pmutation,lenchrom,chrom,sizepoppop,bound) pick=rand;
while pick==0
pick=rand;
end
index=ceil(pick*sizepop);
pick=rand;
if pick>pmutation
continue;
end
flag=0;
while flag==0
pick =rand;
while pick==0
pick=rand;
end
pos=ceil(pick*sum(lenchrom));
v=chrom(i,pos);
v1=v-bound(pos,1);
v2=bound(pos,2)-v;
pick=rand;
if pick>0.5
delta=v2*(1-pick^((1-pop(1)/pop(2))^2));
chrom(i,pos)=v+delta;
else
delta=v1*(1-pick^((1-pop(1)/pop(2))^2));
chrom(i,pos)=v-delta;
end
flag=test(lenchrom,bound,chrom(1,:),fcode);
end
end
end
ret=chrom;
end
end
主函数
clc
clear
maxgen=200;
sizepop=20;
pcross=[0.6];
pmutation=[0.01];
lenchrom=[1 1 1 1 1];
bound=[0 0.9*pi;0 0.9*pi;0 0.9*pi;0 0.9*pi;0 0.9*pi;];
individuals=struct('fitness',zeros(1,sizepop),'chrom',[]); avgfitness=[];
bestfitness=[];
bestchrom=[];
for i =1:sizepop
individuals.chrom(1,:)=code(lenchrom,bound);
x=individuals.chrom(i,:);
individuals,fitness(i)=fun(x);
end
[bestfitness, bestindex]=max(individuals.fitness);