I am creating an evolutionary algorithm that is used in a game of mine that gets an enemy NPC (Individual) and its properties (health, speed, attack power, etc.) which are all float numbers. The aim is the enemy's properties to match the target values that are decided based on the performance of the player. My problem is that with my current fitness function the algorithm finds the two out of the three values (more properties will be added in the future) but then it just stops trying to find the last one. It's as if the fitness value doesn't change at all. So for example, if my target values are (50, 16, 120) the best individual's values will be (68, 16, 120) and it won't be able to match the final value.
I tried changing my fitness function but I can't seem to find the solution to the problem. This is the fitness function:
private float FitnessFunction(int index)
{
Individual _dna = ga.population[index];
int _matchingDNA = _dna.genes.Length;
float _score = 0;
for (int i = 0; i < _dna.genes.Length; i++)
{
float _tmpDiff = Math.Abs(target.genes[i] - _dna.genes[i]);
if (_dna.genes[i] == target.genes[i])
{
_score++;
}
else if (_dna.genes[i] != target.genes[i])
{
_matchingDNA--;
}
if (_tmpDiff <= 30)
{
_score += 0.5f;
}
else if (_tmpDiff > 5)
{
_score -= 0.5f;
}
if (_matchingDNA == _dna.genes.Length)
{
solutionFound = true;
}
}
return _score;
}