a8.m

来自「遗传算法工具箱」· M 代码 · 共 29 行

M
29
字号
% Define GA Parameters
Dim=20; % No. of variables
NIND =20;    % No. of individuals 
Preci=20;    % No. of individuals 
MAXGEN = 50;% Maximum no. of generations
GGAP = 0.8; % Generation gap
SEL_F = 'sus'; % 选择函数名
XOV_F ='xovsp';% 重组函数名
MUT_F ='mut'; % 变异函数名
OBJ_F ='objdopi'; % 目标函数名
FieldDR=feval(OBJ_F,[],1); 
FieldDD=[rep([Preci],[1,Dim]);FieldDR;rep([1;0;1;1],[1,Dim])];
Chrom=crtbp(NIND,Dim*Preci); % Fitness assignment to whole population
gen = 0;% counter
Best=NaN*ones(MAXGEN,1);% Generational loop
while gen < MAXGEN, % Generational loop
ObjV=feval(OBJ_F,bs2rv(Chrom,FieldDD));
Best(gen+1)=min(ObjV);
plot(log10(Best),'bo');
FitnV = ranking(ObjV);
SelCh = select(SEL_F,Chrom,FitnV, GGAP);% Select individuals from population
SelCh=recombin(XOV_F, SelCh);% Recombine selected individuals
SelCh = mutate(MUT_F,SelCh); % Mutate offspring
% Insert best offspring replacing worst parents
Chrom = reins(Chrom, SelCh);
% Increment counter
gen=gen+1;
end
grid;xlabel('迭代次数');ylabel('目标函数值(取对数)')

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?