tourselect.c 文件中共有两个函数:
selection (population *old_pop, population *new_pop)
individual* tournament (individual *ind1, individual *ind2)
首先,第一个函数代码如下:
1 /*Routine for tournament selection, it creates a new_pop from old_pop by performing tournament selection and the crossover*/
2 void selection (population *old_pop, population *new_pop)3 {4 int *a1, *a2;5 inttemp;6 inti;7 intrand;8 individual *parent1, *parent2;9 a1 = (int *)malloc(popsize*sizeof(int));10 a2 = (int *)malloc(popsize*sizeof(int));11 for (i=0; iind[a1[i]], &old_pop->ind[a1[i+1]]);29 parent2 = tournament (&old_pop->ind[a1[i+2]], &old_pop->ind[a1[i+3]]);30 crossover (parent1, parent2, &new_pop->ind[i], &new_pop->ind[i+1]);31 parent1 = tournament (&old_pop->ind[a2[i]], &old_pop->ind[a2[i+1]]);32 parent2 = tournament (&old_pop->