Genetic algorithm --- stochastic algorithm
The genetic algorithm (genetic algorithms,GA) was first made in 1975 Year by the American University of Michigan D . J. Professor Holland and his colleagues drew upon the principles of natural selection and Mendel's genetic evolutionary mechanisms in the biological world. After nearly Five years of research and application, genetic algorithm has been widely used in function optimization, robot system, neural network learning process, pattern recognition, image processing, industrial optimization control and other fields.
The
Genetic algorithm is to treat every possible solution of the problem as an individual in a group
In the process of running genetic algorithm, some parameters should be selected beforehand, including population size, chromosome length, crossover rate, mutation rate, maximum evolutionary algebra, and so on, these parameters have important influence on GA performance. In the experiment, the parameters are generally selected as follows: Population size n= 20~100 , crossover probability = 0.4 ~0.9 , mutation probability = 0.001~0.1 , maximum evolutionary algebra Maxgen = 100~500.
Genetic algorithm is a search algorithm with the iterative process of " generation + detection " . It is shown in basic process flow 1 .
Figure 1, basic flow of genetic algorithms
The basic flow of genetic algorithms is described as follows:
(1) Encoding: The solution data of the solution space is encoded in binary, expressed as the genetic space of the genotype string (that is, chromosome) structure data, such as the data 9 encoded as "1001";
(2) Initialize the population: Define integer pop_size as the number of chromosomes, and randomly produce pop_size chromosomes as initial populations ;
(3) Assessment of individual fitness in the population: the evaluation function of each chromosome (chromosome) in the population toobtain their individual fitness ;
(4) Select: Select the current group of individuals with a higher degree of adaptability according to a certain rule or model to the next generation of the population, the rule is: the chromosome in the population is selected in proportion to the size of the individual's fitness;
(5) Crossover: Define the probability of the parameter as the cross operation, and thetwo individuals selected by (4) are given the probability to exchange the respective chromosome, and the new two individuals are obtained.
(6) Variation: The probability of defining a parameter as a mutation operation is obtained by (5) Each individual gene value is varied by probability;
(7) Evolution: After selection, crossover and mutation operation, a new population is obtained, and the genetic algorithm terminates after a given cycle number (maxgen) population evolution of the above steps.
/*the maximum value of Y=x*sin (10*pi*x) +2 is obtained by genetic algorithm -1=<x<=2 accurate to 6 decimal places POW (2,21) <3*1000000<pow (2,22) encoded binary length is*/#include<stdio.h>#include<string.h>#include<stdlib.h>#include<time.h>#include<math.h>#defineN 3000000#definePI 3.14159265#defineMAX (a) (a) > (b)? ( A):(B))#defineSIZE 50#defineMaxgen 50#defineP_corss 0.75#defineP_mutation 0.05#defineLEN 22typedefstructnode{CharX[len]; DoubleFitness,fitsum;} Node;node cur[size],next[size],max,min;Doublerandd () {return(Double) rand ()/Rand_max;}intRandiintk) { return(int) (RandD () *k+0.5);}//calculate the fitness of each individual in the current populationvoidcal_fitness () {inti,j,k; DoubleD; for(i=0; i<size; i++) {k=0; for(j=len-1; j>=0; j--) k= (k<<1)+Cur[i].x[j]; D=(Double) k/n*3-1; Cur[i].fitness=d*sin (Ten*PI*D) +2; Cur[i].fitsum=i>0? (cur[i].fitness+cur[i-1].fitsum):(cur[0].fitness); }}voidinit () {inttmp; for(intI=0; i<size; i++) {tmp=Randi (N); for(intj=0; j<len; J + +) {Cur[i].x[j]=tmp%2; TMP=tmp>>1; }} cal_fitness ();}intsel () {Doublep=randd (); Doublesum=cur[size-1].fitsum; for(intI=0; i<size; i++) { if(cur[i].fitsum/sum>p)returni; }}//ReplacementvoidTran () {intI,j,pos; //finding the optimal individual of the current populationmax=cur[0]; for(i=1; i<size-1; i++) { if(cur[i].fitness>max.fitness) max=Cur[i]; } for(intk=0; k<size; k+=2) { //Select a cross-entityI=sel (); J=sel (); //Select intersection positionPos=randi (len-1); //Cross if(RandD () <P_corss) {memcpy (Next[k].x,cur[i].x,pos); memcpy (next[k].x+pos,cur[j].x+pos,len-POS); memcpy (Next[k+1].x,cur[j].x,pos); memcpy (Next[k+1].x+pos,cur[i].x+pos,len-POS); } Else{memcpy (Next[k].x,cur[i].x,len); memcpy (Next[k+1].x,cur[j].x,len); } //mutation if(RandD () <p_mutation) {POS=randi (len-1); Next[k].x[pos]^=Next[k].x[pos]; POS=randi (len-1); Next[k+1].x[pos]^=next[k+1].x[pos]; } } //finding the worst individuals of the next generationmin=next[0],j=0; for(i=1; i<size-1; i++) { if(next[i].fitness<min.fitness) min=next[i],j=i; } //replace the worst individuals of the next generation with the best individuals of the previous generationnext[j]=Max; memcpy (Cur,next,sizeof(cur)); Cal_fitness ();}//Print individual fitness and binary codingvoidPrint (node tmp) {printf ("%.6LF", tmp.fitness); for(intI=0; i<len; i++) printf ("%d", Tmp.x[i]); printf ("\ n");}//Print populationvoidprintcur () { for(intI=0; i<size; i++) print (Cur[i]);}voidGA () {intCnt=0; Doubleans; while(cnt++<Maxgen) {tran ();//printf ("%.6lf\n", max.fitness);//printcur ();} ans=cur[0].fitness; for(intI=1; i<size; i++) ans=MAX (ans,cur[i].fitness); printf ("%.6lf\n", ans);}intMain () {srand (unsigned) time (NULL)); Init (); GA (); return 0;}View Code
Genetic algorithms to understand