2016-11-17 3 views
-1

私はJavaで書かれた遺伝的アルゴリズムを作成しています。突然変異関数は、配列内のビットを割り当てられた確率で反転させます。 突然変異関数は、配列の変異集団(個体)を保持していません。 GAの最初の反復後ディープ・アレイ・コピーが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)); 
    } 
} 

、突然変異の機能は、人口ではなく、すべての異なる「変異した」個人の最後個々のすべてのコピーされている個人の人口を返します。 (配列の最後にある適合度は評価されていません)。ループ

外部

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 ITERATION

コピー

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 ITERATION

コピー

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; 
} 

両方の方法は、ループ内または外に使用するかどうかを変異集団中の最後の個々のコピーの同一個体(又はアレイ)の集団を生成します。 この例では、コピーする最後の2つの配列は同じですが、結果の子孫/変異子孫配列はすべて最後の配列のコピーです。

私は、GAが機能するように変異配列の集団を保持したいと考えています。

+0

このメソッドを100回実行すると、それは完全に実行されるテストで100回実行されます。 –

答えて

1

まず、私はあなたがGAの基本を正しく解釈していないと思います。あなたは全人口を摂取していて、10%の確率で配列のすべての要素を「変異させる」のですか? その場合、母集団のほとんどすべての要素に変異があります。それは正しいとは思わない。世代ごとに突然変異に参加するには、この割合を適用して集団の10%のみを選択する必要があります。

あなたの問題は、ゲノムのハードコピーを作成していないということです。

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

あなたはindividual.gene = gene;実際にはこの新しい個体を「親」のゲノムに向けています。問題はthisと似ていますか?

+0

これは私のクローンが各ゲノムを実行していなかったことです。 また、母集団の各個体が各世代の突然変異の候補であると私は考えましたか?そしてはい、私は突然変異の確率を変え、各ゲノムは潜在的に突然変異していますか?私はこれを読んで、感謝します。 –

関連する問題