主题:求大哥大姐们帮忙解一道遗传算法编程求解函数的最大值题目
吴三桂是我
[专家分:50] 发布于 2008-06-11 19:02:00
用遗传算法编程求解Max f(x)=-x^2+2x+0.5 x属于【-1,2】
看了好多关于遗传算法的程序 可是一直都搞不清楚到底是怎么编的,因此还请大大们帮帮忙!
5 楼
matrixlover [专家分:0] 发布于 2008-06-21 21:16:00
%用于函数优化的遗传算法
function [result,individual]=GA_OPT(popsize,stringlength,pc,pm,fun,a,b)
%popsize初始种群数量
%stringlength染色体长度
%fun需要优化的函数
%a,b是区间上下界
%pc交叉概率0.6
%pm变异概率0.001
pop=initial(popsize,stringlength,fun,a,b);
var_y=[];
for i=1:150
[bestindividual,bestvalue]=best(pop,stringlength);
var_y=[var_y,mean(pop(:,stringlength+2))];
newpop=selection(pop,popsize,stringlength);
newpop=crossover(newpop,stringlength,fun,a,b,pc);
newpop=mutation(newpop,stringlength,fun,a,b,pm);
pop=newpop;
end
[bestindividual,bestvalue]=best(pop,stringlength);
result=bestvalue;
individual=bestindividual;
subplot(1,2,1)
draw_ginger(pop,stringlength);
subplot(1,2,2)
plot(var_y); %收敛曲线
grid on
%--------种群初始化-------------%
function pop=initial(popsize,stringlength,fun,a,b)
pop=round(rand(popsize,stringlength+2));
for i=1:popsize
pop(i,stringlength+1)=sum(2.^(stringlength-1:-1:0).*pop(i,1:stringlength))*(b-a)/(2^stringlength-1)+a;
pop(i,stringlength+2)=fun(pop(i,stringlength+1));
end
%------------选择算子-------------%
function newpop=selection(pop,popsize,stringlength)
totalfit=sum(pop(:,stringlength+2));
prob=pop(:,stringlength+2)/totalfit;
prob=cumsum(prob);
newin=1;
while newin<=popsize %转轮赌方式生成新的个体
rns=rand;
i=find(prob>rns);
i=i(1);
newpop(newin,:)=pop(i,:);
newin=newin+1;
end
%----------------交叉算子-------------------%
function newpop=crossover(newpop,stringlength,fun,a,b,pc)
[px,py]=size(newpop);
for i=1:2:px-1
if rand<=pc
cpoint=unidrnd(stringlength-1);
newpop(i,1:stringlength)=[newpop(i,1:cpoint),newpop(i+1,(cpoint+1):stringlength)];
newpop(i+1,1:stringlength)=[newpop(i+1,1:cpoint),newpop(i,(cpoint+1):stringlength)];
newpop(i,stringlength+1)=sum(2.^(stringlength-1:-1:0).*newpop(i,1:stringlength))*(b-a)/(2^stringlength-1)+a;
newpop(i+1,stringlength+1)=sum(2.^(stringlength-1:-1:0).*newpop(i+1,1:stringlength))*(b-a)/(2^stringlength-1)+a;
newpop(i,stringlength+2)=fun(newpop(i,stringlength+1));
newpop(i+1,stringlength+1)=fun(newpop(i+1,stringlength+1));
else
newpop(i,:)=newpop(i,:);
newpop(i+1,:)=newpop(i+1,:);
end
end
%-----------------变异算子-------------------%
function newpop=mutation(newpop,stringlength,fun,a,b,pm)
[px,py]=size(newpop);
for i=1:px
if(rand<=pm)
mpoint=unidrnd(stringlength);
newpop(i,mpoint)=abs(newpop(i,mpoint)-1);
newpop(i,stringlength+1)=sum(2.^(stringlength-1:-1:0).*newpop(i,1:stringlength))*(b-a)/(2^stringlength-1)+a;
newpop(i,stringlength+2)=fun(newpop(i,stringlength+1));
else
newpop(i,:)=newpop(i,:);
end
end
%----------------求最大适应值-----------------%
function [bestindividual,bestfit]=best(pop,stringlength)
[px,py]=size(pop);
bestindividual=pop(1,stringlength+1);
bestfit=pop(1,stringlength+2);
for i=2:px
if bestfit<pop(i,stringlength+2)
bestfit=pop(i,stringlength+2);
bestindividual=pop(i,stringlength+1);
end
end
%-------------------画图函数-----------------%
function draw_ginger(pop,stringlength)
figure(1)
fplot('x^2+2*x+1',[-2,2]);
hold on
plot(pop(:,stringlength+1),pop(:,stringlength+2),'r*');