Introduction to evolutionary computing and implementation of genetic algorithms-Use of the aforge. NET Framework (6)

Source: Internet
Author: User

I started school, and I was busy...

The previous article introduced the use of aforge. Net on artificial neural networks, but I still don't think it is enjoyable. MATLAB is not used to it, so I thought about evolutionary computing again.

Introduction to evolutionary computing

Evolutionary computing is not a new method. A large number of researchers have made great efforts, which leads to a large number of evolutionary computing.Algorithm. They not only study algorithms themselves, but also expand the application scope of algorithms.

As we all know, there are a lot of complex problems in the real world. Some of them cannot be accurately solved in a reasonable period of time using conventional methods, while the other part does not even have effective solutions.

The most famous example is the TSP problem. This problem is intended to seek the minimum path cost for a single traveler to return to the origin point after all given request points.

Evolutionary computing can be applied to these problems, because in most cases such problems allow us to give optimal solutions within a reasonable period of time.

Evolutionary computing does not guarantee the best solution to a specific problem, but it can find a good solution, which may be very close to the best solution.

Branch and Application of Evolutionary Algorithms

Evolutionary computing is a general term for some algorithms, including genetic algorithms (GA Genetic Algorithm), Genetic Programming (GP genetic planning), and Gene Expression Programming (GEP Gene Expression Programming ).

Evolutionary Algorithms can solve the following problems:

1. Function Optimization

2. symbol Regression

3. Time Series Prediction

4. Traveling Salesman Problem

Introduction to Genetic Algorithms

Genetic algorithms (Genetic Algorithm) was first proposed by John Holland in 1960 Based on the evolutionary point of view. Since then, relevant research has continued.

Most of the research results have been applied to many fields and achieved good results. Although genetic algorithms have a long history, there are still new methods that have been proposed to expand the application field.

 

Genetic algorithms are based on Darwin's theory of "survival of the fittest" and biological evolution of genetic mechanisms. Algorithms act on each generation of genes, and each gene is a possible solution to the problem.

Generally, there are four steps to apply a genetic algorithm:

1. Randomly select an individual and perform crossover

2. Variation

3. Calculate fitness

4. Select the next generation of individuals

The algorithm's stop condition is generally the completion of a specified number of iterations or a reliable solution.

The simplest single-point crossover algorithm is to randomly select a point of two genes and exchange a part of the two genes.

Genes 0-1 0 01 1 0 1
2: 1 0 01 0 0 0
Result: 0 0 0 1 0 0

Another good way is to randomly select two vertices of two genes to exchange the parts between the two vertices.

Single point of variation is generally used.

Genes 1:0 1 0 010 1
Result: 0 1 0 000 1
Use aforge. Net to implement Genetic Algorithms

I think the advantage of aforge. NET is not to provide specific implementations, but to provide a scalable framework for learning and research.

Taking function optimization as an example. Function I select: x ^ 0.5 + sin (x/23) * 30 range from 0 to 100

First use MATLAB to estimate the optimal value

Establish a fitness evaluation function;

Public ClassMyownfunction: optimizationfunction1d
{
PublicMyownfunction ()
:Base(NewAforge. Range (0,100))
{
}

Public Override DoubleOptimizationfunction (DoubleX)
{
ReturnMath. SQRT (x) + math. Sin (x/23)*30;

}
}

The main category of genetic algorithms is population. It contains all chromosomes and provides fitness evaluation methods, encoding methods, and selection methods.

Myownfunction F =NewMyownfunction ();
Population =NewPopulation (40,NewBinarychromosome (32), F,NewEliteselection ());

This sectionCodeThe fitness function uses the custom myownfunction. The encoding uses binary encoding. The length is 32, and the number of individuals in each generation is 40. The selection method is "best choice ".

This name is very domineering, in fact, it is to sort the order, and then remove the bad, the Code is as follows;

 Public   Class Eliteselection: iselectionmethod
{
/// <Summary>
/// Initializes a new instance of <See CREF = "eliteselection"/> Class.
/// </Summary>
Public Eliteselection (){}

/// <Summary>
/// Apply selection to the specified population.
/// </Summary>
///
/// <Param name = "chromosomes"> Population, which shoshould be filtered. </Param>
/// <Param name = "size"> The amount of chromosomes to keep. </Param>
///
/// <Remarks> Filters specified population keeping only specified amount of best
/// Chromosomes. </Remarks>
///
Public Void Applyselection (list <ichromosome> chromosomes, Int Size)
{
// Sort Chromosomes
Chromosomes. Sort ();

// Remove bad Chromosomes
Chromosomes. removerange (size, chromosomes. Count-size );
}
}

In general, you can select the next generation using the gambling wheel. Complete code:

Console. writeline ( "  Start!  " );
Myownfunction F = New Myownfunction ();
Population = New Population ( 40 ,
New Binarychromosome ( 32 ), F, New Eliteselection ());
Population. runepoch ();
Double Goodx = f. Translate (population. bestchromosome );
Console. writeline ( " Best chromosome ==>{ 0} " , Goodx );
Console. writeline (" Best result ==>{ 0} " , F. optimizationfunction (goodx ));
Console. writeline ( " Over! " );
Console. Readline ();

Effect:

Basically, it is about 0.0002 different from the estimated value of Matlab.

If you want to output all the results of the last generation, you can use

For(IntI =0; I <population. size; I ++)
{
Console. writeline ("Chromosome {0 }=>{ 1}", I, F. Evaluate (population [I]);
}

The result is as follows:

Conclusion:

1. I personally think that aforge. NET is easier to use than MATLAB, because its overall architecture is relatively unified, unlike MATLAB, which is provided by the toolbox, with different use styles.

2. the evolutionary computing of aforge. NET is far more complete than the neural network, and basically does not need to be implemented by yourself.

3. The representation of membership functions in aforge. Fuzzy is not rich enough, and only two intermediate types are implemented. I expanded the representation of other types, and I will release it after finishing the sorting.

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.