Howdy!
I wrote this program to mutate colors on a grid, where there’s a probability of an offspring grid inheriting each square from its parent grid, or mutating it into a randomized color.
…but I cannot get it to work.
Every time that the “mutate versus inherit random roll” chooses to inherit, it inherits an empty gene (0). I cannot get the parent’s genes to stay as they were assigned, so they cannot be inherited. They all turn to zero in the offspring.
I am posting a simplified version of the program, that holds the gist of the problem at hand.
Thank you.
float x = 0;
float y = 0;
int mouseCount = 0;
int[] rowA0 = new int[16];
int[] rowA1 = new int[16];
int[] rowA2 = new int[16];
int[] rowA3 = new int[16];
ArrayList<Animal> animals;
float mutationRate = 0.4; //40% probability of mutation for each gene. 60% probability of inheritance for each gene.
void setup() {
size (333, 1000);
animals = new ArrayList<Animal>();
}
void draw() {
}
void mousePressed() {
animals.add(new Animal(mouseX, mouseY));
Animal a = animals.get(mouseCount); //mouseCount is the index. By getting mouseCount it is accessing that Animal in that spot on the arrayList.
a.renderAllRows();
mouseCount ++;
}
class Animal {
float x = 0;
float y = 0;
float xOffset = 0;
float yOffset = 0;
int[] tempRowA = new int[16];
int r = 0;
Animal(float tempX, float tempY) {
x = tempX;
y = tempY;
}
void renderAllRows() {
println("ANIMAL " + mouseCount);
println("rowA0");
determineColor();
assignAndRender();
int [] rowA0 = tempRowA.clone();
printArray(rowA0);
y += 20;
xOffset = 0;
println("rowA1");
r++;
determineColor();
assignAndRender();
int [] rowA1 = tempRowA.clone();
printArray(rowA1);
y += 20;
xOffset = 0;
println("rowA2");
r++;
determineColor();
assignAndRender();
int [] rowA2 = tempRowA.clone();
printArray(rowA2);
y += 20;
xOffset = 0;
r = 0;
}
//DETERMINE COLOR of rows : SIMPLE METER
int[] determineColor() {
if (mouseCount >= 1 && r == 0) {
for (int i = 0; i < 16; i++) {
if (random(1) > mutationRate) {
tempRowA[i] = rowA0[i];
println(i + " inherited");
} else {
tempRowA[i] = int(random(6));
}
}
}
if (mouseCount >= 1 && r == 1) {
for (int i = 0; i < 16; i++) {
if (random(1) > mutationRate) {
tempRowA[i] = rowA1[i];
println(i + " inherited");
} else {
tempRowA[i] = int(random(6));
}
}
}
if (mouseCount >= 1 && r == 2) {
for (int i = 0; i < 16; i++) {
if (random(1) > mutationRate) {
tempRowA[i] = rowA2[i];
println(i + " inherited");
} else {
tempRowA[i] = int(random(6));
}
}
}
if (mouseCount >= 1 && r == 3) {
for (int i = 0; i < 16; i++) {
if (random(1) > mutationRate) {
tempRowA[i] = rowA3[i];
println(i + " inherited");
} else {
tempRowA[i] = int(random(6));
}
}
}
if (mouseCount == 0) {
for (int i = 0; i < 16; i++) tempRowA[i] = int(random(6));
}
return tempRowA;
}
//RENDERS (draws) colors on phenotype (rows)
void assignAndRender() {
for (int i = 0; i < tempRowA.length; i++) {
if (tempRowA[i] == 0) fill(0, 0, 0, 0.0); //double probability
if (tempRowA[i] == 1) fill(0, 0, 0, 0.0); //of transparent
if (tempRowA[i] == 2) fill(255, 0, 0);
if (tempRowA[i] == 3) fill(0, 0, 255);
if (tempRowA[i] == 4) fill(255, 255, 255);
if (tempRowA[i] == 5) fill(255, 255, 0);
rect(x + xOffset, y + yOffset, 12, 20);
xOffset += 12;
if (i == 15) {
xOffset -= 192;
}
}
}
}