使遗传算法适应20个值而不是2个

问题描述

我目前有一个遗传算法可以正常工作,除了双数组sol1仅包含2个值的事实。我一直在尝试修改代码,以使其在sol1包含20个值时正常工作,但始终会出现超出范围的异常错误。有什么简单的方法可以使此GA使用20个值?任何帮助都会得到应用。

class Example {

    public static void main(String[] args) {
        long startT = System.currentTimeMillis();

        double[] sol1 = {100,100};
        double fitness;

        boolean successful = false;
        double idealfitness;
        while (successful == false) {
            double[][] population1 = firstPopulation1();
            double[][] bestPop1 = findBestPopulation(population1,100);

            double[][] modifiedPopulation = crossover1(bestPop1);
            double[][] bestModified = findBestPopulation(modifiedPopulation,25);

            double bestFit = 100;
            for (double[] doubles : bestModified) {
                fitness = Assess.getTest1(new double[]{doubles[0],doubles[1]});
                if (fitness < 0.07) {
                    successful = true;
                    if (fitness < bestFit) {
                        bestFit = fitness;
                        sol1[0] = doubles[0];
                        sol1[1] = doubles[1];
                    }
                }
            }
        }

private static double[][] firstPopulation1() {
        int row = 100000;
        int col = 2;
        double[][] arr = new double[row][col];

        double minimum = -5;
        double maximum = 5;

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                double rndDouble = minimum + new Random().nextDouble() * (maximum - minimum);
                rndDouble = Math.round(rndDouble * 100) / 100D;
                arr[i][j] = rndDouble;
            }
        }
        return arr;
    }
private static double[][] crossover1(double[][] pop) {
        double[][] modifiedPopulation = new double[100][2];

        ArrayList<Double> myList1 = new ArrayList<>();
        ArrayList<Double> mylist2 = new ArrayList<>();

        for (int i = 0; i < 100; i++) {
            myList1.add(pop[i][0]);
            mylist2.add(pop[i][1]);
        }

        ArrayList<Double> tmp = new ArrayList<>();
        int point1 = new Random().nextInt(100);
        int point2 = new Random().nextInt(100);

        if (point2 < point1) {
            int tmpInt = point1;
            point1 = point2;
            point2 = tmpInt;
        }

        for (int i = point1; i < point2; i++) {
            tmp.add(myList1.get(i));
        }
        int tmpCounter = 0;
        for (int i = point1; i < point2; i++) {
            myList1.set(i,mylist2.get(i));
            mylist2.set(i,tmp.get(tmpCounter));
            tmpCounter++;
        }
        for (int i = 0; i < 100; i++) {
            modifiedPopulation[i][0] = myList1.get(i);
            modifiedPopulation[i][1] = mylist2.get(i);
        }

        return modifiedPopulation;
    }
private static double[][] findBestPopulation(double[][] population1,int max) {
        double[][] bestPopulation = new double[max][2];
        int counter = 0;
        for (double[] doubles : population1) {
            double fitness = Assess.getTest1(new double[]{doubles[0],doubles[1]});
            if (fitness < 3 && counter < max) {
                bestPopulation[counter][0] = doubles[0];
                bestPopulation[counter][1] = doubles[1];
                counter = counter + 1;
            }
        }
        return bestPopulation;
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)