MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   MATLAB论坛 (https://www.labfans.com/bbs/forumdisplay.php?f=6)
-   -   [求助]遗传算法选择部分的问题 (https://www.labfans.com/bbs/showthread.php?t=7198)

431126 2009-03-30 17:55

[求助]遗传算法选择部分的问题
 
%Generic Algorithm for function f(x1,x2) optimum
clear all;
close all;

%Parameters
Size=50;
G=200;
CodeL=10;

umax=2.048;
umin=-2.048;

E=round(rand(Size,2*CodeL)); %Initial Code初始化代码
%function[bin_gen,bits]=encoding(min_var,max_var,scale_var,popsize)
%bits=ceil(log2(max_var,min_var)./scale_var);
%bin_gen=randint(popsize,sum(bits));
%实际精度为scale_dec=(max_var-min_var)/(2^bits-1);

%Main Program主程序
for k=1:1:G
time(k)=k;

for s=1:1:Size
m=E(s,:);
y1=0;y2=0;

%Uncoding解码
m1=m(1:1:CodeL);
for i=1:1:CodeL
y1=y1+m1(i)*2^(i-1);
end
x1=(umax-umin)*y1/1023+umin;% a=amin+b*(amax-amin)/(2^m-1)
m2=m(CodeL+1:1:2*CodeL);
for i=1:1:CodeL
y2=y2+m2(i)*2^(i-1);
end
x2=(umax-umin)*y2/1023+umin;

F(s)=100*(x1^2-x2)^2+(1-x1)^2;
end


%******Step 1 : Evaluate Bestfi ******

fi=100*exp(-100*F); %Fitness Function
fi=F;
[Orderfi,indexfi]=sort(fi,'descend'); %Arranging fi small to bigger
Bestfi=Orderfi(Size); %Let Bestfi=max(fi)
BestS=E(indexfi(Size),:); %Let BestS=E(m), m is the Indexfi belong to max(fi)
bfi(k)=Bestfi;

%****** Step 2 : Select and Reproduct Operation******
fi_sum=sum(fi);
fi_Size=(Orderfi/fi_sum)*Size;

fi_S=floor(fi_Size); %Selecting Bigger fi value

kk=1;
for i=1:1:Size
for j=1:1:fi_S(i) %Select and Reproduce
TempE(kk,:)=E(indexfi(i),:);
kk=kk+1; %kk is used to reproduce
end
end

%************ Step 3 : Crossover Operation ************
pc=0.80;
n=ceil(20*rand);
for i=1:2:(Size-1)
temp=rand;
if pc>temp %Crossover Condition
for j=n:1:20
TempE(i,j)=E(i+1,j);
TempE(i+1,j)=E(i,j);
end
end
end
TempE(Size,:)=BestS;
E=TempE;

%************ Step 4: Mutation Operation **************
%pm=0.001;
%pm=0.001-[1:1:Size]*(0.001)/Size; %Bigger fi, smaller Pm
%pm=0.0; %No mutation
pm=0.1; %Big mutation

for i=1:1:Size
for j=1:1:2*CodeL
temp=rand;
if pm>temp %Mutation Condition
if TempE(i,j)==0
TempE(i,j)=1;
else
TempE(i,j)=0;
end
end
end
end

%Guarantee TempPop(30,:) is the code belong to the best individual(max(fi))
TempE(Size,:)=BestS;
E=TempE;
end

Min_Value=Bestfi
BestS
x1
x2
figure(1);
plot(time,bfi);
xlabel('times');ylabel('Best F');


以上是一个Rosenbrock 函数求最小值的程序,程序中的选择部分用的是轮盘赌的方式,我想请教一下哪位高手会用联赛选择的方式做一下选择部分啊,谢谢了!

anbcjys 2009-04-01 13:28

回复: [求助]遗传算法选择部分的问题
 
那就选择相应的部分修改成你需要的程序即可


所有时间均为北京时间。现在的时间是 05:09

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.