遗传算法入门程序例子
2013-06-30 17:04
302 查看
参考:《智能控制》作者刘金琨,电子工业出版社
利用遗传算法求取Rosenbrock函数的最大值
该函数有两个局部极大值点,分别是f(2.048,-2.048)=3897.7342, f(-2.048,-2.048)=3905.9262;其中后者为全局最大点
Matlab程序:
%Generic Algorithm for function f(x1,x2) optimum
clear all;
close all;
%Parameters
Size=80; % 80个群
G=100;
CodeL=10;
umax=2.048;
umin=-2.048;
E=round(rand(Size,2*CodeL)); % Initial Code
%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;
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
Ji=1./F;
%****** Step 1 : Evaluate BestJ ******
BestJ(k)=min(Ji);
fi=F; %Fitness Function
[Oderfi,Indexfi]=sort(fi); %Arranging fi small to bigger
Bestfi=Oderfi(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=(Oderfi/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.60;
n=ceil(20*rand);% 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); %对没有交叉的某个群体,matlab语法设置该行数据为0
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
Max_Value=Bestfi
BestS
x1
x2
figure(1);
plot(time,BestJ);
xlabel('Times');ylabel('Best J');
figure(2);
plot(time,bfi);
xlabel('times');ylabel('Best F');
利用遗传算法求取Rosenbrock函数的最大值
该函数有两个局部极大值点,分别是f(2.048,-2.048)=3897.7342, f(-2.048,-2.048)=3905.9262;其中后者为全局最大点
Matlab程序:
%Generic Algorithm for function f(x1,x2) optimum
clear all;
close all;
%Parameters
Size=80; % 80个群
G=100;
CodeL=10;
umax=2.048;
umin=-2.048;
E=round(rand(Size,2*CodeL)); % Initial Code
%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;
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
Ji=1./F;
%****** Step 1 : Evaluate BestJ ******
BestJ(k)=min(Ji);
fi=F; %Fitness Function
[Oderfi,Indexfi]=sort(fi); %Arranging fi small to bigger
Bestfi=Oderfi(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=(Oderfi/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.60;
n=ceil(20*rand);% 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); %对没有交叉的某个群体,matlab语法设置该行数据为0
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
Max_Value=Bestfi
BestS
x1
x2
figure(1);
plot(time,BestJ);
xlabel('Times');ylabel('Best J');
figure(2);
plot(time,bfi);
xlabel('times');ylabel('Best F');
相关文章推荐
- 遗传算法入门例子和总结
- TensorFlow入门教程:1:安装和第一个例子程序
- Direct3D 9 入门例子程序 圆锥体
- struts2入门-第一个例子==》第一个struts2实例——HelloWorld==》Struts2.0搭建和简单实例==>写一个Steuts2的经典入门案例,做了一个登陆程序作为入门例子
- 很不多的ICE架构入门学习例子-ping程序
- 遗传算法入门例子和总结
- junit3和junit4的一个入门例子程序
- windows service程序的入门范例(三)——真正好用的实战例子
- 一个非常好的理解遗传算法的例子 强烈推荐入门
- 很不多的ICE架构入门学习例子-ping程序
- SharePoint EventHandler 开发入门程序例子
- TensorFlow入门教程:1:安装和第一个例子程序
- SparkStreaming的一个入门例子程序
- jena处理RDF数据入门程序小例子
- 图解Windows下开发Objective-C程序之二 - Objective-C入门例子
- JMS入门例子
- 一、从计算机结构到汇编程序入门
- c++读写文件和测试程序运行时间的例子
- S3c2410裸板程序入门---单按键(一)
- node.js入门(二) 第一个程序 Hello World