这是使用遗传算法的近似解决方案。
我从您的测试案例开始:
data_points = 10;
num_people = 25;
to_keep = 13;
to_drop = num_people - to_keep;
num_comparisons = 100;
for j = 1:data_points
rand_dat(j,:) = 1 + 2.*randn(num_people,1);
end
A = corr(rand_dat);
接下来,我定义了需要演化遗传算法所需的函数:
function individuals = user1205901individuals(nvars, FitnessFcn, gaoptions, num_people)
individuals = zeros(num_people,gaoptions.PopulationSize);
for cnt=1:gaoptions.PopulationSize
individuals(:,cnt)=randperm(num_people);
end
individuals = individuals(1:nvars,:)';
是个人生成函数。
function fitness = user1205901fitness(ind, A)
fitness = sum(sum(A(ind,ind)));
健康评估功能
function offspring = user1205901mutations(parents, options, nvars, FitnessFcn, state, thisScore, thisPopulation, num_people)
offspring=zeros(length(parents),nvars);
for cnt=1:length(parents)
original = thisPopulation(parents(cnt),:);
extraneus = setdiff(1:num_people, original);
original(fix(rand()*nvars)+1) = extraneus(fix(rand()*(num_people-nvars))+1);
offspring(cnt,:)=original;
end
这个函数是用来改变一个个体的。
function children = user1205901crossover(parents, options, nvars, FitnessFcn, unused, thisPopulation)
children=zeros(length(parents)/2,nvars);
cnt = 1;
for cnt1=1:2:length(parents)
cnt2=cnt1+1;
male = thisPopulation(parents(cnt1),:);
female = thisPopulation(parents(cnt2),:);
child = union(male, female);
child = child(randperm(length(child)));
child = child(1:nvars);
children(cnt,:)=child;
cnt = cnt + 1;
end
“generate_offspring()”函数用于通过将两个父代进行交叉配对来生成新的个体。
在这一步,您可以定义您的问题:
gaproblem2.fitnessfcn=@(idx)user1205901fitness(idx,A)
gaproblem2.nvars = to_keep
gaproblem2.options = gaoptions()
gaproblem2.options.PopulationSize=40
gaproblem2.options.EliteCount=10
gaproblem2.options.CrossoverFraction=0.1
gaproblem2.options.StallGenLimit=inf
gaproblem2.options.CreationFcn= @(nvars,FitnessFcn,gaoptions)user1205901individuals(nvars,FitnessFcn,gaoptions,num_people)
gaproblem2.options.CrossoverFcn= @(parents,options,nvars,FitnessFcn,unused,thisPopulation)user1205901crossover(parents,options,nvars,FitnessFcn,unused,thisPopulation)
gaproblem2.options.MutationFcn=@(parents, options, nvars, FitnessFcn, state, thisScore, thisPopulation) user1205901mutations(parents, options, nvars, FitnessFcn, state, thisScore, thisPopulation, num_people)
gaproblem2.options.Vectorized='off'
打开遗传算法工具。
gatool
从 文件
菜单中选择 导入问题...
,然后在打开的窗口中选择 gaproblem2
。
现在,运行工具并等待迭代停止。
gatool
可以让你改变数百个参数,因此你可以在所选输出中权衡速度和精度。
结果向量是您必须在原始矩阵中保留的索引列表,这样 A(garesults.x,garesults.x)
就是只包含所需人员的矩阵。
sum(A, 2)
返回每行的总和。 - sco1x
,使得x'*S*x
最小化,其中S
是给定的对称正定矩阵,而x
受到以下约束:x
的元素只能为1
或0
,且x
的元素之和为n
。是这样吗? - Matthew Gunn