As a programmer must master the algorithm of genetic algorithm

Table of contents

I. Introduction

1.1 Purpose

1.2 Significance

2. Introduction to Genetic Algorithms

2.1 Basic idea of ​​genetic algorithm

2.2 The main difference between genetic algorithm and other algorithms

2.3 Java-based Genetic Algorithm Design Ideas

Third, the specific implementation of the genetic algorithm

3.1 Diagram and description of system function modules

3.2 Code and description

3.2.1 Initialization

3.2.2 Select operation

3.2.3 Cross operation

3.2.4 Mutation operation

3.2.5 Main function

4. System testing

4.1 Test environment

4.2 Test steps 

4.3 System running results

5. Summary of Genetic Algorithms


I. Introduction

        A programmer may encounter various algorithms in his life, but there are always a few algorithms that a programmer will definitely encounter and need to master with a high probability. Today, let’s talk about these very important “must catch!” algorithms~ , such as genetic algorithms

1.1 Purpose

The genetic algorithm is mainly implemented in the java language to find the maximum value of x12 + x22. Mainly, the previous parameter range is relatively fixed, so I plan to make a custom setting parameter range, which can independently set the number of groups in each generation, and also set the number of iterations.

1.2 Significance

It is possible to explore the relationship between the parameter range and the number of iterations and the number of groups that affect the maximum value of the genetic algorithm.

   

2. Introduction to Genetic Algorithms

2.1 Basic idea of ​​genetic algorithm

Genetic Algorithms (GA) is a natural selection that simulates Darwin's biological evolution theory. The computational model of the biological evolution process of the genetic mechanism is a method to search for the optimal solution by simulating the natural evolution process.

A population consists of a certain number of individuals encoded by genes.

A genetic algorithm starts with a population that represents the set of possible potential solutions to a problem.

Human cells have 23 pairs of chromosomes (22 pairs of autosomes and a pair of sex chromosomes), that is, a total of 46 chromatids per cell.

Chromosomes serve as the main carriers of genetic material, i.e. collections of multiple genes

Chromosomes whose internal expression (ie, genotype) is a combination of genes that determine the external appearance of an individual's shape

For example, the characteristic of black hair is determined by a certain combination of genes in the chromosomes that control this characteristic

After the generation of the first-generation population, according to the principles of survival of the fittest and survival of the fittest, generation by generation (generation) evolution produces better and better approximate solutions.

In each generation, individuals are selected according to their fitness in the problem domain, and combined crossover and mutation are performed with the help of genetic operators of natural genetics to generate a population representing a new solution set.

This process will lead to the natural evolution of the population. The offspring population is more adaptable to the environment than the previous generation. The optimal individual in the last generation population can be used as an approximate optimal solution to the problem after decoding.       

2.2 The main difference between genetic algorithm and other algorithms

  1. Self-organizing, adaptive and self-learning (intelligence)
  2. When the genetic algorithm is applied to solve the problem, after the coding scheme, fitness function and genetic operator are determined, the algorithm will use the information obtained in the evolution process to organize the search by itself. Since the selection strategy based on nature is "survival of the fittest and elimination of the unsuitable", individuals with high fitness have higher survival probability. Individuals with high fitness have a genetic structure that is more adaptable to the environment, and through genetic manipulation such as gene recombination and gene mutation, they may produce offspring that are more adaptable to the environment.
  3. The essential parallelism of the genetic algorithm, the genetic algorithm searches for a population number of points in parallel, rather than a single point. Its parallelism manifests itself in two ways:

One is that the genetic algorithm is inherently parallel, that is, the genetic algorithm itself is very suitable for large-scale parallelism. The simplest parallel method is to let hundreds or even thousands of computers carry out evolutionary calculations of independent populations, without any communication during the operation, and only communicate and compare at the end of the calculation to select the best individual.

The second is the inherent parallelism of the genetic algorithm. Since the genetic algorithm uses the population to organize the search, it can search multiple regions in the solution space at the same time and exchange information with each other.

Using this search method, although only calculations proportional to the population size n are performed each time. But in essence, about O(n3) effective searches have been carried out, which enables the genetic algorithm to obtain greater benefits with less calculation.

