0

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;
    }
  • Please [edit] your question to include a more detailed description for "gets a list of real values and tries to match them to the target values". Also write in detail what your "individual" is, how it is defined and what it defines. Include the full source code you have as a [mcve], which can be compiled and tested by others. – Progman Aug 17 '19 at 18:03
  • Why don't you simply make your score the sum of the abs differences between each vector component? Then if the whole thing is lower than a threshold stop. Also try using vectors of dimension 1 (only one component) to check if you have an off-by-one error somewhere. – Pablo Aug 30 '19 at 14:19

0 Answers0