Hello,
i have problem with overwriting totaly unrelated values with one function.
In my class Populace i create class vector
Prihrada[] prihrada_array;
and single object
Prihrada BIS;
Then i run it through my script and end up with naturalSelection function, where i create new object vector
Prihrada[] newPrihrada_array = new Prihrada[prihrada_array.length];
and find the most fitting object from the aray and set it as BIS object.
BIS = new Prihrada(prihrada_array[bestPrihrada].DNA);
and i set it on first spot of new array newPrihrada_array
newPrihrada_array[0] = new Prihrada(META.DNA);
The DNA is an object that store input values for the new object. It consist of several int vectors.
All other object in newPrihrada_array are made by following steps:
-select random prihrada_array object
-set its DNA object as mummy / daddy
DNA mummy = selectParent();
-do krizeni (crossover) and mutace (mutation)
mummy.krizeni(daddy);
mummy.mutace(0);
-finish up with separace (separation)
mummy.separace(0);
All those steps are made withing the selected DNA object, in this case called mummy
class Populace {
Prihrada[] prihrada_array;
Prihrada META;
.
.
.
void naturalSelection() {
Prihrada[] newPrihrada_array = new Prihrada[prihrada_array.length];
setBestPrihrada();
fitnessSum();
newPrihrada_array[0] = new Prihrada(META.DNA);
for (int i = 1; i < newPrihrada_array.length; i++) {
DNA mummy = selectParent();
DNA daddy = selectParent();
mummy.krizeni(daddy);
mummy.mutace(0);
mummy.separace(0);
mummy.kolize();
newPrihrada_array[i] = new Prihrada(mummy);
}
prihrada_array = newPrihrada_array;
gen++;
}
void setBestPrihrada() {
float[] fitness = new float[prihrada_array.length];
for (int i = 0; i < prihrada_array.length; i++) {
fitness[i] = prihrada_array[i].fitness;
}
float maxFitness = max(fitness);
int bestPrihrada = 0;
for (int i = 0; i < prihrada_array.length; i++) {
if (fitness[i] == maxFitness) {
bestPrihrada = i;
}
}
println("Gen " + gen + " maxFitness = " + maxFitness);
if (prihrada_array[bestPrihrada].fitness > fitnessmax) {
fitnessmax = prihrada_array[bestPrihrada].fitness;
BIS = new Prihrada(prihrada_array[bestPrihrada].DNA);
}
}
My problem is that if i run the for cycle, values in the Prihrada BIS changes and it ocures during the
separace function.
void separace(int j) {
int Jsize = joint_x.length - 3;
int[] gene = new int[chromosome.length];
for (int i = 0; i < gene.length; i++) {
gene[i] = chromosome[i][j];
}
arrayCopy(gene, 0, joint_x, 3, 1);
arrayCopy(gene, Jsize, joint_z, 3, 1);
arrayCopy(gene, 2 * Jsize, prurez, 1, prurez.length - 1);
prurez[0] = prurez[1];
}
If it would change values in the Prihrada array i would say that there is something wrong with the code, but how can it change values from the object i dont even mention in there?
Before separace function
After separace function both values in BIS changed to value of curently processed DNA object
Also in separace function:
Before
After. If the x value chanes, the other changes too
What i find odd is that it changes values stored in “this” which to my knowledge are only avaiable through “this” function which i dont even use.