generate.cpp
来自「一个遗传算法(GA)的实现。command line 的结构。输入输出格式请见命」· C++ 代码 · 共 56 行
CPP
56 行
/*----------------------------------------------------------------------------*/
/* generate.c - create a new generation of individuals */
/*----------------------------------------------------------------------------*/
#include "generate.h"
//#include "sga.h"
#include "external.h"
#include "rselect.h"
#include "srselect.h"
#include "tselect.h"
#include "operators.h"
#include "app.h"
extern int popsize;
void generation()
{
int mate1, mate2, jcross, j = 0;
// printf("generation() ...... ");
/* perform any preselection actions necessary before generation */
t_preselect();
/* select, crossover, and mutation */
do
{
/* pick a pair of mates */
mate1 = t_select();
mate2 = t_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]),0); // !!!
newpop[j].parent[0] = mate1+1;
newpop[j].xsite = jcross;
newpop[j].parent[1] = mate2+1;
objfunc(&(newpop[j+1]),0);
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;
// printf(" %d",j);
}while(j < (popsize-1));
// printf(" OK!\n");
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?