matlab----GA遺傳算法

1.GA的調用:






2.例:當N = 10時



函數爲:

function f = lbw(x)
if x(1)>30 || x(1) <-30 || x(2)>30 || x(2) <-30 || x(3)>30 || x(3) <-30 || x(4)>30 || x(4) <-30 || x(5)>30 || x(5) <-30 || x(6)>30 || x(6) <-30 || x(7)>30 || x(7) <-30 || x(8)>30 || x(8) <-30 || x(9)>30 || x(9) <-30 || x(10)>30 || x(10) <-30
    f = 300;
else
    s1 = 0;
    s2 = 0;
    for i = 1 : 10
        s1 = s1 + x(i)^2;
        s2 = s2 + cos(2 * pi * x(i));
    end
    f = -2 * pi *exp(-0.2 * sqrt(1/10 * s1)) - exp(1/10 * s2) + 2 * pi;
end

.m爲:

%調用GA遺傳算法
%Generations : 超過800代時停止; StallGenLimit : 超過連續代數300不進化時停止;  PlotFcns :繪圖函數
options = gaoptimset('Generations',800,'StallGenLimit',300,'PlotFcns',@gaplotbestf);

%ga(待求函數,未知變量,選擇): 求最小值
[x,f] = ga(@lbw,10,options)


最終結果:


3.GA在求解多約束非線性規劃問題

注:Lingo也爲首選方法; Matlab優化工具箱求解精度不夠,窮舉法效率太低

3.1:例



函數爲:

%子函數:適應度函數同時也是目標函數,
function f=ch14_2f(x)
g1=1.5+x(1)*x(2)-x(1)-x(2);
g2=-x(1)*x(2);
if(g1>0|g2>10)
    f=100;
else
    f=exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+1);
end


.m爲:

%主程序:本程序採用遺傳算法接力進化,
%將上次進化結束後得到的最終種羣作爲下次輸入的初始種羣
clc;
clear all;
%進化的代數
T=100;
optionsOrigin=gaoptimset('Generations',T/2);
[x,fval,reason,output,finnal_pop]=ga(@ch14_2f,2,optionsOrigin);
%進行第二次接力進化
options1=gaoptimset('Generations',T/2,'InitialPopulation',finnal_pop,...
    'PlotFcns',@gaplotbestf);
[x,fval,reason,output,finnal_pop]=ga(@ch14_2f,2,options1);
Bestx=x
BestFval=fval





發佈了58 篇原創文章 · 獲贊 3 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章