As shown in the figure above (left), when the genetic algorithm is used, an individual consists of multiple chromosomes, and each chromosome consists of multiple genes. The figure above (right) shows how chromosomes are divided and combined. The concept of natural selectionThe process of natural selection starts with selecting the individuals in the population that are best adapted to the environment. The offspring inherit the characteristics of their parents, and these characteristics will be added to the next generation. If the parents have better fitness, then their offspring will be more likely to survive. This process of natural selection is carried out iteratively, and eventually, we will get a generation consisting of the individuals that are best adapted to the environment. This concept can be applied to search problems. We consider many solutions to a problem and search for the best one. The genetic algorithm consists of the following five steps:
initializationThe process starts with a set of individuals in the population, each of which is a candidate solution to the problem to be solved. Individuals are characterized by a set of parameters (variables) called genes, which are strung together to form chromosomes (the solution to the problem). In genetic algorithms, the genome of a single individual is presented as a string, which can usually be encoded using binary (a string of 1s and 0s), i.e., a binary string represents a chromosome string. Therefore, we can say that we encode the characteristics of the gene string or candidate solution in the chromosome. Populations, chromosomes, and genes Individual evaluation (calculating fitness function)Individual evaluation uses the fitness function to evaluate the individual's fitness for the environment (the ability to compete with other individuals). Each individual has a fitness score, and the likelihood of the individual being selected for reproduction depends on its fitness score. The larger the fitness function value, the higher the quality of the solution. The fitness function is the driving force of the evolution of the genetic algorithm and the only criterion for natural selection. Its design should be combined with the requirements of the problem itself. Select OperationThe purpose of the selection operation is to select the individuals with the best fitness and have them pass on their genes to the next generation. Based on their fitness scores, we select multiple pairs of better individuals (parents). Individuals with high fitness are more likely to be selected for reproduction, that is, to pass on the genes of better parents to the next generation. Crossover operationCrossover is the most important phase in the genetic algorithm. For each pair of parents, there is a randomly selected crossover point for the genes. For example, the intersection point in the following figure is 3. The offspring are produced by the exchange of genes between the parents before the crossover point. Genes are exchanged between parents, and the resulting new offspring are then added to the population. Mutation OperationIn some of the new offspring formed, some of their genes may be affected by low-probability mutation factors. This means that some bits in the binary bit string may flip. Before and after mutation operation Mutation operations can be used to maintain diversity within the population and prevent premature convergence. termination The algorithm terminates when the population converges (no offspring are produced within the population that are significantly different from the previous generation). In other words, the genetic algorithm provides a set of solutions to the problem. The size of the population is constant. When a new generation is formed, the individuals with the worst fitness die to make room for the next generation. The sequence of these stages is repeated over and over again to produce a new generation that is better than the previous one. Pseudocode for this iterative process: START Example implementation in Java Below is a sample implementation of the genetic algorithm in Java, which can be debugged and modified at will. Given a set of five genes, each of which can hold a binary value of 0 or 1. The fitness here is the number of 1s in the genome. If there are five 1s in the genome, the individual's fitness reaches the maximum value. If there are no 1s in the genome, the individual's fitness reaches the minimum value. The genetic algorithm hopes to optimize the fitness and provide a population composed of individuals with the best fitness. Note: In this example, after the crossover and mutation operations, the individual with the best fitness is replaced by a new offspring with the best fitness. import java.util.Random; |
<<: Key technologies for implementing microservice architecture
>>: In-depth understanding of the Android Instant Run operating mechanism
There are many debates online about "raising...
The school season is about to begin, and the camp...
Organizing activities is a good choice. It can in...
[[140264]] Preface Before I knew it, I have been ...
There are too many "have to" in life I ...
Today's scientific experimental devices are c...
...
Doing SEO is about taking advantage. If you do SE...
How to become a favorite concubine in palace dram...
1. What is ASO ? It mainly uses machines and a se...
Recently, an asteroid numbered 2022 AP7 was notic...
China Unicom recently released its latest user da...
Is it profitable to unblock WeChat? Many people k...
gossip Tomatoes are a common food in our daily li...
Some basic processes and functions of 400 calls; ...