Java로 작성된 Genetic Algorithm을 만들고 있습니다. 돌연변이 함수는 할당 된 확률로 배열의 비트를 뒤집습니다. 돌연변이 함수가 배열의 변형 된 채우기를 보유하지 않습니다 (개인).깊은 배열 복사본이 Java의 루프 외부에서 값이 변경되는 이유는 무엇입니까?
public static void mutation(Individual[] population, Individual[] mutatedOffspring, double mutationRate) {
// Iterate through gene, randomly select whether
// or not to change the value of the genome
//
System.out.println("\nMUTATION\n");
Random mutant = new Random();
Individual[] offspring = new Individual[POPULATION_SIZE];
System.out.println("mutated offspring array");
for (int i = 0; i < (population.length); i++) {
for (int j = 0; j < population[i].gene.length; j++) {
// flip bits in array at preset probability (0.1)
if (mutationRate > mutant.nextDouble()) {
if (population[i].gene[j] == 0) {
population[i].gene[j] = 1;
} else if (population[i].gene[j] == 1) {
population[i].gene[j] = 0;
}
}
}
// Deep copy contents of mutated array into new object array index (Individual)
fitness(population);
offspring[i] = new Individual(population[i].gene, population[i].fitness);
// Print both mutated array and copied array to show successful copy
System.out.println("offspring " + i + Arrays.toString(population[i].gene) + (population[i].fitness));
System.out.println("copy: " + i + Arrays.toString(offspring[i].gene) + (offspring[i].fitness));
}
//print same array outside loop of population
System.out.println("\n");
for (int i = 0; i < offspring.length; i++) {
System.out.println("copy: " + i + Arrays.toString(offspring[i].gene) + (offspring[i].fitness));
}
// deep copy outside p of population using .clone
for (int i = 0; i < offspring.length; i++) {
mutatedOffspring[i] = offspring[i].clone();
}
fitness(mutatedOffspring);
System.out.println("\n");
System.out.println("deep copied array using .clone() outside loop");
for (int i = 0; i < mutatedOffspring.length; i++) {
System.out.println("offspring " + i + Arrays.toString(mutatedOffspring[i].gene) + (mutatedOffspring[i].fitness));
}
}
는 GA의 첫 번째 반복 한 후, 돌연변이 기능은 모든 다른 '돌연변이'개인으로, 인구의 마지막 개인의 모든 사본입니다 개인의 인구를 반환합니다. 배열 끝에있는 적합도 값은 평가되지 않았습니다. 루프
외부copy: 0[0, 0, 1, 0, 1, 1, 0, 1, 0, 1]4
copy: 1[1, 0, 1, 0, 0, 0, 0, 0, 0, 1]3
copy: 2[0, 1, 1, 0, 0, 1, 1, 1, 1, 1]6
copy: 3[0, 1, 1, 0, 0, 1, 1, 1, 1, 1]8
copy: 4[0, 1, 1, 0, 0, 1, 1, 1, 1, 1]7
copy: 5[0, 0, 1, 0, 1, 1, 0, 1, 0, 1]5
딥 카피 사용 .clone() : 인구의 루프 외부
offspring 0[0, 0, 1, 0, 1, 1, 0, 0, 0, 1]4
copy: 0[0, 0, 1, 0, 1, 1, 0, 0, 0, 1]4
offspring 1[1, 0, 1, 0, 0, 0, 0, 0, 0, 1]3
copy: 1[1, 0, 1, 0, 0, 0, 0, 0, 0, 1]3
offspring 2[1, 1, 1, 1, 0, 0, 1, 1, 0, 0]6
copy: 2[1, 1, 1, 1, 0, 0, 1, 1, 0, 0]6
offspring 3[1, 1, 1, 1, 1, 0, 1, 1, 1, 0]8
copy: 3[1, 1, 1, 1, 1, 0, 1, 1, 1, 0]8
offspring 4[0, 1, 1, 0, 0, 1, 1, 1, 1, 1]7
copy: 4[0, 1, 1, 0, 0, 1, 1, 1, 1, 1]7
offspring 5[0, 0, 1, 0, 1, 1, 0, 1, 0, 1]5
copy: 5[0, 0, 1, 0, 1, 1, 0, 1, 0, 1]5
같은 복사 배열 : 인구의 루프 내
1 반복
사본
offspring 0[0, 0, 1, 0, 1, 1, 0, 1, 0, 1]5
offspring 1[1, 0, 1, 0, 0, 0, 0, 0, 0, 1]3
offspring 2[0, 1, 1, 0, 0, 1, 1, 1, 1, 1]7
offspring 3[0, 1, 1, 0, 0, 1, 1, 1, 1, 1]7
offspring 4[0, 1, 1, 0, 0, 1, 1, 1, 1, 1]7
offspring 5[0, 0, 1, 0, 1, 1, 0, 1, 0, 1]5
루프
외부
copy: 0[0, 1, 0, 1, 0, 0, 0, 1, 1, 0]4
copy: 1[0, 1, 0, 1, 0, 0, 0, 1, 1, 0]5
copy: 2[0, 1, 0, 1, 0, 0, 0, 1, 1, 0]3
copy: 3[0, 1, 0, 1, 0, 0, 0, 1, 1, 0]5
copy: 4[0, 1, 0, 1, 0, 0, 0, 1, 1, 0]4
copy: 5[0, 1, 0, 1, 0, 0, 0, 1, 1, 0]4
딥 카피 사용 .clone() : 인구의 루프 외부
offspring 0[0, 1, 0, 0, 1, 1, 0, 0, 0, 1]4
copy: 0[0, 1, 0, 0, 1, 1, 0, 0, 0, 1]4
offspring 1[0, 1, 0, 0, 1, 1, 1, 1, 0, 0]5
copy: 1[0, 1, 0, 0, 1, 1, 1, 1, 0, 0]5
offspring 2[0, 0, 0, 0, 1, 1, 0, 1, 0, 0]3
copy: 2[0, 0, 0, 0, 1, 1, 0, 1, 0, 0]3
offspring 3[1, 1, 0, 1, 0, 0, 0, 1, 1, 0]5
copy: 3[1, 1, 0, 1, 0, 0, 0, 1, 1, 0]5
offspring 4[0, 1, 0, 1, 0, 0, 0, 1, 1, 0]4
copy: 4[0, 1, 0, 1, 0, 0, 0, 1, 1, 0]4
offspring 5[0, 1, 0, 1, 0, 0, 0, 1, 1, 0]4
copy: 5[0, 1, 0, 1, 0, 0, 0, 1, 1, 0]4
같은 복사 배열 : 인구의 루프 내
2 회 반복
사본
offspring 0[0, 1, 0, 1, 0, 0, 0, 1, 1, 0]4
offspring 1[0, 1, 0, 1, 0, 0, 0, 1, 1, 0]4
offspring 2[0, 1, 0, 1, 0, 0, 0, 1, 1, 0]4
offspring 3[0, 1, 0, 1, 0, 0, 0, 1, 1, 0]4
offspring 4[0, 1, 0, 1, 0, 0, 0, 1, 1, 0]4
offspring 5[0, 1, 0, 1, 0, 0, 0, 1, 1, 0]4
나는 새 개인을 만들고 각각의 반복 후에 복사 할 개인의 값을 할당하여 전체 복사본을 만들고 있다고 확신한다. 또한 Individual 클래스에서 clone() 함수를 만들려고했습니다.
public Individual clone(){
Individual individual = new Individual(gene, fitness);
individual.gene = gene;
individual.fitness = fitness;
return individual;
}
두 방법은 루프 내부 또는 외부 사용 여부 돌연변이 인구의 마지막 개인의 사본입니다 동일한 개인 (또는 배열)의 인구를 생산하고 있습니다. 예제에서 복사 할 마지막 두 배열은 동일하지만 결과로 생성되는 자손/돌연변이 자식 배열은 모두 마지막 복사본입니다.
GA가 작동하도록 변형 된 배열의 채우기를 유지하려고합니다.
이 방법을 테스트 할 때마다 100 번 실행하면 완벽하게 수행됩니다 ... –