What is a genetic algorithm in layman's terms?


First of all, there is a kind of small things, they all live on the prairie, they can survive without being eaten.

This thing usually looks like this:

It consists of several elements:

  • Node, let's call it a leg for the time being , that is, the darker the circle in the picture above, the greater the friction with the ground.
  • Muscal, muscle , thick line connecting the circles , darker and thicker muscles indicate more strength, and each muscle has a different original length. The stronger the muscle, the more able he is to pull the two Nodes that are connected
  • And the most important image below: the clock . That is, these muscles have time to lengthen and time to contract. During the stretch, the muscles always want to make the distance between the legs farther, and during the contraction time the muscles want to make the distance between the legs closer.

Is it complicated?

In fact, you can imagine: several iron balls are connected together by several rubber bands, the rubber bands are twitching regularly, and then the thing will run forward with the twitching.

So then here comes our goal: what kind of thing like this ( how many balls, how many rubber bands, how strong ) can run the fastest?

That's when the genetic algorithm comes into play.

The first step, the first seed players

The genetic algorithm must first have a parent. That is, the initial little things like this.

Generally speaking, if we know some knowledge, such as roughly knowing what kind of things can run fast, we can pick some out as the male parent.

But now we don't even know how these things run, how can we know which one runs faster?

So the crudest thing is: 1000 of them!

100 randomly generated little things. There are three legs, there are four, five, six and seven, and there are more or less muscles. Then we see how they run.

First player:



How should I put it, in a squirm, the No. 1 player can run a distance of almost 1 meter in 15 seconds. Very good performance!

Do you think this is not fast enough?

Take a look at player number 2, for example:



"Please don't lie on the ground"

and other players



In general, because all the players were randomly generated, many had to stay in place, and half of the players chose to go backwards instead of forwards.

Genetic Algorithm Step Two: Survival

"Survival of the Fittest"

The male parent of the first generation, sorted by distance run within 15 seconds. Those who run slowly will be eliminated.

1000 small things sorted according to the speed of running


However, not necessarily all slow runners will be eliminated by nature. It's just that the faster one is more likely to survive, and the slower one is more likely to die .

Nature is so amazing that it will allow some lucky weaklings to survive. Maybe it will show different characteristics in the next generation.

Half of the 1,000 of the first generation will be eliminated, and the rest will be eligible to serve as the father of the next generation, leaving genes behind. The black representation in the picture below is eliminated.

The third step of the genetic algorithm: generating offspring

Just like when two blackheads occur, there is a high probability of black hair, and the probability of having a child born to both parents is very high. But there will also be two gifted children born to unwise parents.

The same is true in the genetic algorithm. The offspring produced by the combination of two fathers (don't care about these details) may have different "legs":

It's also quite possible that the "muscles" will not be as strong:

But there are also relatively rare variants :

The legs are not the same. Of course, it is all the power of nature, and it has little to do with who lives next door.

The significance of mutation in genetic algorithm is: even if the father's genes do not contain the best genes (for example, the father has 5 legs, but the best solution is actually 3), it may be generated through mutation. . If the result of the mutation is good, it will stay and become the new parent. This way the next generation of the population contains the best genes.

So we have the next generation: the children of the previous generation with 500 samples --> 1000 new samples.

The last step of the genetic algorithm: iteration

We already have the complete process,

  1. have a group
  2. Groups are randomly eliminated based on performance (objective function)
  3. crossover to create new groups
  4. group eliminated again
  5. Repeat 3 and 4 continuously until you find a sample that meets the conditions (meeting the requirements), or the population can no longer evolve significantly.

Let's take a look at such a genetic algorithm, what kind of effect can be achieved in the end

By the 10th generation, the fastest one can run 3 meters faster in 15 seconds. It runs like this



This little thing has 5 legs and can already squirm forward stably

By the 30th generation:



It feels like it's about to fly. Still 5 legs.

In the 30s, the fastest runner could already run to 6 meters in 15 seconds, and the median (500th in the 1,000-person marathon) could also run to 3 meters. The whole group is making rapid progress

This graph shows the fastest (top black line) and population median (red line) performance for each generation in evolution.

Then by the '62 generation, a guy with 4 legs became the new champion:



It appears that evolutionary forces are at work. The 4-legged group evolved a structure that was very favorable for running: a black leg in the back, a white leg in the front, and two similar legs in the middle.

And in the whole group, the creatures with 4 legs and 6 muscles also occupied the majority, and most of the other 3 legs and 5 legs were gradually eliminated.

result

At generation 300, the growth rate becomes very slow and the simulation stops.

Nature is amazing~ Did you learn it?


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324926359&siteId=291194637