NIND = 40; % Number of individuals
MAXGEN =500; % 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([-500;500],[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 =objfun7(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 =objfun7(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');