This article mainly introduces writing genetic algorithms in PHP, which has certain reference value. Now I share it with everyone. Friends in need can refer to it
For a detailed introduction to genetic algorithms, please search by yourself. This article does not completely follow the writing method on the Internet. The author's level is limited, and the intermediate algorithms used are not very clever. Please read the article with criticism.
Genetic algorithm steps used in this article
- ①Initial population
- ②Elimination
- ③Crossover
- ④Mutation
- ⑤Rebuild the population and iterate steps ②-④
- ⑥Drawing and other additional operations
This article introduces a story about scallops by a great god, and the PHP code is roughly written in this way.
There was a group of scallops living without worries on a certain beach. God had nothing to do and sent bob to use genetic algorithm to rectify the group of scallops. After bob came, he made a request to the scallops:
①You can only have 16 scallops, I will kill 2 in each generation, and I will kill whichever 2 shells have the most unlike the Google Chrome icon;
② Among the remaining 14, there are 4 scallops in pairs Give birth to 2 children together, and then make up for 16, and so on;
These scallops are very distressed, but what can they do? Some scallops left, and then there were exactly 16 left. It was these scallops that created Later chrome scallops.
The genetic algorithm simulates the genetic laws of Darwin and Montesquieu to screen, reproduce and mutate the population, so that after many generations, it can be cultivated Target those that comply with the rules.
The first step of the genetic algorithm is to establish an initial population. The initial population can be randomly established, such as the first 16 scallops in the story.
The second step is to establish an elimination mechanism, which is a screening process. For this purpose, we add a fitness attribute to the scallop, that is, how similar the pattern on the scallop's back is to our chrome icon. Here is the fitness calculation standard. is the sum of the differences (absolute values) of the 4 channels of the pixel, and the 4 channels include the transparent channel.
-
①Initial population -
②Elimination -
③Crossover -
④Mutation -
⑤Rebuild the population and iterate steps ②-④ -
⑥Drawing and other additional operations
There was a group of scallops living without worries on a certain beach. God had nothing to do and sent bob to use genetic algorithm to rectify the group of scallops. After bob came, he made a request to the scallops:
①You can only have 16 scallops, I will kill 2 in each generation, and I will kill whichever 2 shells have the most unlike the Google Chrome icon;
These scallops are very distressed, but what can they do? Some scallops left, and then there were exactly 16 left. It was these scallops that created Later chrome scallops.
The first step of the genetic algorithm is to establish an initial population. The initial population can be randomly established, such as the first 16 scallops in the story.
The second step is to establish an elimination mechanism, which is a screening process. For this purpose, we add a fitness attribute to the scallop, that is, how similar the pattern on the scallop's back is to our chrome icon. Here is the fitness calculation standard. is the sum of the differences (absolute values) of the 4 channels of the pixel, and the 4 channels include the transparent channel.
Related recommendations:
The above is the detailed content of Write genetic algorithm in PHP. For more information, please follow other related articles on the PHP Chinese website!