Why does my deep array copy change it's value outside for loop in Java? -
this question has answer here:
i'm creating genetic algorithm written in java. mutation function flips bits in array @ assigned probability. mutation function not retaining mutated population of arrays (individuals).
public static void mutation(individual[] population, individual[] mutatedoffspring, double mutationrate) { // iterate through gene, randomly select whether // or not change value of genome // system.out.println("\nmutation\n"); random mutant = new random(); individual[] offspring = new individual[population_size]; system.out.println("mutated offspring array"); (int = 0; < (population.length); i++) { (int j = 0; j < population[i].gene.length; j++) { // flip bits in array @ 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 new object array index (individual) fitness(population); offspring[i] = new individual(population[i].gene, population[i].fitness); // print both mutated array , copied array show successful copy system.out.println("offspring " + + arrays.tostring(population[i].gene) + (population[i].fitness)); system.out.println("copy: " + + arrays.tostring(offspring[i].gene) + (offspring[i].fitness)); } //print same array outside loop of population system.out.println("\n"); (int = 0; < offspring.length; i++) { system.out.println("copy: " + + arrays.tostring(offspring[i].gene) + (offspring[i].fitness)); } // deep copy outside p of population using .clone (int = 0; < offspring.length; i++) { mutatedoffspring[i] = offspring[i].clone(); } fitness(mutatedoffspring); system.out.println("\n"); system.out.println("deep copied array using .clone() outside loop"); (int = 0; < mutatedoffspring.length; i++) { system.out.println("offspring " + + arrays.tostring(mutatedoffspring[i].gene) + (mutatedoffspring[i].fitness)); } }
after first iteration of ga, mutation function returns population of individuals, copies of last individual in population, not of different 'mutated' individuals. (the fitness values @ end of arrays haven't been evaluated).
1st iteration
copy within loop of population:
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
same copied array outside loop of population:
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
deep copy using .clone() outside loop
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
2nd iteration
copy within loop of population:
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
same copied arrays outside loop of population:
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
deep copy using .clone() outside loop
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
i'm sure i'm doing deep copy creating new individual , assigning values of individual wish copy after each iteration. i've tried creating clone() function in individual class.
public individual clone(){ individual individual = new individual(gene, fitness); individual.gene = gene; individual.fitness = fitness; return individual; }
both methods produce population of identical individuals (or arrays) copy of last individual in mutated population whether used inside loop or out. in example last 2 arrays copy same assure resulting offspring/ mutated offspring arrays copy of last one.
i want retain population of mutated arrays ga work.
first of all. think not correctly interpreting ga fundamentals. taking whole population, , "mutating" every single element of array 10% probability? if case, mutation every single element of population. doesn't seem correct. should apply percentage select 10% of population participate in mutation every generation.
besides that, think problem not making hard-copy of genome.
individual individual = new individual(gene, fitness); individual.gene = gene;
when individual.gene = gene; pointing new individual genome of "parent". problem similar this question.
Comments
Post a Comment