新建 文本文档.txt

来自「几个基于matlab的遗传算法程序,有用就下」· 文本 代码 · 共 70 行

TXT
70
字号
%程序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

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?