Introduction to Genetic algorithm---genetic algorithm of search-say algorithm

Source: Internet
Author: User
Tags prev

Optimization Algorithm Starter series article catalog (in update):

1. Simulated annealing algorithm

2. Genetic algorithms

Genetic Algorithms (GA, Genetic algorithm), also known as evolutionary algorithms. Genetic algorithm is a heuristic search algorithm, which is inspired by Darwin's theory of evolution and used for reference to the evolutionary process of biology. So before introducing genetic algorithm, it is necessary to introduce the knowledge of biological evolution briefly.

I. Evolutionary knowledge  

As an introduction to the biological background of genetic algorithms, the following information can be learned:

population (Population) : The evolution of organisms is carried out in the form of groups, a group called a population.

individual : a single organism that makes up a population.

Gene (Gene) : a genetic factor.  

chromosome (chromosome) : Contains a set of genes.

The survival of the fittest : A high degree of environmental adaptability, cattle B's individual participation in reproductive opportunities are more, offspring will be more and more. Low-adaptability individuals are less likely to participate in reproduction, and fewer offspring.

heredity and mutation : The new individual inherits the genes from each part of the parent, and at the same time has a certain probability of genetic variation.

Simply put: the reproductive process, the genetic crossover (Crossover), Gene mutation (Mutation), Fitness (Fitness) low-level individuals will be phased out, and more adaptable individuals will be more and more. Then after the natural selection of the n generation, the individuals that are preserved are highly adaptable, which is likely to contain the highest degree of adaptability in history.

two. Genetic algorithm ideas  

Using the theory of biological evolution, genetic algorithm will solve the problem of simulation into a process of biological evolution, through replication, crossover, mutation and other operations to produce the next generation of solutions, and gradually eliminate the low degree of fitness function value of the solution, increase the value of the solution of high fitness function. This evolution of n generation is likely to evolve an individual with a high degree of fitness function.

For example, using a genetic algorithm to solve the "0-1 knapsack problem" idea: 0-1 the solution of the backpack can be encoded as a string of 0-1 strings (0: No, 1: take); first, randomly produce m 0-1 strings and then evaluate the merits of these 0-1 strings as a solution to the 0-1 knapsack problem; Randomly select some strings to generate the next generation of M strings by crossing, mutation, and more, and the better solution is chosen in a higher probability. After the evolution of the G generation, it is possible to produce an "approximate optimal solution" for the 0-1 knapsack problem.

Coding : You need to encode the solution of the problem in the form of a string to use a genetic algorithm. The simplest encoding is the binary encoding, which encodes the solution of the problem into the form of a bits array. For example, the solution to the problem is an integer, so it can be encoded in the form of an bits array. The solution to the 0-1 string as a 0-1 knapsack problem belongs to the binary encoding.

Genetic algorithms have 3 of the most basic operations: selection, crossover, mutation.

selection : Select some chromosomes to produce the next generation. A common selection strategy is "Proportional selection", i.e. the probability that an individual is selected is proportional to the value of its fitness function. Assuming that the total number of individuals in a group is m, then the probability of a body Xi being selected is f (Xi)/(f (X1) + f (X2) + ... + f (Xn)). The proportional selection implementation algorithm is called "Roulette algorithm" (Roulette Wheel Selection), a simple implementation of the roulette algorithm is as follows:

Roulette Betting algorithm

/*
* Randomly select an individual at a set probability
* P[i] indicates the probability that the first individual is selected
*/
int RWS ()
{
m = 0;
R =random (0,1); R is a random number from 0 to 1
for (i=1;i<=n; i++)
{
/* The random number generated in m~m+p[i] is considered to be selected I
* So the probability of I being selected is p[i]
*/
m = m + p[i];
if (r<=m) return i;
}
}

Cross (Crossover): 2 chromosomes exchange part of the gene to construct the next generation of 2 new chromosomes. For example:

Before crossing:

00000|011100000000|10000

11100|000001111110|00101

