当我尝试运行我的代码时,它告诉我在Caluclate Fitness中有多个数组超出范围异常,并且选择了任何想法来帮助我感谢。它说错误在第67行,我相信我的主类(遗传算法)中的一个错误和37个错误。个人类似乎很好我希望问题在于我如何定义我的功能,但我不知道要解决这个错误。我已经出演了它是选择数组的错误。在选择线路,它说数组越界50我猜意味着它认为在数组中的我没有声明,但我已经宣布它修复阵列越界异常新阵列异常
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);
}
}
}
请格式化你的代码,其中,NPE是从 –
未来亮点的行号请提供“个人”的定义和抛出异常的完整堆栈跟踪以帮助诊断问题。 –
@LouFranco指出,它看起来像第43行,'n_population = new Individual [P];',用一个新的'null'数组覆盖'n_population',产生第50行,if( n_population [parent2] .fitness> = n_population [parent1] .fitness)',尝试访问'null'个人的'fitness'属性。 –