Genetic Algorithm (Genetic Algorithm)

1. Introduction

Genetic Algorithm (Genetic Algorithm) follows the principles of "survival of the fittest" and "survival of the fittest". It is a kind of randomized search algorithm that draws on the natural selection and natural genetic mechanisms of the biological world.

The genetic algorithm simulates the evolution process of an artificial population. Through the mechanisms of Selection, Crossover and Mutation, a set of candidate individuals are retained in each iteration, and this process is repeated. After the population has evolved for several generations , Under ideal circumstances, its fitness reaches the state of *** approximate optimal *** .

Since the genetic algorithm was proposed, it has been widely used, especially in the fields of function optimization, production scheduling, pattern recognition, neural network, adaptive control, etc., genetic algorithm has played a very important role and improved the solution of some problems. effectiveness.


2. Composition of genetic algorithm

  • Code -> Create Chromosome
  • Individual -> Population
  • Fitness function
  • Genetic operator
    • select
    • cross
    • Mutations
  • Operating parameters
    • Whether to choose elite operation
    • Population size
    • Chromosome length
    • The maximum number of iterations
    • Crossover probability
    • Mutation probability


2.1 Encoding and Decoding

The first step in implementing genetic algorithms is to clarify the encoding and decoding methods for solving the problem.

For function optimization problems, there are generally two encoding methods, each with advantages and disadvantages

  • Real number coding: directly use real numbers to represent genes, which is easy to understand and does not require a decoding process, but it is easy to converge prematurely and fall into local optimality
  • Binary encoding: high stability, large population diversity, but requires large storage space, needs to be decoded and is difficult to understand


For solving the function maximum problem, I chose binary encoding.
Take our objective function f(x) = x + 10sin(5x) + 7cos(4x), x∈[0,9] as an example.

If the accuracy of the solution is set to 4 digits after the decimal point, the solution space of x can be divided into (9-0)×(1e+4)=90000 equal parts.

2^16<90000<2^17, 17-bit binary numbers are required to represent these solutions. In other words, a solution code is a 17-bit binary string.

In the beginning, these binary strings are randomly generated.

One such binary string represents a chromosome string, where the length of the chromosome string is 17.

For any such chromosome, how to restore (decode) it to a value in the interval [0,9]?

For this problem, we can use the following formula to decode:

x = 0 + decimal(chromosome)×(9-0)/(2^17-1)

decimal( ): Convert a binary number to a decimal number.

Generalized decoding formula:

f(x), x∈[lower_bound, upper_bound]
x = lower_bound + decimal(chromosome)×(upper_bound-lower_bound)/(2^chromosome_size-1)

lower_bound: the lower limit of the function domain
upper_bound: the upper limit of the function domain
chromosome_size: the length of the chromosome

Through the above formula, we can successfully decode the binary chromosome string into a decimal real number solution in the interval [0,9].

2.2 Individual and population

"Chromosomes" express a certain characteristic, and the carrier of this characteristic is called "individual".

For the problem of solving the maximum value of the unary function to be solved in this experiment, the individual can be represented by the chromosome constructed in the previous section, and there is one chromosome in an individual.

Many such individuals form a population, which means a one-dimensional point set (line segment [0,9] on the x-axis).

2.3 Fitness function In

genetic algorithm, the quality of an individual (solution) is evaluated by the value of the fitness function. In this problem, f(x) is the fitness function.

The larger the fitness function value, the higher the quality of the solution.

The fitness function is the driving force for the evolution of genetic algorithms and the only criterion for natural selection. Its design should be combined with the requirements of the problem itself.

2.4 Genetic operators

We hope to have such a population, the function values ​​corresponding to the individuals it contains are very close to the maximum value of f(x) on [0,9], but this population may not be so good at the beginning. Because the individual chromosome strings are randomly generated.

How to make the population excellent?

Continuous evolution.

In each evolution, we try to keep the excellent individuals in the population as much as possible, weed out the undesirable individuals, and carry out chromosomal crossover between the excellent individuals, and some individuals may also mutate.

Every time a population evolves, an optimal individual will be produced. The optimal individual of all generations of the population may be the point in the domain corresponding to the maximum value of the function f(x).

If the population evolves endlessly, the best solution can always be found. But in fact, we have limited time, and usually stop evolution when we get a solution that looks good.

For a given population, how to give it the ability to evolve ?

  • First choice (selection)
    • The selection operation is to select most pairs of superior individuals from the previous generation population. A pair of superior individuals is called a pair of parents, and parents are allowed to pass their genes to the next generation until the number of individuals in the next generation reaches Upper limit of population
    • Before the selection operation, arrange the individuals in the population according to their fitness from small to large
    • Using roulette selection method (of course there are many other selection methods), the probability of each individual being selected is proportional to the value of its fitness function
    • The roulette selection method is random, and better individuals may be lost in the selection process, so the elite mechanism can be used to directly select the best individuals of the previous generation
  • Followed CROSS (Crossover)
    • Two different chromosomes to be crossed (parents) exchange part of their genes in a certain way according to the cross_rate
    • Single point crossover method is used, other crossover methods can also be used
  • Finally variation (mutation)
    • Chromosomes are mutated according to the probability of mutation (mutate_rate)
    • Single point mutation method is used, other mutation methods can also be used


Generally speaking, the cross rate (cross_rate) is relatively large, and the mutation rate (mutate_rate) is extremely low. For problems like solving the maximum value of a function, I set the cross rate (cross_rate) to 0.6 and the mutation rate (mutate_rate) to 0.01.

Because the genetic algorithm believes that the crossover of two excellent parent chromosomes is more likely to produce excellent offspring, and the possibility of producing excellent offspring if mutated is extremely low, but there is also the possibility of mutating very good offspring at once. This is also in line with the characteristics of biological evolution in nature.


3. Genetic algorithm process I
wrote a test program under matlab.
Attach the code https://github.com/yanshengjia/artificial-intelligence/tree/master/genetic-algorithm-for-functional-maximum-problem

test results

  • The best individual: 00011111011111011
  • Optimal fitness: 24.8554
  • The optimal individual corresponds to the independent variable value: 7.8569
  • The number of iterations required to achieve the optimal result: After many experiments, it is found that the number of iterations to achieve convergence varies from 20 to more than 100


The relationship between the number of iterations and the average fitness (horizontal axis: iteration number, vertical axis: average fitness).


There are ready-made tools that can directly use genetic algorithms, such as Matlab.
Finally, I will introduce how to use genetic algorithms in Matlab.

Use GA in MATLAB

1. Open the Optimization tool, select ga-genetic algorithm in Solver, fill in @target in Fitness function

2. Create a new target.m in your project folder, and note that the current path of MATLAB is yours The path of the project folder

3. Write down the fitness function in target.m, for example

function [ y ] = target(x)
y = -x-10*sin(5*x)-7*cos(4*x);
end

*GA in MATLAB only solves the (approximate) minimum value of the function, so the objective function must be reversed first .

4. Open the Optimization tool, enter the number of variables and the value of the independent variable domain (Bounds), click Start, and the genetic algorithm will run. Finally, you can see the (approximate) minimum value of the function in the output box, the number of iterations to this degree (Current iteration) and the value of the final independent variable (Final point)

5. In the Optimization-ga tool, there are many options . Through these options, you can set the following properties

  • Population (Population)
  • Selection
  • Crossover (Crossover)
  • Mutation
  • Stopping criteria
  • Plot functions

Guess you like

Origin blog.csdn.net/qq_41371349/article/details/105157871