?? gademo1eeval.m
字號:
%舉一實例 利用遺傳算法計算下列函數的最大值
% f(x)=x+10*sin(5x)+7*cos(4x) x屬于[0,9]
%編寫目標函數
function [sol, eval] =gaDemo1Eeval(sol,options)
x=sol(1);
eval = x + 10*sin(5*x)+7*cos(4*x);
%參數說明
%eval:個體的適應度;
%sol:當前個體,n+1個元素的行向量。
%遺傳算法求最大值
clc
fplot('x + 10*sin(5*x)+7*cos(4*x)',[0 9])
% 生成初始種群,大小為10
initPop=initializega(10,[0 9],'gademo1eeval1');
plot (initPop(:,1),initPop(:,2),'b*')
% 調用遺傳函數
% 1次遺傳迭代
[x endPop] = ga([0 9],'gademo1eeval1',[],initPop,[1e-5...
1 1],'maxGenTerm',1,'normGeomSelect',[0.08],['arithXover'], ... [20], 'nonUnifMutation',[2 1 3]);
plot (endPop(:,1),endPop(:,2),'bo')
% 25次遺傳迭代
[x endPop bpop trace] = ga([0 9],'gademo1eeval1',[],initPop, [1e-6 1 1],'maxGenTerm',25, 'normGeomSelect',[0.08], ['arithXover'],[2],'nonUnifMutation',[2 25 3]);
plot (endPop(:,1),endPop(:,2),'y*')
figure(2)
plot(trace(:,1),trace(:,3),'y-')
hold on
plot(trace(:,1),trace(:,2),'r-')
xlabel('Generation'); ylabel('Fittness');
legend('解的變化','種群平均值的變化');
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -