2016-11-17 1 views
-1

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가 작동하도록 변형 된 배열의 채우기를 유지하려고합니다.

+0

이 방법을 테스트 할 때마다 100 번 실행하면 완벽하게 수행됩니다 ... –

답변

1

우선. 나는 당신이 GA의 기본 원리를 정확하게 해석하지 못하고 있다고 생각합니다. 전체 인구를 섭취하고 10 % 확률로 배열의 모든 요소를 ​​"변경"하고 있습니까? 이 경우, 당신은 돌연변이가 인구 집단의 거의 모든 단일 요소입니다. 그건 틀린 것 같아. 매 세대마다 돌연변이에 참여할 인구의 10 % 만 선택하려면이 비율을 적용해야합니다.

그 외에도 당신의 문제는 당신이 게놈의 하드 카피를 만들고 있지 않다는 것입니다.

Individual individual = new Individual(gene, fitness); 
individual.gene = gene; 

당신이 할 때 individual.gene = gene; 실제로이 새로운 개체를 "부모"의 게놈으로 향하게합니다. this 질문과 비슷한 문제입니까?

+0

고마워요, 제 클론이 각 게놈을 수행하지 못했습니다. 또한, 나는 인구의 각 개체가 각 세대의 돌연변이 후보자라고 생각 했습니까? 그리고 네, 저는 돌연변이 확률을 바꾸고 각 게놈은 잠재적으로 돌연변이가됩니까? 이것에 대해서 다시 읽어 볼께. 고마워. –

관련 문제