Issue with setting values in an array
I'm trying to write a genetic algorithm, I'm having a bit of trouble managing the arrays though.
The method I'm writing is supposed to take a array of 16 binary digits (16 zeros or ones) and create ten new arrays that are each replicas of the original array with two random digits flipped (if it was a zero, it becomes a one), then returning all of the arrays in a multidimensional array.
My problem is that whenever I modify one of the new arrays ( x=4; bit1=6; generation[x][bit1] = 1; ) it changes the state of all the arrays (even the original one!) at digit 6.
Here's a simplified version of my code to illustrate the problem:
Code:
import java.util.Random;
public class GeneticTestingSimplified{
public static void main(String[] args){
int closestMatch[] = {0,1,0,0,1,1,0,1,1,0,1,1,0,1,1,1};
System.out.println("The input array:");
for(int i=0;i<16;i++){
System.out.print(closestMatch[i]);
}
int generation[][] = mutate(closestMatch);
System.out.println("\nThe same array after calling mutate():");
for(int i=0;i<16;i++){
System.out.print(closestMatch[i]);
}
//note that this is different than the original closestMatch array,
//even though mutate() should not have been able to modify it
System.out.println("\nOutput array of mutate():");
for(int x=0;x<10;x++){
for(int y=0;y<16;y++){
System.out.print(generation[x][y]);
}
System.out.print("\n");
}
}
public static int[][] mutate(int closestMatch[]){
Random rand = new Random();
int generation[][] = new int[10][16];
for(int i=0;i<10;i++){
generation[i] = closestMatch;
}
for(int x=1;x<10;x++){
int bit1 = rand.nextInt(16);
if(generation[x][bit1] == 0){
generation[x][bit1] = 1;
}
else generation[x][bit1] = 0;
int bit2 = rand.nextInt(16);
while(bit1 == bit2){
bit2 = rand.nextInt(16);
}
if(generation[x][bit2] == 0){
generation[x][bit2] = 1;
}
else generation[x][bit2] = 0;
}
return generation;
}
}
Why are all of my arrays being changed? Mutate shouldn't be able to modify closestMatch[], how is this happening!?
How can I fix this code!?!? :smash:
Re: Issue with setting values in an array
Quote:
Originally Posted by
pyat77
Why are all of my arrays being changed?
Because you wrote code to do that.
Quote:
Mutate shouldn't be able to modify closestMatch[]
Incorrect.
Quote:
how is this happening!?
Looks like you need to revise Objects and parameter passing.
Re: Issue with setting values in an array
there a problem with the mutate method. Specifically here:
Code:
Random rand = new Random();
int generation[][] = new int[10][16];
for(int i=0;i<10;i++){
generation[i] = closestMatch;
}
you can't just pass the array closestMatch into generation[i]; You should pass in each value of closestMatch into generation, one at a time with a second for loop.
Re: Issue with setting values in an array
Thank you CHLim! Can you please explain to me why this is? Or, why I was getting those results?
Re: Issue with setting values in an array
Well pyat, i just messed around with the code and kinda got it to work. Why it didn't work i'm not sure but i'm guessing that there's an error that was not detected. But if you ever find out why, do let me know. Thanks.