Differentiating Array definitions

Hello friends,

I am programming a genome that’s made of chromosomes. Each chromosome is an array. Each defines its values through symmetry producing operations (symmetry as in retrogrades of the integer sequence of its segments).

I made a function for this, so that I can call it in setup. It makes an array called tempChromosome. I would like that array to define each separate chromosome array. The problem is that it is defining all of the chromosomes as the same, as you can see in the console, but I want them to be different from each other!

int[] tempChromosome = new int[16];
int[] chromosome0 = new int[16];
int[] chromosome1 = new int[16];
int[] chromosome2 = new int[16];
int[] chromosome3 = new int[16];
int[] chromosome4 = new int[16];
int[] chromosome5 = new int[16];

//int[] genotype = new int[6];

void setup() {


  determineSymmetry(); //stochastic generation of number pattern. it defines tempChromosome.
  chromosome0 = tempChromosome;
  determineSymmetry(); //I would like this to redefine tempChromosome, so that chromosome1 is different from chromosome0.
  chromosome1 = tempChromosome;
  determineSymmetry(); // and so on, with each call to the function..., so that every chromosome is different. 
  chromosome2 = tempChromosome;
  determineSymmetry();
  chromosome3 = tempChromosome;
  determineSymmetry();
  chromosome4 = tempChromosome;
  determineSymmetry();
  chromosome5 = tempChromosome;




  println("=o=o=o=o=chromosome0");
  printArray(chromosome0);
  println("=o=o=o=o=chromosome1");
  printArray(chromosome1);
  println("=o=o=o=o=chromosome2");
  printArray(chromosome2);
  println("=o=o=o=o=chromosome3");
  printArray(chromosome3);
  println("=o=o=o=o=chromosome4");
  printArray(chromosome4);
  println("=o=o=o=o=chromosome5");
  printArray(chromosome5);
}

void draw() {
}


 int[] determineSymmetry() {


  for (int i = 0; i < 4; i++) {
    tempChromosome[i] = int(random(5));
  }

  float smallSymm1 = random(3);
  float largeSymm = random(3);
  float smallSymm2 = random(3);


  if (smallSymm1 < 1) {          //repeat sequence
    tempChromosome[4] = tempChromosome[0];
    tempChromosome[5] = tempChromosome[1];
    tempChromosome[6] = tempChromosome[2];
    tempChromosome[7] = tempChromosome[3];
  } else if (smallSymm1 < 2) {    //retrograde sequence     
    tempChromosome[4] = tempChromosome[3];
    tempChromosome[5] = tempChromosome[2];
    tempChromosome[6] = tempChromosome[1];
    tempChromosome[7] = tempChromosome[0];
  } else {                         
    for (int i = 4; i < 8; i++) { //generate another random sequence
      tempChromosome[i] = int(random(5));
    }
  }

  if (largeSymm < 1) {          //repeat sequence (large scale)
    tempChromosome[8] = tempChromosome[0];
    tempChromosome[9] = tempChromosome[1];
    tempChromosome[10] = tempChromosome[2];
    tempChromosome[11] = tempChromosome[3];
    tempChromosome[12] = tempChromosome[4];
    tempChromosome[13] = tempChromosome[5];
    tempChromosome[14] = tempChromosome[6];
    tempChromosome[15] = tempChromosome[7];
  } else if (largeSymm < 2) {    //retrograde sequence  (large scale) 
    tempChromosome[8] = tempChromosome[7];
    tempChromosome[9] = tempChromosome[6];
    tempChromosome[10] = tempChromosome[5];
    tempChromosome[11] = tempChromosome[4];
    tempChromosome[12] = tempChromosome[3];
    tempChromosome[13] = tempChromosome[2];
    tempChromosome[14] = tempChromosome[1];
    tempChromosome[15] = tempChromosome[0];
  } else {                         
    for (int i = 8; i < 12; i++) { //generate another random sequence of four numbers
      tempChromosome[i] = int(random(5));
    }

    if (smallSymm2 < 1) {          //repeat sequence
      tempChromosome[12] = tempChromosome[8];
      tempChromosome[13] = tempChromosome[9];
      tempChromosome[14] = tempChromosome[10];
      tempChromosome[15] = tempChromosome[11];
    } else if (smallSymm2 < 2) {    //retrograde sequence     
      tempChromosome[12] = tempChromosome[11];
      tempChromosome[13] = tempChromosome[10];
      tempChromosome[14] = tempChromosome[9];
      tempChromosome[15] = tempChromosome[8];
    } else {                         
      for (int i = 12; i < 16; i++) { //generate another random sequence
        tempChromosome[i] = int(random(5));
      }
    }
  }
  
  return tempChromosome;
}

I feel this is simple to fix but I don’t know how. Help would be appreciated. Thank you.

1 Like
1 Like

I don’t understand why, but that worked. Thank you!

Arrays in Processing (and Java) are objects, which means that the variable only holds a reference. When you assign the array to a different variable, it does not create a copy of the array. Both variables will point to the same array. See this example:

int[] array1 = {1, 2, 3};
int[] array2 = {4, 5, 6};

printArray(array1); // 1, 2, 3
printArray(array2); // 4, 5, 6

array1 = array2;
// now both variables refer to the same array

printArray(array1); // 4, 5, 6
printArray(array2); // 4, 5, 6

// there is now only one array, changes are
// visible through both variables (references)
array2[0] = 42;

printArray(array1); // 42, 5, 6
printArray(array2); // 42, 5, 6

array1 = array2.clone();
// now there are two separate arrays
array2[0] = 21;

printArray(array1); // 42, 5, 6
printArray(array2); // 21, 5, 6
2 Likes

How do I concatenate all my arrays into a single long array? concat does not seem to be working as the error appears: The function “concat()” expects parameters like: “concat(int[],int[])”

void setup() {

  
  
  println("chromosome0");
  determineSymmetry(); 
  int [] chromosome0 = tempChromosome.clone();
  printArray(chromosome0);

  println("chromosome1");  
  determineSymmetry();
  int [] chromosome1 = tempChromosome.clone();
  printArray(chromosome1);  
  
  println("chromosome2");  
  determineSymmetry(); 
  int [] chromosome2 = tempChromosome.clone();
  printArray(chromosome2);  
  
  println("chromosome3");  
  determineSymmetry();
  int [] chromosome3 = tempChromosome.clone();
  printArray(chromosome3);  

  println("chromosome4");
  determineSymmetry();
  int [] chromosome4 = tempChromosome.clone();
  printArray(chromosome4);  
  
  println("chromosome5");
  determineSymmetry();
  int [] chromosome5 = tempChromosome.clone();
  printArray(chromosome5);
  
int[] genotype = concat(chromosome0, chromosome1, chromosome2, chromosome3, chromosome4, chromosome5);


 
 println("genotype");
 printArray(genotype);
}

You need to do it a pair at once rather than all at once: :couple:

int[] genotype = concat(chromosome0, chromosome1);
genotype = concat(genotype, chromosome2);
genotype = concat(genotype, chromosome3);

// and so on...

Processing.org/reference/concat_.html

1 Like