First of all sorry if the question is basic, but I am not a C++ expert.
I am investigating Genetic Algorithms in Java and I arrived to this link, which contains interesting info: http://web.archive.org/web/20100216182958/http://fog.neopages.org/helloworldgeneticalgorithms.php
However, I quite can´t understand what is this method doing:
int fitness(bool* chromosome)
{
// the ingredients are: 0 1 2 3 4 5 6
// salt sugar lemon egg water onion apple
return ( -chromosome[0] + chromosome[1] + chromosome[2]
-chromosome[3] + chromosome[4] - chromosome[5]
-chromosome[6] );
}
With academic purposes, I am trying to "translate" the C++ program to Java, but I don´t understand this method, what is exactly returning? (I assume it´s operating with an array.)
It's returning an integer. Booleans are being converted to integers before being added/subtracted together. True is 1. False is 0.
Here's the Java translation. In our case, we're having to convert the booleans to integers ourselves.
int fitness(boolean[] chromosome)
{
int[] intChromosome = toInt(chromosome);
// the ingredients are: 0 1 2 3 4 5 6
// salt sugar lemon egg water onion apple
return ( -intChromosome [0] + intChromosome [1] + intChromosome [2]
-intChromosome [3] + intChromosome [4] - intChromosome [5]
-intChromosome [6] );
}
int[] toInt(boolean[] values) {
int[] integers = new int[values.length];
for (int i = 0; i < values.length; i++) {
integers[i] = values[i] ? 1 : 0;
}
return integers;
}