GeneticAlgorithmPython icon indicating copy to clipboard operation
GeneticAlgorithmPython copied to clipboard

GANN doesn't work with `mutation_type='adaptive'`

Open 60k41p opened this issue 2 years ago • 1 comments

versions: Python 3.11.4, PyGAD 3.1.0 on Linux x86_64.

pygad.gann module documentation includes an XOR Example.

It works as is. However, as soon as I switch to using 'adaptive' mutation, I get an error:

list indices must be integers or slices, not NoneType
Traceback (most recent call last):
  File "/home/a/.local/lib/python3.11/site-packages/pygad/pygad.py", line 2019, in run
    self.last_generation_offspring_mutation = self.mutation(self.last_generation_offspring_crossover)
                                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/a/.local/lib/python3.11/site-packages/pygad/utils/mutation.py", line 529, in adaptive_mutation
    offspring = self.adaptive_mutation_randomly(offspring)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/a/.local/lib/python3.11/site-packages/pygad/utils/mutation.py", line 676, in adaptive_mutation_randomly
    average_fitness, offspring_fitness = self.adaptive_mutation_population_fitness(offspring)
                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/a/.local/lib/python3.11/site-packages/pygad/utils/mutation.py", line 479, in adaptive_mutation_population_fitness
    fitness[idx] = self.fitness_func(self,
                  ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/dev/shm/xor.py", line 9, in fitness_func
    predictions = pygad.nn.predict(last_layer=GANN_instance.population_networks[sol_idx],
                                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^
TypeError: list indices must be integers or slices, not NoneType

My only modification to the example from the documentation page linked above:

                       mutation_type='adaptive',
                       mutation_percent_genes=(100, 50),

Is enough to trigger the error. Same with:

                       mutation_probability=(.20, .05),

I would appreciate seeing this bug fixed as I would like to use adaptive mutation.

Please let me know if more information is needed.

60k41p avatar Jul 18 '23 12:07 60k41p

Thanks. The issue is solved https://github.com/ahmedfgad/GeneticAlgorithmPython/commit/01cb71c555da32272ec7fcb70c64416d10ae84bb

ahmedfgad avatar Aug 16 '23 11:08 ahmedfgad