Solving the optimal value by genetic algorithm

Source: Internet
Author: User

1. Introduction to Genetic algorithms

Genetic algorithm, a computational model that simulates the evolution of natural selection and biological Evolution , is an algorithm that constantly chooses good individuals. When it comes to genetics, think about the nature of animal genetics, the main process of nature, including chromosome selection, crossover, mutation, after these operations, to ensure that the future is basically the best, then continue to iterate in this way, you can always be optimal.

2, the problem solved

Genetic algorithms can solve many problems, but the genetic algorithm is mainly to solve the problem of optimization , especially the kind of a complex problem that can not be solved directly, and the actual situation is usually the case.

This part is mainly to understand the application of genetic algorithm, select a complex two-dimensional function to optimize the genetic algorithm, the function is shown as Y=10*sin (5*x) +7*abs (x-5) +10, the function image is:

Of course functions you can also arbitrarily assume and write, as long as the match can. Ask for its maximum value (this seems to be possible, it is easy to write----if it is more complicated to estimate it is not) such problems if the genetic algorithm or other optimization method is very simple, for example, I divide X into 1 million parts, and then all of a sudden value into the calculation, to find the corresponding 1 million Y value, Compare their size to find the maximum value is not OK, very stupid, people are impossible, but the computer can. Genetic algorithm is also very stupid search , but the home a little something, is artificial to its calculation of the direction and strategy , let it have the purpose of the calculation, which is the algorithm.

3, how to start?

The first is the choice of individuals . Genetic algorithm has a number of individuals, so that the chance of mutual integration, the resulting offspring will be diverse, will have a better gene, beneficial to the development of the population. So the algorithm is the same, of course, the individual is a problem, generally speaking, 20-100 I feel almost. So what exactly is an individual ? In our question, it's naturally x-value. In other cases, the individual is the variable of the problem, here we assume that the individual number selected 100, that is, to start selecting 100 different X-values, do not understand the assumption is 100 monkeys. Well, now that we have a population of 100 monkeys, how can this population grow better? Speaking of which, let's think about how the definition of this is getting better. This should have an evaluation indicator . In our problem, it seems that the greater the Y value, the better it is. We can even give them a name to decide what's good and what's not. We call this the adaptation of the individual, which should be considered the second half of the algorithm.

4. Coding

First understand what is coding? Why encode? How to encode?

OK, what is coding? In fact, the code is to change the argument (x) in the form of a computer can recognize the way, in this form, it is easier to manipulate other processes (such as crossover, mutation, etc.). The general encoding is binary encoding, natural number encoding, matrix encoding and so on, two use the most can be said to be binary code, feeling this and human DNA, gene arrangement is very similar. Think about how DNA is lined up? Is it a pair of lines on two long chains? So what is binary encoding? Very simple, is the 1,0,1,0 corresponding to the combination of the back and forth. For example: 1100100010, 0011001001, and so on, these are binary encodings with a bit length of 10. Then another question, how to expand this precision? If you want to keep 0-5 unchanged, can only increase the number of digits, the 9-bit coding 10-bit, 20-bit, 100-bit, wow, big enough, into 100 0,1 combination, very scary bar, in fact, how much depends on the situation, the general sense of 20 people can be, although said the bigger the better, But too big to consume the memory, the speed is slow, is not worth.

5. About crossover and variation

Mutation is called mutation, with the concept of coding, which is based on the code of mutation ( a code changed ). The simplest variant is the mutation of a single point. Now in 10-bit long code, for example, to encode x=3, arbitrarily assumed to be 11000 10010, well, in the mutation operation, assuming that the 5th bit variant (0 to 1 · 1 to 0), then what became of this time? That certainly is not 3, is how much is certainly can inverse calculate back, here is lazy to forget, assume 3.213 bar, hair did not find, so, X is not changed? Since the change is good, bring to the original function (fitness function) to compare the two x value corresponding to which Y value of the first write, if the later variant of the larger is not to say that produced a good mutation ah, you can choose it in the next individual choice. So, think of a lot of x, what about a mutation? Will certainly generate a lot of solutions, what happens repeatedly? As long as the optimal solution is retained every time, I would like to cycle 1 million times, can always find solutions, of course, so many times to spend, it is not appropriate, this is only a point in the mutation, if I let more than one point mutation? Wow, it's incredible, it's changed a lot. Of course, the mutation is more than that, more to see the professional papers it. Know what the mutation is doing, the rest of the say, well, this is still a variation, think about the nature of genetic in addition to the mutation what? Cross it, then what's the cross?

