📜 ⬆️ ⬇️

Genetic algorithm. Just about the difficult


Recently, more and more "talk" goes about new-fangled algorithms, such as neural networks and the genetic algorithm. Today I will talk about genetic algorithms, but this time let's try to do without abstruse definitions and complex terms.
As one of the great scientists said: “If you cannot explain your theory to your wife, your theory is worth nothing!” So ​​let's try to figure everything out in order.

Pinch of history


As Wikipedia says: "The founding father of genetic algorithms is John Holland, who invented using genetics for his own purposes in 1975." For reference, the same year Altair 8800 appeared, and no, this is not a terrorist, but the first personal computer. By that time, John was already 46 years old.

Where is it used


Since the algorithm is self-learning, the range of applications is extremely wide:

Operating principle


The genetic algorithm is primarily an evolutionary algorithm, in other words, the main feature of the algorithm is the crossing (combination). As it is easy to guess, the idea of ​​the algorithm is brazenly taken from nature, since it will not be sued for it. So, by going through and most importantly the selection turns out the right "combination".
The algorithm is divided into three stages:

If the result does not suit us, these steps are repeated until the result starts to satisfy us or one of the following conditions occurs:

More on steps

Creating a new population . At this step, an initial population is created, which, quite possibly, will not be kosher, but it is likely that the algorithm will correct this problem. The main thing is that they conform to the "format" and be "adapted to breeding."
Reproduction . Well, everything is like that of people, it takes two parents to get a descendant. The main thing is that a descendant (child) can inherit their features from their parents. In this case, everyone breeds, not only the survivors (this phrase is especially absurd, but since we are all in a spherical vacuum, then everything is possible), otherwise one alpha male will stand out, the genes of which will block all the others, but for us this is not acceptable in principle .
Mutations . Mutations are similar to reproduction, from mutants choose a certain number of individuals and change them in accordance with pre-defined operations.
Selection Here the sweetest begins, we begin to choose from the population the proportion of those who "go on." At the same time, we define the share of “survivors” after our selection in advance with our hands, indicating as a parameter. Sadly, the rest of the individuals must die.

Practice


You have successfully listened to the "fairy tale" about the miracle algorithm, and it is quite possible that you were waiting for when we begin to exploit it at last, I want to please you, the time has come.
Let's look at the example of my favorite Diophantine equations (Equations with integer roots).
Our equation: a + 2b + 3c + 4d = 30
You probably already suspect that the roots of this equation lie on the interval [1; 30], so we take 5
random values ​​a, b, c, d. (The limit of 30 is taken specifically to simplify the task)
And so, we have the first generation:
  1. (1,28,15,3)
  2. (14,9,2,4)
  3. (13,5,7,3)
  4. (23,8,16,19)
  5. (9,13,5,2)

In order to calculate the survival rates, we substitute each solution in the expression. The distance from the received value to 30 will be the desired value.
  1. | 114-30 | = 84
  2. | 54-30 | = 24
  3. | 56-30 | = 26
  4. | 163-30 | = 133
  5. | 58-30 | = 28

Smaller values ​​are closer to 30, respectively, they are more desirable. It turns out that larger values ​​will have a lower survival rate. To create a system, we calculate the probability of choosing each (chromosome). But the solution is to take the sum of the inverse of the coefficients, and from this calculate the percentages. ( PS 0.135266 - the sum of inverse coefficients )
  1. (1/84) /0.135266 = 8.80%
  2. (1/24) /0.135266 = 30.8%
  3. (1/26) /0.135266 = 28.4%
  4. (1/133) /0.135266 = 5.56%
  5. (1/28) /0.135266 = 26.4%

Next, we will choose five pairs of parents who will have exactly one child. We will give the chance to the case exactly five times, each time the chance to become a parent will be the same and will be equal to the chance for survival.
3-1, 5-2, 3-5, 2-5, 5-3
As mentioned earlier, the descendant contains information about the genes of the father and mother. This can be achieved in various ways, but in this case a “crossover” will be used. (| = dividing line)

There are so many ways to transfer information to a descendant, and a cross over is only one of many. The location of the separator can be completely arbitrary, as the father or mother will be to the left of the dash.
And now we will do the same with the descendants:

Now we calculate the survival rates of the offspring.

Source: https://habr.com/ru/post/128704/


All Articles