回 帖 发 新 帖 刷新版面

主题:求大哥大姐们帮忙解一道遗传算法编程求解函数的最大值题目

用遗传算法编程求解Max f(x)=-x^2+2x+0.5 x属于【-1,2】
看了好多关于遗传算法的程序 可是一直都搞不清楚到底是怎么编的,因此还请大大们帮帮忙!

回复列表 (共6个回复)

沙发

呵呵 这道题我也看过 可惜的是我也一直看不懂 要不然的话倒可以和你说说!

板凳

http://hi.baidu.com/pengjun/blog/item/4f83eac4d3a1e5ac8326ace1.html

3 楼


谢谢彭大哥的回答!不过我好像运行不了这条语句啊
>> f =@(x)-x.^2-2*x-0.5;
>>[x,ff,flag,out]=ga(f,1,[],[],[],[],-1,2)
它提示的是:??? Error using ==> ga
Too many input arguments.
在你机器上可以运行的出结果来吗?还有这是用遗传算法解答的吗?

4 楼


[b]还有主函数你可能弄错了f(x)=-x^2+2*x+0.5[/b]

5 楼

%用于函数优化的遗传算法
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*');

    

6 楼

我的是2008a版本的,运行没有问题,还有就是遗传算法默认的是求最小值,所以如果要求最大值,要加负号

>> [x,ff,flag,out]=ga(f,1,[],[],[],[],-1,2)
Optimization terminated: average change in the fitness value less than options.TolFun.

x =

    2.0000


ff =

   -8.5000


flag =

     1


out = 

      problemtype: 'boundconstraints'
        randstate: [625x1 uint32]
       randnstate: [2x1 double]
      generations: 51
        funccount: 1040
          message: [1x86 char]
    maxconstraint: 0
上面是我电脑上的运行结果

我来回复

您尚未登录,请登录后再回复。点此登录或注册