我创建了执行遗传算法的函数,以解决背包问题。在上一代中,我可以通过输出x
轻松获得最佳染色体,但是如何从第一代中获得最佳染色体?看here时,我注意到向量Best
应该保留每一代中最好的元素。如何访问该载体?
function [x,fval,exitflag,output,population,score] = evolutionaryGaSolution(ds, Wmax)
nvars = 32;
PopulationSize_Data = 50;
MaxGenerations_Data = 80;
FitnessLimit_Data = -207;
f = @(x)fitnessBackpack(x, ds, Wmax);
% Load default settings
options = optimoptions('ga');
% Modify options setting
options = optimoptions(options,'PopulationType', 'bitstring');
options = optimoptions(options,'PopulationSize', PopulationSize_Data);
options = optimoptions(options,'MaxGenerations', MaxGenerations_Data);
options = optimoptions(options,'FitnessLimit', FitnessLimit_Data);
options = optimoptions(options,'MutationFcn', { @mutationuniform 0.1 });
options = optimoptions(options,'Display', 'off');
options = optimoptions(options,'PlotFcn', { @gaplotlogbestf });
[x,fval,exitflag,output,population,score] = ...
ga(f,nvars,[],[],[],[],[],[],[],[],options);
fprintf("Starting point: %s\n", ????); % What should I write here?
fprintf("Final point: %s\n", num2str(x));
fprintf("Generations:%d\n", output.generations);
end