修复数组超出范围异常新数组异常

时间:2017-10-19 19:02:35

标签: java arrays algorithm

当我尝试运行我的代码时,它告诉我在Caluclate Fitness中有多个Array out of bounds异常,并选择任何想法​​来帮助我感谢。它说错误在第67行我相信其中一个错误和37个我的主类(遗传算法)。 Individual类似乎很好我期望问题在于我如何定义我的函数但我不知道修复这个错误。我已经主演了错误,它是选择数组。在选择行中它表示数组超出界限50我猜这意味着它认为数组中的i未声明但我已声明它

package genetic.algorithm;

import static genetic.algorithm.GeneticAlgorithm.Selection;
import static genetic.algorithm.GeneticAlgorithm.n_population;
import static genetic.algorithm.Individual.N;
import static genetic.algorithm.Individual.fitness;
import static genetic.algorithm.Individual.gene;
import java.util.Random;

public class GeneticAlgorithm {

    static int P = 50;
    static int i = 0;
    static int j = 0;
    static Random rand = new Random();
    static Individual[] n_population = new Individual[P];
    static Individual[] offspring = new Individual[P];

    public static Individual[] population() {
        int rn = rand.nextInt();

        for (i = 0; i < P; i++) {
            for (j = 0; j < N; j++) {
                n_population[i].gene[j] = rn % 2;
            }
            n_population[i].fitness = 0;
        }
        return n_population;
    }

    public static int CalculateFitness(int[] gene) {
        gene = new int[N];
        fitness = 0;
        for (j = 0; j < N; ) {
            for (i = 0; i < P; ) {
                if (n_population[i].gene[j] != 0) n_population[i].fitness++;
            }
        }
        return fitness;
    }

    public static Individual[] Selection(Individual[] n_population) {
        n_population = new Individual[P];
        offspring = new Individual[P];
        fitness = 0;
        int rands = rand.nextInt();
        int parent1 = rands % P;
        int parent2 = rands % P;
        for (i = 0; i < P; i++) {
           **if (n_population[parent2].fitness >= n_population[parent1].fitness)**
                offspring[i] = n_population[parent1];
            else {
                offspring[i] = n_population[parent2];**
            }
        }
        return offspring;
    }

    public static Individual[] Crossover(Individual[] n_population) {
        int rands = rand.nextInt();
        n_population = new Individual[P];
        offspring = new Individual[P];
        gene = new int[N];
        int[] temp_gene1 = new int[N];
        int[] temp_gene2 = new int[N];
        int parent1 = rands % P;
        int parent2 = rands % P;
        int crossoverPoint = rands % N;

        for (j = 0; j < N; j++) {
            if (j > crossoverPoint) {
                temp_gene1[j] = n_population[parent2].gene[j];
                temp_gene2[j] = n_population[parent1].gene[j];
            } else {
               * temp_gene1[j] = n_population[parent1].gene[j]*;
                temp_gene2[j] = n_population[parent2].gene[j];
            }
        }

        if (CalculateFitness(temp_gene1) >= CalculateFitness(temp_gene2))
            offspring[i] = n_population[parent1];
        else {
            offspring[i] = n_population[parent2];
        }
        return offspring;
    }

    public static Individual[] Mutation(Individual[] n_population) {
        double x = 0.1;
        double y = 0.107;
        double randoms = rand.nextDouble();
        for (i = 0; i < P; ) {
            for (j = 0; j < N; ) {
                if ((x < randoms) && (randoms < y)) {
                    if (n_population[i].gene[j] == 0)
                        n_population[i].gene[j] = 1;
                    else {
                        n_population[i].gene[j] = 0;
                    }
                }
                offspring[i] = n_population[i];
            }
        }
        return offspring;
    }

    public static int Mean(Individual[] n_population) {
        int mean_fitness = 0;
        for (i = 0; i < P; ) {
            mean_fitness += n_population[i].fitness;
        }
        return (mean_fitness / P);
    }

    public static void main(String[] args) {

        n_population = population();

        for (i = 0; i < 50; i++) {
            n_population = Selection(n_population);
            n_population = Crossover(n_population);
            n_population = Mutation(n_population);
        }
    }
}

1 个答案:

答案 0 :(得分:2)

你有:

 public static Individual[] Selection(Individual[] n_population) {
    n_population = new Individual[P];

因此,您传入n_population然后立即隐藏它,并声明一个包含所有空值的数组。因此,其中的任何n_population[i]都为空。我希望你稍后检查它中的.fitness时会得到一个NullPointerException(不是超出范围)

我认为你应该删除n_population = new Individual[P];