Home > Backend Development > PHP Tutorial > Write genetic algorithm in PHP

Write genetic algorithm in PHP

不言
Release: 2023-03-24 22:06:01
Original
2311 people have browsed it

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

This article attempts to write genetic algorithms in PHP language

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

Section 1 Story Prelude and Algorithm Summary

1. Story Prelude

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.

2. Algorithm Summary

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.

This article attempts to write a genetic algorithm in PHP language

For a detailed introduction to the genetic algorithm, 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 algorithm used is also None of them are 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

Section 1 Story Prelude and Algorithm Summary

1. Story Prelude

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.

2. Algorithm Summary

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.

Related recommendations:

PHP implements KMP algorithm

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template