當我嘗試運行我的代碼時,它告訴我在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'屬性。 –