What do you know when animals mate, that some chromosomes do? Did you cross it? is to exchange the genes in the corresponding parts. Then take the code as an example (a piece of code has changed to exchange each other ), such as now randomly from 100 x value to choose two bar, chicken coop just select x=3 and 4, the corresponding coding hypothesis is 11001 10101 and 00101 01011, then how to cross it? We know that each cross is usually a piece of chromosomes, well, here is a piece of algorithm design. For example, the position in the 2,3,4 code to the overall cross, then x=3 corresponding position is 100 bar, x=4 corresponding position is 010 bar, good, after the exchange x=3 corresponding position becomes the 010,x=4 corresponding position becomes 100, add back becomes what? X=3 is not 10101 10101,x=4 is not 01001 01011. And now, will they re-encode them back to x=3 and x=4? Obviously it is not (of course there is a small probability is the same, very small). What is that? Do not want to calculate, or assume that the assumption is 3.234 and 4.358, OK, the new individual is not again? Well, in the same vein, take the fitness function and take the good ones. Similarly, some specialize in the development of this algorithm out of a variety of cross-way, what an individual's money 3 with the latter after the next three crosses, the middle of a few to cross and so on, in short, the production of new individuals. and where is the purpose of this? Is nothing more than three words, randomness, fully guarantee the production of new individuals with randomness , you say your x=3 mutation for 3.2,3.2 what the distance of 3 so near, in some of the problem of local optimal solution will never jump out of the local optimal solution, on the contrary, your x=1 suddenly mutated into a x=5, wow, Great change, from this head to the head, which is very good for the vast search ability of the algorithm.

6, about the choice of questions

Select the most qualified individuals , corresponding to this question, each crossover or mutation is not a new individual? If these individuals are preserved, the population is going to explode, and in the algorithm, we also stipulate that each cycle must be guaranteed to be 100 individuals. So you have to cull 100 out of 200 individuals, how do you get rid of it? Some people say it is very simple, ranking it, take the first 100th number x is not OK? Is it really good to rank this thing? I do not believe, with what almost can not be selected, do not make the next mutation in a sudden rushed to the first? This question in the choice also has some corresponding rules , the most common is the roulette method, is simply a probability selection method (of course, there are many other methods, interested in their own search related documents, I also used). What is the Roulette method? is the corresponding all Y-values (fitness function values) added up, with the respective Y value to remove the sum value, so who is the probability of a large who's probability is very clear? And then randomly generate a 0-1 probability value p, who in the range of P is not the choice of who, for example, x=3 in 100 x in the value of Y is the largest, then the probability of choosing it is not the largest, for example, 0.1 (0.1 small?) Well, think of what other will be, than 0.1 small, then from the probability, choose 100 times, is not there 10 times to preach X=3, the other is less than 10 times is it, then in the next 100 species of the individual there are 10 x=3, and then a return may have 20 x=3 of the individual. Then there are 30, and finally only 100 x=3 of the individual, where its own cross-variation has no meaning, if this time means that the algorithm can be ended. More details, as shown: Now in the following three large categories to select 100 x individual, Roulette 100 times after the turn, is not an art fell in the S3 of the individual more, the principle of choice is so, do not understand the program directly behind it.

7, almost what?

At this point, feel the same, after the choice is repeated in the above steps cross, mutation and so on, then when is the head? Very simple, the way is the number of iterations , iterative 10 times to look at the results, 20 times, look at the results, 30 times, 40 times, 100 times, when the number of times reached a certain degree, excellent individuals more and more, mostly concentrated in the vicinity of the optimal solution, even if the mutation or crossover is also near the optimal solution, No effect. The next choice will be back again, so this is really over. For example, first come to the result, the question according to my thinking, after the iteration 100 times become what? Look at the picture:

Solving the optimal value by genetic 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.