After crossing:

00000|000001111110|10000

11100|011100000000|00101

Chromosome crosses occur at a certain probability, and this probability is recorded as a PC.

mutation (Mutation): In the breeding process, the genes in the newly-produced chromosomes will go wrong in a certain probability, called mutations. The probability of mutation occurrence is recorded as PM. For example:

Before mutation:

000001110000000010000

After mutation:

000001110000100010000

Fitness Function (Fitness Function): Used to evaluate the fitness of a chromosome, expressed in F (x). Sometimes it is necessary to distinguish the fitness function of chromosomes from the objective function of the problem. For example: The objective function of the 0-1 knapsack problem is the value of the obtained item, but it may not necessarily be appropriate to use the value of the item as a function of the chromosome's fitness. The fitness function is positively correlated with the objective function, and the function of fitness can be obtained by some deformation of the objective functions.

three. Pseudo-code for basic genetic Algorithms  

Basic genetic algorithm pseudo-code

/*
* Pc: Probability of crossover occurrence
* Pm: probability of mutation occurrence
* M: Population size
* G: Termination of evolutionary algebra
* TF: Evolution of any individual's fitness function over Tf, can terminate the evolutionary process
*/
Initializes parameters such as PM,PC,M,G,TF. Random generation of first-generation population pops

Do
{
Calculates the fitness f (i) for each individual in a population pop.
Initializing an empty population of newpop
Do
{
Selecting 2 individuals from a population pop based on the proportional selection algorithm of fitness
if (random (0, 1) < Pc)
{
Cross-operation for 2 individuals by cross-probability pc
}
if (random (0, 1) < Pm)
{
Mutation operation for 2 individuals by mutation probability PM
}
Adding 2 new individuals to the population newpop
} until (created by M descendants)
Replace pop with Newpop
}until (any chromosome that scores more than TF, or reproduction algebra over g)

Four. Basic Genetic algorithm Optimization  

The following methods can optimize the performance of genetic algorithms.

elitism (elitist strategy) selection : is an optimization of the basic genetic algorithm. In order to prevent the optimal solution of evolutionary process from being destroyed by crossover and mutation, the optimal solution of each generation can be copied intact to the next generation.

insert Operation : An insert operation can be added based on 3 basic operations. The insert action shifts a random fragment of a chromosome to another random position.

Five. Using aforge.genetic to solve TSP problems

Aforge.net is a C # implementation of the open-source architecture for AI, computer vision and other fields. The aforge.net contains a class library with a genetic algorithm.

Aforge.net Home: http://www.aforgenet.com/

Aforge.net Code Download: http://code.google.com/p/aforge/

Introduce the genetic algorithm usage of aforge. The class structure of the aforge.genetic is as follows:


Figure 1. Class diagram of Aforge.genetic

Use Aforge.genetic to write a simple example of solving the TSP problem. Test data set the coordinates of 31 provincial cities in China, which are spread online:

36391315
37121399
33261556
41961004
4386570
25621756
23811676
37151678
40612370
36762578
42632931
35072367
34393201
31403550
27782826

Operation Process:

(1) Download Aforge.net class library, website: http://code.google.com/p/aforge/downloads/list

(2) Create a C # empty project GENTICTSP. Then locate AForge.dll and AForge.Genetic.dll in the Aforge directory and copy it to the Bin/debug directory of the TESTTSP project. Add these two DLLs to the project again through add Reference.

(3) Save 31 city coordinate data as bin/debug/data.txt.

(4) Add TSPFitnessFunction.cs, adding the following code:

Tspfitnessfunction class

Using System;
Using Aforge.genetic;

