Genetic algorithm is a simulation of biological heredity. In the algorithm, a population is initialized, and each chromosome in the population is a solution. We measure the quality of this solution through the adaptive degree. And they are selected, mutated and crossed to find the best solution.
Summarize the basic steps of genetic algorithm:
1. Initialize a population and evaluate the fitness of individuals corresponding to each chromosome.
2. Selection, crossover and mutation produce new populations.
3. Re-evaluate the fitness value of each individual. If the fitness value meets the requirements or reaches the maximum number of cycles, otherwise repeat 2 to continue to generate a new population.
After understanding the general process of GA, let's analyze the details in detail and how to realize it.
We know that genetic algorithm originated from biological inheritance, so each individual in the population is a chromosome. So how to encode the chromosome so that it represents our solution (that is, encode the parameters to be optimized in reality into a chromosome). Here we encounter a problem of coding and decoding. We encode the target to be optimized into chromosomes, and then decode it into a solution that we can use to calculate fitness.
There are generally two ways to optimize parameters: real coding and binary coding.
Real number coding: Genes are directly represented by real numbers, which is relatively simple and does not need special decoding, but it is easy to converge prematurely and fall into local optimum when crossing and mutating.
Binary coding: genes are expressed in binary form, and parameter values are converted into binary form, which is easier to operate when crossing and mutating, and has good diversity, but it takes up a lot of storage space and needs decoding.
Chromosomes are called individuals. For an experiment, an individual is a solution that needs to be optimized, and many such individuals constitute a population.
Facing so many individuals in the group, how to judge the quality of individuals is to bring the solution into the fitness function through the fitness function. The larger the fitness value, the better the solution.
In genetic algorithm, how can we make the individuals better and better?
The core idea is: selecting the best and eliminating the bad, in order to produce a better solution, we should try our best to cross mutation and bring new solutions.
Selection is to select better individuals from the current population and eliminate bad individuals.
The common selection methods are: roulette wheel selection, tournament selection, best reservation selection, etc.
Roulette wheel selection is to determine the probability of each individual being selected according to the comparison between the fitness of each individual and the sum of all the fitness of the population, and then make n selections to form a new population, which is a way of putting back sampling.
Tournament is to select m individuals from a population at a time, choose the best one, put it into a new population, and repeat the selection until the number of individuals in the new population reaches N.
The best choice is to add fitness individuals to the new population on the basis of roulette, because roulette is a probability model, and the best individuals may not enter the new population.
After selection, it is necessary to consider generating new and better solutions and bringing new blood to the group. The idea of genetic algorithm is to cross two excellent solutions and often get a good solution.
Crossover: By randomly selecting a pair of parents in the selected population and crossing their chromosomes, a new individual is generated to replace the original solution.
Commonly used intersection methods include: single-point intersection, multi-point intersection and so on.
Crossover is like chromosome exchange genes in organisms ~ but not all individuals in the population cross. When it is realized, we can set a crossover rate and crossover probability, and randomly select two individuals and a random number in the population. If it is less than the crossover rate, we will carry out crossover operation and judge the degree of crossover according to the crossover probability, so as to produce new individuals, otherwise we will keep these two individuals.
Mutation is also a way to produce new individuals. By changing the genes in individuals, it is expected to produce better solutions. For example, in an individual with a binary code, 0, 1 in the binary code will change, that is, 0 will change to 1 and 1 will change to 0. It should also be considered that the mutation rate and the new solution generated by mutation are uncontrollable, which may be good or bad, and cannot guarantee a certain effect like crossover, so the mutation rate is often set to be relatively small.