我如何在不影响需要保持相同且不引入重复的情况下随机将行随机突变在1D INT数组中?

发布于 2025-01-31 10:00:14 字数 2602 浏览 5 评论 0原文

我正在编写一种遗传算法来解决Sudoku难题,并且进入了突变产生的潜在解决方案的最后阶段,但是对代码有一些麻烦。

我要实现的结果是:

  1. 通过每行循环循环15个染色体的种群,
  2. 突变
  3. 突变的机会为10%,导致2个随机值(1-9)切换位置,将
  4. 突变的行插入后。它属于数组中的位置,

我还需要考虑某些空间被用户定义为要解决的起始板,不应交换。

到目前为止,在我的解决方案中,我还有另一个称为非静态值的1D数组,该数组保持允许交换哪个值的位置。与“突变率”变量相比,我还具有一种方法,可以使用随机数创建突变的机会10%。 当将所有这些放在一起时,问题就会出现

,因为它很快就变成了复杂的嵌套回路,并且由于染色体被表示为1D阵列,因此行彼此之间并没有区别。

当前,该代码由3个嵌套循环组成,并具有跟踪当前节点的计数器,该节点最多为80(第81个节点),但是由于无论出于何种原因,该计数器也会产生界限异常。

代码:

        public static void mutation(Population population){
    //    Randomise mutation
    //        Set mutation rate
            // 10 percent chance to
            double mutationRate = 0.1;
            int[] row =  new int[9];
    
            //for each chromosome in the population
            for(int i=0; i<15; i++){
    
                //variable for current node
                int currentNode = 0;
                //Placeholder chromosome to grab non-static values from
                Chromosome placeholder = new Chromosome();
                //current chromosome
                Chromosome currChromosome = population.populationMap.get(i);
                placeholder.generateChromosome(Driver.board);
                //putting non-static values into their own list for reference later
                ArrayList<Integer> nonStaticValues = placeholder.getNonStaticValues();
    
    
                // for each row in each chromosome
                for (int j=0; j<9; j++){
                    //determine if row will be mutated
                    //Generate a random probability to determine whether
                    double probability = Math.random();
    
                    for (int k = 0; k < 9; k++){
                        if(probability <= mutationRate ){
                            placeholder.genes[currentNode] = currChromosome.genes[currentNode];
    
    
                        }
                        
                        System.out.println("Current Node: " + currentNode);
                        System.out.println("Current Node Data: "+ currChromosome.genes[currentNode]);
                        currentNode++;
                    }
                }
            }
        }

预期结果的示例

原始行从启动板的CSV文件:

[0,3,0,0,2,7,8,0,0]

生成其余的行值后,

[9, 3, 5, 1, 2, 7, 8, 6, 4]

我想对此介绍更多随机性,但是我无法更改从CSV定义的启动值,因此可更改的值位置位置存储在称为非静态值的数组中。

示例结果(4和9个交换位置):

[4, 3, 5, 1, 2, 7, 8, 6, 9]

预先感谢您的任何帮助,希望我的问题得到正确编写,并且代码符号对您有所帮助。

I am writing a genetic algorithm to solve a sudoku puzzle and I'm onto the final stage of mutating the generated potential solutions, but am having some trouble with the code.

The result I want to achieve is:

  1. The population of 15 chromosomes is looped through
  2. Each row is looped through with a 10% chance to mutate
  3. Mutation causes 2 random values in the row (1-9) to switch places
  4. The mutated row is inserted back into the place in the array it belongs in

I also need to factor in that some of the spaces are defined by the user as the starting board to solve and should not be swapped.

So far in my solution I have another 1d array called non-static values, which holds the positions of which values are allowed to be swapped. I also have a method for creating a 10% chance for the mutation to happen using a random number compared to a "mutation rate" variable.
it

The problem is arising when it comes to putting this all together, as it quickly becomes a complicated mess of nested loops, and since rows are not distinct from each other due to the chromosome being represented as a 1d array.

Currently the code consists of 3 nested loops, with a counter to track the current node up to 80(the 81st node), but this counter is also producing an out of bounds exception as it is reaching 81 for whatever reason.

Code:

        public static void mutation(Population population){
    //    Randomise mutation
    //        Set mutation rate
            // 10 percent chance to
            double mutationRate = 0.1;
            int[] row =  new int[9];
    
            //for each chromosome in the population
            for(int i=0; i<15; i++){
    
                //variable for current node
                int currentNode = 0;
                //Placeholder chromosome to grab non-static values from
                Chromosome placeholder = new Chromosome();
                //current chromosome
                Chromosome currChromosome = population.populationMap.get(i);
                placeholder.generateChromosome(Driver.board);
                //putting non-static values into their own list for reference later
                ArrayList<Integer> nonStaticValues = placeholder.getNonStaticValues();
    
    
                // for each row in each chromosome
                for (int j=0; j<9; j++){
                    //determine if row will be mutated
                    //Generate a random probability to determine whether
                    double probability = Math.random();
    
                    for (int k = 0; k < 9; k++){
                        if(probability <= mutationRate ){
                            placeholder.genes[currentNode] = currChromosome.genes[currentNode];
    
    
                        }
                        
                        System.out.println("Current Node: " + currentNode);
                        System.out.println("Current Node Data: "+ currChromosome.genes[currentNode]);
                        currentNode++;
                    }
                }
            }
        }

Example of expected results

Original Row from csv file of starting board :

[0,3,0,0,2,7,8,0,0]

After generating the rest of the rows values

[9, 3, 5, 1, 2, 7, 8, 6, 4]

I want to introduce some more randomness to this but I cannot change the values which start defined from the csv, so the changeable values positions are stored in an array called non-static values.

Example outcome (4 and 9 swap places):

[4, 3, 5, 1, 2, 7, 8, 6, 9]

Thank you in advance for any help, I hope my question is written properly and code notation helps.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文