这里有几个基于matlab的遗传算法程序,很多地方看不明白,想请大家帮助!
%程序1
function xoverKids = crossoverpmx(parents,options,GenomeLength,FitnessFcn,unused,thisPopulation)
nKids = length(parents);
xoverKids = zeros(nKids,GenomeLength);
index = 1;
for i=1:fix(nKids/2)
parent1 = parents(index);
index = index + 1;
parent2 = parents(index);
index = index + 1;
sz = length(parent1) - 1;
xOverPoint1 = ceil(sz * rand);
xOverPoint2 = ceil(sz * rand);
while(xOverPoint2 == xOverPoint1)
xOverPoint2 = ceil(sz * rand);
end
if(xOverPoint1 < xOverPoint2)
left = xOverPoint1;
right = xOverPoint2;
else
left = xOverPoint2;
right = xOverPoint1;
end
for i=left:right
t=parent1;
parent1=parent2;
parent2=t;
end
for i=left:right
j=find(parent1==parent1(i));
if (size(j,2)==2)
parent1(j(find(j~=i)))=parent2(i);
end
end
for i=left:right
k=find(parent2==parent2(i))
if (size(k,2)==2)
parent2(k(find(k~=i)))=parent1(i);
end
end
xoverKids(i,:) = parent1;
xoverKids(nKids-i+1,:) = parent2;
end
请问,这个程序主要执行的是不是交叉操作,能否详细说明一下??不少地方看不大懂!谢谢
%程序2
function Population = myfun1(nvars, FitnessFcn, options)
totalpopulation = sum(options.PopulationSize);
for i=1:totalpopulation
Population(i,:)=randperm(40);
end
请问这个程序又主要在执行什么操作??谢谢了!
%程序3
function mutationChildren = myfun2(parents, options, nvars, FitnessFcn, state, thisScore, thisPopulation)
if(nargin < 8)
mutationRate = 0.01; % default mutation rate
end
mutationChildren = zeros(length(parents),nvars);
for i=1:length(parents)
child = thisPopulation(parents(i),:);
mutationPoint1=floor(rand*40+1);
mutationPoint2=floor(rand*40+1);
t=child(mutationPoint1);
child(mutationPoint1)=child(mutationPoint2);
child(mutationPoint2)=t;
mutationChildren(i,:) = child;
end
这个程序是不是在执行变异操作?怎么执行的?能否详细说说?谢谢了!