Genetic algorithms do not require derivatives or other auxiliary knowledge, but only the objective function and the corresponding fitness function that affect the search direction.

  1. Genetic algorithms emphasize probabilistic transition rules rather than deterministic transition rules.
  2. Genetic algorithms can be applied more directly.
  3. Genetic algorithms can generate many potential solutions to a given problem, and the final choice can be determined by the user
  4. In some special cases, such as a multi-objective optimization problem where more than one solution exists, there is a set of optimal solutions.

2.3 Java-based Genetic Algorithm Design Ideas

First of all, it should be divided into two categories. One category contains various variables required by the genetic algorithm, for example, two parameters x1 and x2, number of iterations, group array, number of selections, the maximum solution obtained, group fitness and Its proportion, the binary string of the parameter group, etc., are all variables that need to be used when implementing the genetic algorithm, so we should release these variables separately into a class, and when needed, they can be called directly .

The second class is the implementation class of the genetic algorithm, which should contain various methods required by the entire genetic algorithm. For example, the parameter initialization function is mainly used for input. To determine the value range of the parameter, the number of iterations and the number of groups, we need to customize the input. It also includes the calculation of the fitness function, which calculates the corresponding How much is the fitness, and there is a selection operation, and the selection is made according to the fitness of each individual. Here, the roulette method can be used to make the selection. It should also include a crossover algorithm to mate the selected individuals, here it should also be random pairwise mating, which can better reflect Darwin's theory of natural selection, and the crossover point should also be randomly generated. Finally, the mutation algorithm is essential. The mutation point is randomly selected for mutation. Of course, there should be no mutation here. Assuming no mutation, the mutation point should be -1. Other auxiliary methods should also be included, such as converting decimal to binary functions, converting binary to decimal functions, selecting rearrangement functions that need to be rearranged after the operation is completed, printing groups and their binary display functions, and judging whether the optimal solution function is found .

Third, the specific implementation of the genetic algorithm

3.1 Diagram and description of system function modules

Figure 1 below is the main flow chart of the algorithm.

Figure 1 Algorithm flow chart

From the algorithm flow chart, we should be able to know that the parameters are initialized first. After the initialization parameters are completed, the group is initialized according to the input parameters, and then the selection operation is performed to select the group, and then the crossover operation is performed to randomly generate intersection points. Pairwise random assignment was performed for cross-integration. Finally, the mutation operation is performed to determine the offspring individuals. If the optimal solution is not found and the number of iterations is within the set range, re-selection, crossover, and mutation operations.

3.2 Code and description

Next, each process of the system flow chart is analyzed in detail.

3.2.1 Initialization

The initialization is divided into two initialization parameters. The following code 2 intuitively shows that the initialization parameters include the number of groups in each generation, the value range of the two parameters, and the number of iterations.

    // 初始化参数最大值,群体
    public static void xScan(){
        // 输入参数范围
        System.out.print("请输入每代的群体数量:");
        xSize = scan.nextInt();
        System.out.print("请输入X1的最大值:");
        maxX1 = scan.nextInt();
        System.out.print("请输入X2的最大值:");
        maxX2 = scan.nextInt();
        System.out.print("请输入迭代次数:");
        iterations = scan.nextInt();
        // 初始化群体
        xx = new int[xSize][2];
        strXX = new String[xSize];
    }

Code 2 parameter initialization

Code 3 is group initialization, group initialization. Here I choose decimal initialization, and then calculate the number of digits in the binary string of each parameter. This is mainly for the convenience of my console output style, and has nothing to do with the algorithm. Then convert the population into a binary string.

    // 群体初始化
    public static void initXX(){
        System.out.println("-----随机初始化群体----");
        for (int i=0;i<xSize;i++){
            xx[i][0] = (int) (Math.random()*maxX1);
            xx[i][1] = (int) (Math.random()*maxX2);
        }
        // 最大值的二进制字符串位数
        countStrXSize();
        // 群体二进制
        strXXToBin();
        // 打印群体
        printXX();
    }

Code 3 group initialization

3.2.2 Select operation

Code 4 is the main code of the selection operation. To perform the selection operation, the individual fitness calculation and its proportion are first performed. Before each selection, the array of the number of times the individual is selected must be reset. If it is not recreated here, it will be Keep the last value, causing data disorder. Here I choose according to the roulette method. For example, the first individual accounts for 30%, and the second individual accounts for 70%. Then a random number is generated. When the random number is less than or equal to 0.3, the first individual is selected. Select, if the range of the random number is greater than 0.3, select the second individual. By analogy, the selection operation based on the roulette wheel method is completed.

    // 选择运算
    public static void selectXX(){
        System.out.println("----------进行选择运算----------");
        // 先进行计算适应度及其占比情况
        countSuitNum();
        // 每次选择都要重置选择次数数组
        selectTimes = new int[xSize];
        // 轮盘赌法
        for (int i=0;i<xSize;i++){
            double per = Math.random();
            for (int j=0;j<xSize;j++){
                if(per<=xxSuitPoss[j]){
                    selectTimes[j] += 1;
                    break;
                }
            }
        }
        for (int i=0;i<selectTimes.length;i++){
            xxSelectMap.put(i,selectTimes[i]);
            System.out.println("个体编号:"+(i+1)+", ("+xx[i][0]+", "+xx[i][1]+")"+"\t适应度:"+xxSuitList.get(i)
                    +"\t占总数的百分比:"+String.format("%.2f",xxSuitMap.get(i).doubleValue())+"\t选择次数: "+xxSelectMap.get(i));
        }
        // 根据选择结果重新布局
        refreshXX();
        // 选择完更新二进制群体得十进制数
        refreshXXTen();
    }

Code 4 selection operation

3.2.3 Cross operation

The main logic of the crossover algorithm is to randomly generate a non-repeating array from the index of the individual array, and then mate with the head and tail of the non-repeating array to achieve a pseudo-random pairwise mating idea. Then re-assign the mated individuals back to the group array. At this time, the group array has been disrupted, so we need to refresh the decimal group array.

    // 交叉算法
    public static void cross(){
        System.out.println("---------进行交叉运算---------");
        int ranArr[] = new int[xSize];
        int arr[] = new int[xSize];
        for(int i=0;i<xSize;i++){
            arr[i] = i;
        }
        Random random = new Random();
        for (int i=0;i<xSize;i++){
            // 得到一个位置
            int j = random.nextInt(xSize-i);
            // 得到位置的数值
            ranArr[i] = arr[j];
            // 将最后一个未用的数字放到这里
            arr[j] = arr[xSize-1-i];
        }
        for(int i=0;i<xSize/2;i++){
            // 随机确定交叉点
            int cross_x = (int) (Math.random()*(x1BinStrSize+x2BinStrSize));
            System.out.print("配对情况:配对前:"+(ranArr[i]+1)+" = "+strXX[ranArr[i]]+" 和 "+(ranArr[xSize-1-i]+1)
                    +" = " + strXX[ranArr[xSize-1-i]]+"进行配对,交叉点:"+(cross_x+1)+"\t");
            strXX[ranArr[i]] = strXX[ranArr[i]].replace("|","");
            strXX[ranArr[xSize-1-i]] = strXX[ranArr[xSize-1-i]].replace("|","");
            String str1 = strXX[ranArr[i]];
            String str2 = strXX[ranArr[xSize-1-i]];
            String str1_1,str1_2;
            str1_1 = str1.substring(0,cross_x)+str2.substring(cross_x);
            str1_2 = str2.substring(0,cross_x)+str1.substring(cross_x);
            str1_1 = str1_1.substring(0,x1BinStrSize)+"|"+str1_1.substring(x1BinStrSize);
            str1_2 = str1_2.substring(0,x1BinStrSize)+"|"+str1_2.substring(x1BinStrSize);
            System.out.println("配对后:"+(ranArr[i]+1)+" = "+str1_1+", "+(ranArr[xSize-1-i]+1)+" = "+str1_2);
            strXX[ranArr[i]] = str1_1;
            strXX[ranArr[xSize-1-i]] = str1_2;
        }
        // 刷新十进制群体
        refreshXXTen();
        System.out.println("交叉后个体");
        printXX();
    }

Code 5 Intersection Algorithm

3.2.4 Mutation operation

Code 6 mainly describes the logic code of the mutation operation. First, the cycle is mutated for each individual. As for the mutation, the book will choose to mutate. After carefully studying Darwin’s theory of natural selection, I think whether the mutation should also be It is random and should not mutate every offspring. Therefore, there is a probability of 0.5 that no mutation occurs, and the mutation point should be set to -1. The logic here is to randomly generate a random number whose range is twice the length of the binary display individual string, if the random number is smaller than the length of the individual binary string, then mutate at the point of the random number, That is, the inversion operation. 0 becomes 1, 1 becomes 0. If the value of the random number is greater than the length of the individual binary string, no mutation is performed, and the mutation point is set to -1. Then reassign the obtained string to the population binary string array. Because the group binary string array changes, here we should also update the decimal array. And after the mutation operation is over, it means that the generation of mutation algorithm is over, and the algebra +1. Figure 6 below is the logic code of the mutation algorithm

    // 变异算法
    public static void variation(){
        System.out.println("---------进行变异运算---------");
        for (int i=0;i<xSize;i++){
            // 随机选择变异点,当大于字符串范围时不进行变异
            int var = (int) (Math.random()*(2*(x1BinStrSize+x2BinStrSize-1)))+1;
            System.out.print("个体编号:"+(i+1)+"\t"+strXX[i]);
            strXX[i] = strXX[i].replace("|","");
            char [] strX = strXX[i].toCharArray();
            if(var < strX.length) {
                if (strX[var - 1] == '0') {
                    strX[var - 1] = '1';
                } else {
                    strX[var - 1] = '0';
                }
            }
            else{
                var = -1;
            }
            strXX[i] = new String(strX);
            strXX[i] = strXX[i].substring(0,x1BinStrSize)+"|"+strXX[i].substring(x1BinStrSize);
            System.out.println("\t变异点:"+var+"\t变异结果:"+strXX[i]);
        }
        times++;
        // 更新群体十进制数组
        refreshXXTen();
        System.out.println("\n---------------------第"+times+"代群体情况---------------------");
        printXX();
    }

Code 6 mutation algorithm

3.2.5 Main function

According to the above calculation and the system flow chart, we should be able to understand the entire calculation process of the genetic algorithm, first initialize the parameters, then initialize the group, then enter the selection operation, crossover operation, mutation operation, and finally determine whether the optimal solution is found and Whether it is greater than the set iteration range, if not, then re-enter the selection operation, and so on, the logic code of the main function is implemented as follows d code 7

    public static void main(String[] args) {
        // 初始化参数
        xScan();
        System.out.println("---------------------第"+times+"代群体情况---------------------");
        // 初始化群体
        initXX();
        do {
            // 选择运算
            selectXX();
            // 交叉运算
            cross();
            // 变异运算
            variation();
        }
        while (!checkResult() && times<iterations);
    }

Code 7 main function

4. System testing

4.1 Test environment

Dell Inspiron 5509 Win10 computer, IntelliJ IDEA 2021.1 x64 compiler.

4.2 Test steps 

First of all, to determine the test case, the test case should include the number of groups, the range of parameter values, and the number of iterations. The test case table is as follows:

Table 1 Test Cases

name

Number of groups

maximum value of x1

maximum value of x2

iterations

should get the optimal solution

test case 1

10

127

63

100

20098

test case 2

10

127

63

200

20098

Test case 3

20

127

63

100

20098

Test case 4

50

127

63

100

20098

Test case 5

10

127

127

100

32258

Test case 6

10

63

127

100

20098

Test case 7

10

127

127

100

32258

Test case 8

10

127

63

100

20098

Test cases 1 and 2 are used to test the effect of the number of iterations on obtaining the optimal solution

Test cases 3 and 4 are used to test the effect of population on obtaining the optimal solution

