📄 a14.m
字号:
NIND = 40; % Number of individuals
MAXGEN =1000; % Maximum no. of generations
NVAR = 20; % No. of variables
PRECI = 20; % Precision of variables
GGAP =0.9; % Generation gap
% Build field descriptor
FieldD = [rep([PRECI],[1,NVAR]);rep([-5.12;5.12],[1,NVAR]); rep([1;0;1;1],[1,NVAR])];
% Initialise population
Chrom = crtbp(NIND, NVAR*PRECI);
gen = 0; trace=zeros(MAXGEN,2);% Counter
% Evaluate initial population
x=bs2rv(Chrom,FieldD);
ObjV =objfun6(x);
% Generational loop
while gen < MAXGEN,
% Assign fitness values to entire population
FitnV = ranking(ObjV);
% Select individuals for breeding
SelCh = select('sus', Chrom, FitnV, GGAP);
% Recombine individuals (crossover)
SelCh = recombin('xovsp',SelCh,0.7);
% Apply mutation
SelCh = mut(SelCh);
% Evaluate offspring, call objective function
x=bs2rv(SelCh,FieldD);
ObjVSel =objfun6(x) ;% Reinsert offspring into population
[Chrom ObjV]=reins(Chrom,SelCh,1,1,ObjV,ObjVSel);
% Increment counter
gen = gen+1;trace(gen,1)=min(ObjV);trace(gen,2)=sum(ObjV)/length(ObjV);
end
figure(2);clf;plot(trace(:,1));hold on; plot(trace(:,2),'-.');grid;
legend('解的变化','种群均值的变化')
[Y,I]=min(ObjVSel);bs2rv(Chrom(I,:),FieldD),Y %
%figure(3);clf;plot();hold on;grid;plot(bs2rv(Chrom(I,:),FieldD),'bo');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -