I am currently Coding a Genetic algorithm to solve a problem that could be considered similar to the knapsack problem but the differentiating factor is that I am storing items over multiple "trucks", and their value/importance is based on a 1 = Most important, 3 = Least important.
How i am doing this is with an array of binary integers, 0 = not on the truck, 1 = on the truck.
For the most part, the program seems to be doing what it needs to do. Except when I am comparing the chromosomes to find the best three individual chromosomes for each generation. Individual being that no chromosomes can contain the same item, As the item can only be on one truck at a time.
This is the function I am using to compare the chromosomes:
private int[] getBestSolution(int count)
{
int[] Genes = new int[3];
int min1 = Global.p.population[0].fitness;
int min2 = Global.p.population[0].fitness;
int min3 = Global.p.population[0].fitness;
int ref1 = 0;
int ref2 = 0;
int ref3 = 0;
bool areSame = true;
//searches for the first "Fittest" chromosome and stores to ref1
for (int i = 0; i < Global.population; i++)
{
if (min1 > Global.p.population[i].fitness)
{
min1 = Global.p.population[i].fitness;
ref1 = i;
}
}
//searches for 2nd fittest, while also checking they are different
for (int i = 0; i < Global.population; i++)
{
areSame = arrayCompare(Global.p.population[ref1].chromosome, Global.p.population[i].chromosome);
if(areSame == true)
{
continue;
}
if (min2 > Global.p.population[i].fitness)
{
min2 = Global.p.population[i].fitness;
ref2 = i;
}
}
//Searches for 3rd fittest while checking that it is different from the first two
for (int i = 0; i < Global.population; i++)
{
areSame = arrayCompare(Global.p.population[ref1].chromosome, Global.p.population[i].chromosome);
if (areSame == true)
{
continue;
}
areSame = arrayCompare(Global.p.population[ref2].chromosome, Global.p.population[i].chromosome);
if(areSame == true)
{
continue;
}
if (min3 > Global.p.population[i].fitness)
{
min3 = Global.p.population[i].fitness;
ref3 = i;
}
}
//stores the reference of each chromosome and return
Genes[0] = ref1;
Genes[1] = ref2;
Genes[2] = ref3;
return Genes;
}
This is the function i am using to compare the chromosome to the Previously selected chromosome to ensure they do not containt the same value.
public bool arrayCompare(int[] a, int[] b)
{
for (int i = 0; i < a.Length; i++)
{
if ((a[i] == 1) && b[i] == 1)
{
return true;
}
}
return false;
}
I have narrowed it down to these two fuctions causing the problem by using breakpoints at different points of the code and checking what the values are vs what they are expected to be.
Finally, to the problem itself. The getBestSolution() produces the first "fittest" value perfectly. The second and third values stay as what they were initialized as "population[0].fitness" and that is displayed.
I attempted to change the breeding structure, the selection criteria for breeding as I believed that if there were no chromosomes containing different values, it would stay as initialized. I have also tested using larger populations for this same reason, so i believe that their must be a problem with my code.
Any help is much appreciated.