Namespace GENTICTSP
{
<summary>
Fitness function for TSP task (travaling salasman problem)
</summary>
Publicclass tspfitnessfunction:ifitnessfunction
{
Map
privateint[,] map =null;

Constructor
Public Tspfitnessfunction (int[,] map)
{
This.map = map;
}

<summary>
Evaluate chromosome-calculates its fitness value
</summary>
Publicdouble Evaluate (Ichromosome chromosome)
{
RETURN1/(Pathlength (chromosome) + 1);
}

<summary>
Translate genotype to phenotype
</summary>
Publicobject Translate (Ichromosome chromosome)
{
Return chromosome. ToString ();
}

<summary>
Calculate path length represented by the specified chromosome
</summary>
Publicdouble pathlength (Ichromosome chromosome)
{
Salesman Path
ushort[] Path = ((Permutationchromosome) chromosome). Value;

Check path size
if (path. Length! = map. GetLength (0))
{
Thrownew ArgumentException ("Invalid path Specified-not all cities is visited");
}

Path length
int prev = path[0];
int curr = Path[path. LENGTH-1];

Calculate distance between the last and the first city
Double dx = map[curr, 0]-map[prev, 0];
Double dy = map[curr, 1]-map[prev, 1];
Double pathlength = math.sqrt (dx * dx + dy * dy);

Calculate the path length from the
for (int i =1, n = path. Length; I < n; i++)
{
Get current City
Curr = Path[i];

Calculate distance
DX = map[curr, 0]-map[prev, 0];
DY = Map[curr, 1]-map[prev, 1];
Pathlength + = math.sqrt (dx * dx + dy * dy);

Put current city as previous
prev = Curr;
}

return pathlength;
}
}
}

(5) Add GenticTSP.cs, adding the following code:

GENTICTSP class

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.IO;

Using Aforge;
Using Aforge.genetic;


Namespace GENTICTSP
{
Class GENTICTSP
{

Staticvoid Main ()
{
StreamReader Reader =new StreamReader ("Data.txt");

int citiescount = 31; Number of cities

int[,] map =newint[citiescount, 2];

for (int i =0; i < citiescount; i++)
{
String value = Reader. ReadLine ();
string[] Temp = value. Split (');
Map[i, 0] =int. Parse (Temp[0]); Reading City coordinates
Map[i, 1] =int. Parse (temp[1]);
}

Create fitness function
Tspfitnessfunction fitnessfunction =new tspfitnessfunction (map);

int populationsize = 1000; The largest population

/*
* 0:eliteselection algorithm
* 1:rankselection algorithm
* Other: Roulettewheelselection algorithm
* */
int selectionmethod = 0;

Create population
Population Population =new Population (populationsize,
New Permutationchromosome (Citiescount),
Fitnessfunction,
(Selectionmethod ==0)? (Iselectionmethod) New Eliteselection ():
(Selectionmethod ==1)? (Iselectionmethod) New Rankselection ():
(Iselectionmethod) New Roulettewheelselection ()
);

Iterations
int iter = 1;
int iterations = 5000; Iteration Maximum Period

Loop
while (ITER < iterations)
{
Run one epoch of genetic algorithm
Population. Runepoch ();

Increase current Iteration
iter++;
}

System.Console.WriteLine ("Traversal path is: {0}", ((permutationchromosome) population. Bestchromosome). ToString ());
System.Console.WriteLine ("Total distance is: {0}", Fitnessfunction.pathlength (population). Bestchromosome));
System.Console.Read ();

}
}
}

The best result of this group of TSP data is said to be 15404, the above procedure I have tried several times the best time to work out 15402.341, but the worst time also ran out of more than 16000 results.

I also have a version that sets the population size to 1000 and iterates 5,000 times to figure out the result of 15408.508. The source code can be downloaded at the end of the article.

Summarize the general steps to solve the problem using aforge.genetic:

(1) Define the adaptive function class, need to implement Ifitnessfunction interface

(2) The selection of population size, selection algorithm used, chromosome type and other parameters, to create a population population

(3) Set the maximum number of iterations and use Runepoch to start the calculation

This article source code download

Http://www.cnblogs.com/heaad/archive/2010/12/23/1914725.html

Introduction to Genetic algorithm---genetic algorithm of search-say 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.