⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 morph.m

📁 一些用matlab编写的经典遗传算法算例。可以用于解决许多优化问题
💻 M
字号:
function n = morph(chr1,chr2,pmut)
% MORPH - Metamorphose transforms one design (chromosome) into another.  
% 
%   c=morph(chromosome_1,chromosome_2,pmut) transforms
%   chromosome_1 into type 2 keeping only the genes that are common
%   between the chromosomes and adds or discards any extra. The
%   argument pmut is the probability of mutation.
%
% See also MUTATE,PROBMUT,UI_MUTATE

%----------Check the arguments
if and(~isa(chr1,'chrom'),~isa(chr2,'chrom'))
  error('arguments need to be of valid type')
end

%----------no need for morphing
if strcmp(get(chr1,'name'),get(chr2,'name'))
  n=chr1;
  return;
end

for k=1:length(pmut)
  if strcmp(pmut(k).name,get(chr1,'name'))
    if isempty(pmut(k).trans)
      pm_trans=[];
    else
      pm_trans=pmut(k).trans;
    end
    break
  end
end

if rand<=pm_trans

  %----------Extract data
  c1_cdvs=get(chr1,'cdvs');
  c1_ddvs=get(chr1,'ddvs');
  
  c2_cdvs=get(chr2,'cdvs');
  c2_ddvs=get(chr2,'ddvs');
  
  %----------CDVS
  for k=1:length(c2_cdvs)			% chr2 decides 
    k_ind=find(c1_cdvs,get(c2_cdvs(k),'name')); % common dna
    if k_ind
      c(k)=c1_cdvs(k_ind);		% yes keep it
    else
      c(k)=c2_cdvs(k);			% no keep chrom_2
    end
  end
  
  %----------DDVS
  for k=1:length(c2_ddvs)
    k_ind=find(c1_ddvs,get(c2_ddvs(k),'name'));
    if k_ind
      d(k)=c1_ddvs(k_ind);
    else
      d(k)=c2_ddvs(k);
    end
  end
  

  %----------Rebuild and return
  n=chrom(get(chr2,'name'),c,d);
else
  n=chr1;
end

⌨️ 快捷键说明

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