Genetic Algorithm and direct search toolbox study note 9-genetic algorithm example

Source: Internet
Author: User

Let's take a specific example of a genetic algorithm and find the minimum value of the rastrigin function.

1. rastrigin's Function

In genetic algorithms, a function is often used to test the genetic algorithm. This function is the rastrigin function.

The rastrigin function is defined as follows:

The Genetic Algorithm Toolbox provides an M file, which is the implementation of this function. This file is rastriginsfcn. M. The following figure shows the rastrigin function with two independent variables.

In graphical representation, this function has many local minimum points, but only one global minimum point. This point is [0 0]. the value of the function at this point is 0. The main reason this function is used to test the genetic algorithm is that, it is very difficult for the traditional gradient-based algorithm to deal with this function with many local minimization points.

The following shows the contour map of the function.

2. Find the minimum value of the rastrigin Function

This section describes how to use genetic algorithms to find the minimum value of the rastrigin function.

2.1 enter optimtool ('ga ') in the command line window to open the graphical interface of the optimization tool

2.2 In
Fitness Function
Function Area input function handle @ rastriginsfcn

2.3 In
Number
Of variables: 2 independent variables

2.4 click "start" to start algorithm running

When the algorithm is runningCurrent
Iteration

The number of current iterations is displayed. You can use the pause and stop buttons to pause or

Stop an algorithm. After the algorithm is completedRun Solver
And view results

The running result is displayed.

The final result of algorithm running is: objective function value: 0.005160573464458196

When this value is obtained, the point is [0.002 0.005]. The reason for the algorithm to stop is: average change in the fitness value less than options. tolfun. (the average variation of the fitness function value is smaller than that of tolfun ).


3. Use the command line method to search for the minimum value of a function.

In the command line, enter the following command:

[X fval exitflag] = Ga (@ rastriginsfcn, 2)

The algorithm runs as follows:

Optimization terminated: average change in the fitness value less than options. tolfun.

X =

0.0000 0.0122

Fval =

0.0294

Exitflag =

1
The meaning of each field in the above result is not much explained. I believe you can understand it.


4. Graphic Display

In the graphical operation interface, there is
Plot Functions
The panel displays graphical representation of genetic algorithm information, which helps you adjust the algorithm selection and improve the algorithm running effect. For example, you can draw the optimal value and average value of the fitness function during each iteration, and select
Best fitness
.

When you click the "Start" button, the optimal value and average value of the fitness function will be constantly drawn in a graphical window. When the algorithm stops running, the drawn image is as follows.

In the image, the blue points represent the variation of the average value of the fitness function, while the black points represent the variation of the optimal fitness value in each iteration.


Because Random implementations are used in many aspects of the genetic algorithm, the results may not be the same each time. This is a normal phenomenon. If you make a fuss about this, if you suspect that there is a problem with the running results, you should take a look at the knowledge about genetic algorithm theory.



 

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.