This article was originally published by Spritelw in Http://blog.csdn.net/SpriteLW, can be reproduced at will, but without consent may not be modified, reproduced should retain this statement, otherwise held accountable.
Read the book is not as far as the road, today determined to write a SGA (simple genetic alogrithms) program, is to solve the unconstrained optimization problem.
Max F (x1,x2) = 21.5 + x1*sin (4 * pi *x1) + x2*sin (* pi * x2)
-3.0 <= X1 <= 12.1
4.1 <= x2 <= 5.8
This is the most easy genetic algorithm, but the results are disappointing, in the entire solution is convergent in the local optimal, only by increasing the rate of variation to obtain the global optimal, but the problem can be imagined: the global optimal solution is unstable, as if it is a flash.
Check the data only to find that the problem of coding design. I'm using binary encoding.
Coding is the first problem to be solved when applying genetic algorithm, and it is also a key step in the design of genetic algorithm. The coding method affects the operation method of the crossover operator, mutation operator and other genetic operators, which largely determines the efficiency of genetic evolution.
So far, a number of different coding methods have been proposed. In general, these coding methods can be divided into three major categories: binary coding, floating-point coding, symbolic coding method. In the following, we introduce some of the main coding methods from the specific implementation point of view.
1. binary encoding method:
It consists of a set of two value symbols consisting of binary symbols 0 and 1. It has some of the following advantages:
1) Simple coding and decoding operation
2) crossover, mutation and other genetic operations for easy implementation
3) conforms to the minimum character set encoding principle
4) using the model theorem to analyze the algorithm theoretically.
The disadvantage of binary coding is: for some continuous function optimization problem, because of its randomness makes its local search ability is poor, such as for some high-precision problems (such as above), when the solution is approaching the optimal solution, because of its variation after the change of the phenotype is very large, discontinuous, so it will be far away from the optimal solution, not stable. and Gray code can effectively prevent this kind of phenomenon
2. Gray code method:
Gray code method is such a coding method, its two consecutive integers corresponding to the encoding value of only one code bit is different. As the following table
Decimal |
Binary |
Gray Code |
0 |
0000 |
0000 |
1 |
0001 |
0001 |
2 |
0010 |
0011 |
3 |
0011 |
0010 |
4 |
0100 |
0110 |
5 |
0101 |
0111 |
6 |
0110 |
0101 |
7 |
0111 |
0100 |
8 |
1000 |
1100 |
9 |
1001 |
1101 |
10 |
1010 |
1111 |
11 |
1011 |
1110 |
12 |
1100 |
1010 |
13 |
1101 |
1011 |
14 |
1110 |
1001 |
15 |
1111 |
1000 |
Suppose there is a binary encoded B=BMBM-1...B2B1, and its corresponding gray code is G=GMGM-1...G2G1
The conversion formula from binary coded transcoding to Gray code is:
GM = BM
GI = Bi+1⊕bi, i=m-1,m-2,... 2,1
Conversion formula from gray code to binary is:
BM = GM
Bi = bi+1⊕gi, i=m-1,m-2,... 2,1
As can be seen from the above table, when a chromosome is mutated, its original manifestation and current phenotype are continuous.
The main advantages of gray code coding are:
1) Easy to improve the local search ability of genetic algorithm
2) crossover, mutation and other genetic operations for easy implementation
3) conforms to the minimum character set encoding principle
4) Easy to use the model theorem to analyze the algorithm theoretically
3. Floating- point encoding method
For some multidimensional, high-precision continuous function optimization problems, there are some disadvantages in using binary coding to represent an individual.
There is a mapping error in binary coding for continuous function discretization. When the individual length is better known, it may not reach the precision requirement, while the individual coding length is longer, although it can improve the precision, it makes the search space of genetic algorithm expand sharply.
The so-called floating-point method, refers to each individual gene value is expressed in a range of a floating-point number. In the floating-point coding method, it is necessary to ensure that the gene value within the given interval limit, the genetic algorithm used in the crossover, mutation and other genetic operators must also ensure that the results of the operation of the new individual's genetic value is also within the limits of the range.
The floating-point encoding method has several advantages:
1) is suitable for the number of large ranges represented in the genetic algorithm
2)