Test cases 5 and 6 are used to test the influence of parameter x1 on obtaining the optimal solution

Test cases 7 and 8 are used to test the influence of parameter x2 on obtaining the optimal solution

4.3 System running results

The test results should include the optimal solution obtained, the actual optimal solution, whether the optimal solution was obtained in advance, and the algebra used to obtain the optimal solution

Table 2 Test results

Test case result

The optimal solution obtained

actual optimal solution

Whether to obtain the optimal solution in advance

Algebra to get the optimal solution

test case 1

19729

20098

no

100

test case 2

19973

20098

no

200

Test case 3

20098

20098

yes

30

Test case 4

20098

20098

yes

5

Test case 5

32005

32258

no

100

Test case 6

20098

20098

yes

31

Test case 7

32005

32258

no

100

Test case 8

20098

20098

yes

51

According to the running results of test cases 1 and 2, we can know that when the number of iterations is large, the optimal solution obtained will be closer to the actual optimal solution; according to the running results of test cases 3 and 4, we can know that when the number of groups is larger When the parameter range is relatively small, the optimal solution can be obtained earlier; from the running results of test cases 5 and 6 and test cases 7 and 8, we can know that it is easier to obtain the optimal solution when the parameter range is relatively small.

5. Summary of Genetic Algorithms

Genetic algorithm is an optimization algorithm based on the principle of biological evolution, which seeks the optimal solution by simulating the mechanisms of inheritance, variation and selection in the process of biological evolution. It is widely used in problem solving in various fields, such as engineering design, combinatorial optimization, machine learning, etc.

Application scenarios of genetic algorithms include but are not limited to:

  1. Optimization problems: Genetic algorithms can effectively solve complex optimization problems, such as traveling salesman problem, knapsack problem, etc. It is able to perform a global search in the search space to find an approximate optimal solution.

  2. Design issues: Genetic algorithms can be used for design optimization, such as circuit design, structural design, etc. By encoding and evolving the design parameters, a design scheme that meets the requirements can be obtained.

  3. Machine learning: Genetic algorithm can be used as an optimization method for parameter optimization of machine learning algorithms, such as weight optimization of neural networks, parameter selection of support vector machines, etc.

  4. Scheduling problems: Genetic algorithms can be used to solve scheduling problems, such as task scheduling, vehicle path planning, etc. By encoding and evolving the scheduling scheme, the optimal scheduling strategy can be obtained.

It is very important for programmers to master the types and knowledge points of genetic algorithms. The following are some key knowledge points that programmers need to master:

  1. Basic principles of genetic algorithms: understand the basic concepts and operating principles of genetic algorithms, including operations such as encoding methods, fitness functions, selection, crossover, and mutation.

  2. Coding method: understand how to map the solution space of the problem to the coding space of the genetic algorithm, and choose an appropriate coding method to model the problem.

  3. Fitness function: Design a fitness function to evaluate the degree of individual pros and cons to guide the search process of the genetic algorithm.

  4. Selection Operators: Learn about the different selection operators such as Roulette Selection, Tournament Selection, etc., and the pros and cons between them.

  5. Crossover operator: learn how to generate new individuals through crossover operations to increase the diversity of the population and the coverage of the search space.

  6. Mutation operator: Learn how to introduce new genetic information through mutation operations to avoid falling into local optimal solutions.

  7. Parameter setting and tuning: Master how to set the parameters of the genetic algorithm, and find the appropriate parameter values ​​through experiments and tuning to improve the performance of the algorithm.

  8. Parallel and Distributed Genetic Algorithms: Learn how to use parallel and distributed computing methods to accelerate the execution efficiency of genetic algorithms.

Programmers are encouraged to actively learn and delve into the field of genetic algorithms. As a powerful optimization tool, genetic algorithm plays an important role in practical problems. Through learning and research, programmers can apply genetic algorithms to their own work to improve the efficiency and quality of problem solving. In addition, in-depth study of genetic algorithm can also explore its improvement and expansion, and provide new ideas and methods for solving more complex problems.

Guess you like

Origin blog.csdn.net/qq_53317005/article/details/131663949