在开发了随机整数的初始种群之后,我现在面临的问题是将每个种群的个体与适应度函数以及赋予适应度函数的论据联系起来。我有以下创建和健身功能,但是当我从工作区运行GA时,它会出现以下错误"初始用户提供的健身功能评估失败。 GA无法继续":
function pop = facilities_arrangement(NVARS,FitnessFcn,options)
%Creates a population of facility arrangement permutations.
totalPopulationSize = 50;
n = NVARS;
pop = cell(totalPopulationSize,1);
for i = 1:totalPopulationSize
pop{i} = randperm(n);
end
function scores = facility_layout_fitness(CR,distances)
% Custom fitness function for the facility layout problem.
% Calculates the fitness of an individual. The fitness is the total product
% between the closeness rating of the facilities and the distances
% travelled from one facility to another from their centroids
% by using the rectilinear distance.
pop = cell(totalPopulationSize,1);
scores = zeros(size(pop,1),1);
totalPopulationSize = 50;
for i = 1:totalPopulationSize
order = pop{i};
A = CR(order,order);
TCF = sum(A(:).*distances(:));
scores(i) = TCF;
end
答案 0 :(得分:0)
您可以在不提供初始人口的情况下调用ga函数。
但是,如果你想使用一个(我同意这是一个好主意),你必须确保你的初始人口满足问题约束,并且这是一个足够好的解决方案。你这样做的方式,只是生成一个随机的群体,确实给解算器带来了任何东西。
您可以致电:
x = ga(fitnessfcn,nvars)
如果它是设施位置问题,你应该使用某种贪婪算法来尝试生成一个很好的可行解决方案来为你的遗传算法提供信息。