generate.c

来自「遗传算法的简单程序」· C语言 代码 · 共 37 行

C
37
字号
//繁衍 
/*----------------------------------------------------------------------------*/ 
/*             generate.c - create a new generation of individuals            */ 
/*----------------------------------------------------------------------------*/ 
#include "external.h"
 
generation() 
{ 
  int mate1, mate2, jcross, j = 0; 
  /* perform any preselection actions necessary before generation */ 
  preselect(); 
  /* select, crossover, and mutation */ 
  do 
    { 
      /* pick a pair of mates */ 
      mate1 = select(); 
      mate2 = select(); 
      /* Crossover and mutation */ 
      jcross = crossover(oldpop[mate1].chrom, oldpop[mate2].chrom, 
                         newpop[j].chrom, newpop[j+1].chrom); 
      mutation(newpop[j].chrom); 
      mutation(newpop[j+1].chrom); 
      /* Decode string, evaluate fitness, & record */ 
      /* parentage date on both children */ 
      objfunc(&(newpop[j])); 
      newpop[j].parent[0] = mate1+1; 
      newpop[j].xsite = jcross; 
      newpop[j].parent[1] = mate2+1; 
      objfunc(&(newpop[j+1])); 
      newpop[j+1].parent[0] = mate1+1; 
      newpop[j+1].xsite = jcross; 
      newpop[j+1].parent[1] = mate2+1; 
      /* Increment population index */ 
      j = j + 2; 
    } 
  while(j < (popsize-1)); 
} 

⌨️ 快捷键说明

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