Logo Search packages:      
Sourcecode: tablix2 version File versions  Download package

void new_generation ( population pop  ) 

Makes a new generation in the population.

Parameters:
pop Pointer to the population struct.

Definition at line 160 of file genetic.c.

References debug(), table_t::fitness, population_t::gencnt, par_maxequal, par_migrpart, par_migrtime, par_mutatepart, par_randpart, par_toursize, population_t::size, table_compare(), table_fitness(), table_mate(), table_mutate(), table_rand(), and population_t::tables.

{
      table **tables;
      int size;

      int curfitness;
      int numequal;

      int n,m;

      int t1,t2,t3;

      #ifdef HAVE_LIBPVM3
      int migrsize;
      #endif

      tables=pop->tables;
      size=pop->size;

      /* Send migration */

      #ifdef HAVE_LIBPVM3
      if (pop->gencnt%par_migrtime==0) {
            debug(_("sending migration to %x"), sibling);

            migrsize=size/par_migrpart;

            pvm_initsend(0);
            pvm_send(sibling, MSG_MIGRATION);

            table_send(pop, migrsize, sibling, MSG_MIGRATION);
      }
      #endif

      /* Check for sequences of equally fitnessd chromosomes. If a sequence 
       * of more than MAXEQUAL chromosomes is found, assign a very high
       * fitness to them */

        curfitness=tables[0]->fitness;
        numequal=1;
        for(n=1;n<size;n++) {
                if (tables[n]->fitness==curfitness) {
                        numequal++;
                } else {
                        curfitness=tables[n]->fitness;
                        numequal=1;
                }

                if (numequal>par_maxequal) {
                        tables[n]->fitness=INT_MAX;
                }
        }

      /* Crossover */

        for(n=size/2;n<size-1;n+=2) {
                t1=rand()%(size/2);
                t2=rand()%(size/2);

                if (tables[t1]->fitness>tables[t2]->fitness) {
                        t3=t1;
                        t1=t2;
                        t2=t3;
                }

                for(m=2;m<par_toursize;m++) {
                        t3=rand()%(size/2);

                        if (tables[t3]->fitness<tables[t1]->fitness) t1=t3; else
                        if (tables[t3]->fitness<tables[t2]->fitness) t2=t3;
                }

                table_mate(tables[t1], tables[t2], tables[n], tables[n+1]);
        }

      /* Two different kinds of mutations */

        for(n=0;n<(size/2/par_mutatepart);n++) {
                t1=rand()%(size/2);
                table_mutate(tables[t1]);
        }
 
        for(n=0;n<(size/2/par_randpart);n++) {
                t1=rand()%(size/2);
                table_rand(tables[t1]);
        }

      /* Receive migration */

      #ifdef HAVE_LIBPVM3
        if ((n=pvm_nrecv(-1, MSG_MIGRATION))>0) {
            int msgtag, sender;

                pvm_bufinfo(n, NULL, &msgtag, &sender);
            debug("receiving migration from %x", sender);

            migrsize=size/par_migrpart;

            table_recv(pop, migrsize, -1, MSG_MIGRATION);
        }
      #endif

      /* Assign fitness to each chromosome */

        for(n=0;n<size;n++) {
                  if(tables[n]->fitness==-1) {
                  table_fitness(tables[n]);
            }
      }

      /* Sort chromosomes by fitness */

      qsort(pop->tables, size, sizeof(*pop->tables), table_compare);

      pop->gencnt++;
}


Generated by  Doxygen 1.6.0   Back to index