Genetic algorithm steps and coding __c#

Source: Internet
Author: User
Tags character set sin

The overall overview of the implementation process of the genetic algorithm is as follows:

1, choose the coding strategy, convert the parameters into strings;

2, according to the population size n, randomly produces n string composition of the group;

3, according to the fitness function f=f (x) to calculate the fitness of each string;

4, according to the replication probability of the string f=f (x) Select a string to replicate, until already copied n strings, the higher the degree of adaptability, the greater the probability of replication;

5, after the copy of the string 22 pairing, crossover probability to cross;

6, the gene in each string is flipped according to mutation probability;

7. Repeat from 3 until a certain performance index or the specified genetic algebra is satisfied;


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) suitable for genetic algorithms with high accuracy requirements

3) Easy genetic search for larger spaces

4) improved the computational complexity of genetic algorithm and improved the rate of operation.

5) Facilitate the hybrid use of genetic algorithms and classical optimization methods

6) Facilitate the design of knowledge-based genetic operators for problem-specific knowledge

7) Easy to handle complex decision variable constraints

4. Symbol Encoding Method

Symbolic encoding method refers to the individual chromosome encoding string of the gene value taken from a non-numerical meaning, and only the code meaning of the symbol set as {a,b,c ... }。

The main advantages of symbol encoding are:

1) conforming to the principle of meaningful integrable block coding

2) facilitates the use of expertise in genetic algorithms for solving problems

3) facilitates the hybrid use of genetic algorithms and related approximation algorithms.

But for the genetic algorithm using the symbolic encoding method, it is generally necessary to design the operation method of crossover, mutation and other genetic operation to satisfy the various constraints of the village, so as to improve the search performance of the algorithm.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.