24小时热门版块排行榜    

查看: 581  |  回复: 1

wuzhenghai

至尊木虫 (职业作家)

[求助] 求解决一个遗传算法的问题

下面这个MATLAB代码是求的方程的极大值还是极小值?怎样才能输出结果?
%  Genetic algorithm for solving simple optimization problem
% f(x1,x2)=(109.22569476)+(-2.361371926)*x1+(0.049210151624)*x1^2+(-0.00044295978143)*x1^3+(1.45*exp(-06))*x1^4+(-187.80050192)/x2+(8802.6232751)/x2^2+(-79536.41822)/x2^3+(-2264.2319708)/x2^4, 52.5≤x1≤112.5,
% 45≤x2≤120
%The stategy of optimal maintaince is applied in the algorithm

clear all;
clc;
format long;
Population_Size=100;  %群体大小

String_Length=30;    %染色体长度
chromosome=round(rand(Population_Size,String_Length));
chromosome_change=zeros(Population_Size,String_Length);
flag=1;
fitness_function=zeros(1,Population_Size);
selected=zeros(1,Population_Size);
generation=1;     %代数
maxsat=100;       %最大代数

while(flag>0)&(generation<500)
    sum_fit=0;
    for i=1opulation_Size
        a1=chromosome(i,1:15);a2=chromosome(i,16:30);
        param=zeros(1,2);
        for j=1:15
            param(1,1)=param(1,1)+a1(1,j)*pow2(j-1);
        end
        for j=1:15
            param(1,2)=param(1,2)+a2(1,j)*pow2(j-1);
        end
        m=pow2(15)-1;
        x1=52.5+param(1,1)/m*(112.5-52.5);
        x2=45+param(1,2)/m*(120-45);
fitness_function(1,i)=(109.22569476)+(-2.361371926)*100.730+(0.049210151624)*100.730^2+(-0.00044295978143)*100.730^3+(1.45*exp(-06))*100.730^4+(-187.80050192)/60.807+(8802.6232751)/60.807^2+(-79536.41822)/60.807^3+(-2264.2319708)/60.807^4;     
        if(fitness_function(1,i)<maxsat)
maxsat=fitness_function(1,i);
            optimal=[x1 x2 fitness_function(1,i)];
        end
        if(fitness_function(1,i)<=0.001)
            flag=-1;
            optimal
            generation
            break;
        else
            sum_fit=sum_fit+fitness_function(1,i);
        end
        if (flag<0)
            break;
        end      
    end
   
      
    if(flag>0)
        %the first select
        sum_fit=sum_fit-fitness_function(1,Population_Size);
        for i=1opulation_Size-1
            x=round(rand(1)*32767);
            sum=round(sum_fit);
            rr=rem(x,sum);
            n=1;ba=1;
            partsum=0;            
            while((partsum<rr)&(n<Population_Size-1))
                partsum=partsum+fitness_function(1,n);
                ba=n;
                n=n+1;
            end           
            selected(1,i)=ba;
        end
        %reproduce     
        for i=1opulation_Size-1
            for j=1:String_Length
                chromosome_change(i,j)=chromosome(selected(1,i),j);
end
            fitness_function(1,i)=fitness_function(1,selected(1,i));
        end  
        
        %select before crossover      
        for i=1opulation_Size-1
            x=round(rand(1)*32767);
            sum=round(sum_fit);
            rr=rem(x,sum)+1;
            n=1;
            partsum=0;
            while((partsum<rr)&(n<=Population_Size-1))
                partsum=partsum+fitness_function(1,n);
                bba=n;
                n=n+1;
            end
            selected(1,i)=bba;   
        end
        %crossover
        maxsw=max(fitness_function);
        for i=1opulation_Size/2-1
            parent1=selected(1,i);
            parent2=selected(1,Population_Size-1-i);
            child1=i;
            child2=Population_Size-1-i;
            pc=0.8;
            randnum=rand(1);
            site1=round(rand(1)*String_Length);
            for j=1:String_Length
                if(j<site1)
                    chromosome(child1,j)=chromosome_change(parent1,j);
                    chromosome(child2,j)=chromosome_change(parent2,j);
                else
                    chromosome(child1,j)=chromosome_change(parent2,j);
                    chromosome(child2,j)=chromosome_change(parent1,j);
                end
            end
        end
        %mutation
        pm=0.05;
        for i=1opulation_Size-1      
            for j=1:String_Length
                randnum=rand(1);
                if(randnum<pm)
                    chromosome(i,j)=~chromosome(i,j);
                end
            end
        end
    end
    generation=generation+1
end
回复此楼

» 猜你喜欢

» 本主题相关价值贴推荐,对您同样有帮助:

收获不在这里。
已阅   关注TA 给TA发消息 送TA红花 TA的回帖

feixiaolin

荣誉版主 (文坛精英)

优秀版主

极大值
Parameters  x1[52.5, 112.5], x2[45, 120];
MaxFunction  (109.22569476)+(-2.361371926)*x1+(0.049210151624)*x1^2+(-0.00044295978143)*x1^3+(1.45*exp(-06))*x1^4+(-187.80050192)/x2+(8802.6232751)/x2^2
+(-79536.41822)/x2^3+(-2264.2319708)/x2^4;

迭代数: 17
计算用时(时:分:秒:毫秒): 00:00:00:78
计算中止原因: 达到收敛判定标准
优化算法: 麦夸特法(Levenberg-Marquardt) + 通用全局优化法
函数表达式: (109.22569476)+(-2.361371926)*x1+(0.049210151624)*x1^2+(-0.00044295978143)*x1^3+(1.45*exp(-06))*x1^4
          +(-187.80050192)/x2+(8802.6232751)/x2^2+(-79536.41822)/x2^3+(-2264.2319708)/x2^4
目标函数值(最大): 575554.835061638
x1: 112.5
x2: 45.0000000011074

====== 计算结束 ======

极小值
Parameters  x1[52.5, 112.5], x2[45, 120];
MinFunction  (109.22569476)+(-2.361371926)*x1+(0.049210151624)*x1^2+(-0.00044295978143)*x1^3+(1.45*exp(-06))*x1^4+(-187.80050192)/x2+(8802.6232751)/x2^2
+(-79536.41822)/x2^3+(-2264.2319708)/x2^4;

迭代数: 15
计算用时(时:分:秒:毫秒): 00:00:00:63
计算中止原因: 达到收敛判定标准
优化算法: 最大继承法
函数表达式: (109.22569476)+(-2.361371926)*x1+(0.049210151624)*x1^2+(-0.00044295978143)*x1^3+(1.45*exp(-06))*x1^4
          +(-187.80050192)/x2+(8802.6232751)/x2^2+(-79536.41822)/x2^3+(-2264.2319708)/x2^4
目标函数值(最小): 27360.4204312979
x1: 52.5
x2: 77.2996719370498

====== 计算结束 ======
2楼2015-04-22 21:53:39
已阅   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 wuzhenghai 的主题更新
信息提示
请填处理意见