Results 1 to 5 of 5
- 11-03-2011, 12:46 AM #1
Member
- Join Date
- Feb 2010
- Posts
- 15
- Rep Power
- 0
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:
Java 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!?!?
- 11-03-2011, 12:52 AM #2
- 11-03-2011, 02:27 AM #3
Member
- Join Date
- Nov 2011
- Posts
- 24
- Rep Power
- 0
Re: Issue with setting values in an array
there a problem with the mutate method. Specifically here:
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.Java Code:Random rand = new Random(); int generation[][] = new int[10][16]; for(int i=0;i<10;i++){ generation[i] = closestMatch; }
- 11-04-2011, 12:28 AM #4
Member
- Join Date
- Feb 2010
- Posts
- 15
- Rep Power
- 0
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?
- 11-04-2011, 02:01 AM #5
Member
- Join Date
- Nov 2011
- Posts
- 24
- Rep Power
- 0
Similar Threads
-
need to input values from a text file into an array and count values
By pds8475 in forum New To JavaReplies: 14Last Post: 01-22-2011, 02:36 PM -
Setting up a String Array
By AJArmstron@aol.com in forum New To JavaReplies: 12Last Post: 04-16-2010, 12:42 PM -
Compare Array and setting to temp
By saqib15 in forum New To JavaReplies: 0Last Post: 02-17-2010, 06:16 PM -
setting and getting array
By jgonzalez14 in forum New To JavaReplies: 6Last Post: 11-26-2008, 06:54 AM -
setting format for FLoat values
By bugger in forum New To JavaReplies: 2Last Post: 11-16-2007, 01:22 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks