Multi-Objective genetic algorithm------NSGA-II (partial source parsing) Two-yuan tournament selection tourselect.c

Source: Internet
Author: User

There are two functions in a tourselect.c file:

Selection (population *old_pop, population *new_pop)

Individual* Tournament (individual *ind1, individual *ind2)

First, the code of the first function is as follows:

1 /*Routine for tournament selection, it creates a new_pop from Old_pop by performing tournament selection and the Crosso Ver*/2 voidSelection (population *old_pop, population *New_pop)3 {4     int*A1, *A2;5     inttemp;6     inti;7     intRand;8Individual *parent1, *Parent2;9A1 = (int*)malloc(popsize*sizeof(int));TenA2 = (int*)malloc(popsize*sizeof(int)); One      for(i=0; i<popsize; i++) A     { -A1[i] = A2[i] =i; -     } the      for(i=0; i<popsize; i++) -     { -Rand = RND (i, popsize-1); -temp =A1[rand]; +A1[rand] =A1[i]; -A1[i] =temp; +Rand = RND (i, popsize-1); Atemp =A2[rand]; atA2[rand] =A2[i]; -A2[i] =temp; -     } -      for(i=0; i<popsize; i+=4) -     { -Parent1 = Tournament (&old_pop->ind[a1[i]), &old_pop->ind[a1[i+1]]); inParent2 = Tournament (&old_pop->ind[a1[i+2]], &old_pop->ind[a1[i+3]]); -Crossover (Parent1, Parent2, &new_pop->ind[i], &new_pop->ind[i+1]); toParent1 = Tournament (&old_pop->ind[a2[i]), &old_pop->ind[a2[i+1]]); +Parent2 = Tournament (&old_pop->ind[a2[i+2]], &old_pop->ind[a2[i+3]]); -Crossover (Parent1, Parent2, &new_pop->ind[i+2], &new_pop->ind[i+3]); the     } *      Free(A1); $      Free(A2);Panax Notoginseng     return; -}

which

    A1 = (int *)malloc(popsize*sizeof(int));     = (int *)malloc(popsize*sizeof(int));

An array of two population sizes was generated, A1 A2, in which the sequence numbers of the individual populations were saved.

     for (i=0; i<popsize; i++)    {        = a2[i] = i;    }

The two arrays are initialized, and the population numbers are stored sequentially.

     for (i=0; i<popsize; i++)    {        = rnd (i, popsize-1);         = A1[rand];         = A1[i];         = temp;         = Rnd (i, popsize-1);         = A2[rand];         = A2[i];         = temp;    }

For A1, the A2 array of individuals stored in the number of scrambled, which the number of disruption is popsize, the operation is basically to ensure that the number of all individuals is not in its original position.

(In advanced object-oriented languages, the above code can be replaced with one-line library function call)

     for(i=0; i<popsize; i+=4) {Parent1= Tournament (&old_pop->ind[a1[i]], &old_pop->ind[a1[i+1]]); Parent2= Tournament (&old_pop->ind[a1[i+2]], &old_pop->ind[a1[i+3]]); Crossover (Parent1, Parent2,&new_pop->ind[i], &new_pop->ind[i+1]); Parent1= Tournament (&old_pop->ind[a2[i]], &old_pop->ind[a2[i+1]]); Parent2= Tournament (&old_pop->ind[a2[i+2]], &old_pop->ind[a2[i+3]]); Crossover (Parent1, Parent2,&new_pop->ind[i+2], &new_pop->ind[i+3]); }

This part of the code completes the selection and crossover operations in the genetic algorithm.

among them, Old_pop new_pop are all species of the same population, and their population size is popsize.

Tournament Tournament method, which is used in the two-yuan tournament selection method, a total of 4 cycles in the body tournament operation, the cycle of POPSIZE/4 times, so a total of popsize times two tournament selection. Because each time a new individual is selected, the new population of this method chooses the same new_pop individual number and the Old_pop individual number of the old population.

Similarly, the crossover operation has been POPSIZE/2 times, (where each cross-operation is selected two individuals, each time to determine whether the selection of two individuals to cross each other according to the given cross-probability to judge), the Loop body crossover function of the total popsi Ze individual for processing.

Note: The crossover operation Loops calls POPSIZE/2 times instead of popsize times.

1 /*Routine for Binary tournament*/2Individual* Tournament (Individual *ind1, individual *ind2)3 {4     intFlag;5Flag =check_dominance (Ind1, ind2);6     if(flag==1)7     {8         return(IND1);9     }Ten     if(flag==-1) One     { A         return(IND2); -     } -     if(Ind1->crowd_dist > ind2->crowd_dist) the     { -         return(IND1); -     } -     if(Ind2->crowd_dist > ind1->crowd_dist) +     { -         return(IND2); +     } A     if((Randomperc ()) <=0.5) at     { -         return(IND1); -     } -     Else -     { -         return(IND2); in     } -}

The binary tournament competition method is relatively simple, which calls the Check_dominance function to determine the dominant relationship of two individuals, if they do not dictate the two individual crowding distance, if all the same this random selection of an individual.

Multi-Objective genetic algorithm------NSGA-II (partial source parsing) Two-yuan tournament selection tourselect.c

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.