Genetic algorithm is a bionic algorithm, used to obtain the satisfactory solution of some problems, I feel this algorithm is very interesting, wrote this program (later hand over the big homework).
Source code: HTTP://PAN.BAIDU.COM/S/1BOSWRIF
The following copy from Baidu Encyclopedia:
Genetic algorithm (genetic algorithm) is a computational model of evolutionary process that simulates the natural selection and genetic mechanism of Darwinian evolution, and is a method of searching the optimal solution by simulating the natural evolutionary process. Genetic algorithms start with a population (population) that represents the potential solution set of a problem, while a population consists of a certain number of individuals (individual) encoded by gene. Each individual is actually a chromosome (chromosome) with a characteristic entity. Chromosomes as the main carrier of genetic material, that is, the collection of multiple genes, the internal expression (that is, genotype) is a gene combination, it determines the individual shape of the external manifestations, such as the characteristics of black hair is determined by the chromosome control of the characteristics of a certain genetic combination. As a result, mapping from phenotype to genotype is required at the outset to encode work. Since the work of the gene coding is very complex, we tend to simplify, such as binary coding, after the first generation of the population, according to the principle of survival of the fittest and the fittest, generational (generation) evolution produces a better approximate solution, in each generation, Based on the individual adaptation (fitness) size selection (selection) of individuals in the problem domain, combined crossover (crossover) and mutation (mutation) with the aid of genetic operators of natural genetics (genetic operators), Produces a population representing a new set of solutions. This process will lead to a population that is as natural as the evolution of the epigenetic population more adapted to the environment than the previous generation, the best individual in the last population is decoded (decoding), which can be used as the approximate optimal solution of the problem.
In the preferred process I chose the roulette:
int Group::select () //Wheel selection method
{
int p,i;
Double fitadd=0,sum=fitsum;
P = Randis ();
for (i = 0;i < group_num;i++)
{
Fitadd + = Group[i]. Fitness;
if ((Fitadd * 100.0/sum) > P)
return i;
}
return i;
}
Here is the code for the mutation process:
void Group::acrossandvariation ()//is responsible for crossing and variation of passage (int acrossnum1,acrossnum2,i;
for (i=0;i<group_num-1;i+=2) {acrossnum1=select ();
Acrossnum2=select ();
if (Randis () > P_across) {int Pos = Randpos (); memcpy (Groupnext[i]. DNA,GROUP[ACROSSNUM1].
dna,pos*sizeof (int)); memcpy (Groupnext[i]. DNA+POS,GROUP[ACROSSNUM2].
dna,dna_len*sizeof (int)); Point.del (Pos,groupnext[i].
DNA); memcpy (Groupnext[i+1]. DNA,GROUP[ACROSSNUM2].
dna,pos*sizeof (int)); memcpy (Groupnext[i+1]. DNA+POS,GROUP[ACROSSNUM1].
dna,dna_len*sizeof (int)); Point.del (Pos,groupnext[i+1].
DNA); } else {memcpy (groupnext[i]. DNA,GROUP[ACROSSNUM1].
dna,dna_len*sizeof (int)); memcpy (Groupnext[i+1]. DNA,GROUP[ACROSSNUM2].
dna,dna_len*sizeof (int));
} if (Randis () > p_variation) {int Pos1 = Randpos (), Pos2 = Randpos (), Temp; TeMP = Groupnext[i].
DNA[POS1]; Groupnext[i]. DNA[POS1] = Groupnext[i].
DNA[POS2]; Groupnext[i].
DNA[POS2] = Temp;
POS1 = Randpos ();
Pos2 = Randpos (); Temp = groupnext[i+1].
DNA[POS1]; GROUPNEXT[I+1]. DNA[POS1] = groupnext[i+1].
DNA[POS2]; GROUPNEXT[I+1].
DNA[POS2] = Temp;
}} memcpy (Group,groupnext,sizeof (Group)); }
Screenshot of Program:
The self-sensory genetic algorithm may not be a very fast algorithm, but its universality is very strong.