ahmedfgad / GeneticAlgorithmPython

Source code of PyGAD, a Python 3 library for building the genetic algorithm and training machine learning algorithms (Keras & PyTorch).
https://pygad.readthedocs.io
BSD 3-Clause "New" or "Revised" License
1.79k stars 451 forks source link

Exceptions when running multi-objective problems #238

Open sigtrygg-space opened 8 months ago

sigtrygg-space commented 8 months ago

First off: What an awesome library!

I've noticed some problems when trying to solve multi-objective problems though. The underlying problem seems to be the same - some check not being prepared to handle multi-objective problems (e.g. comparison of two numbers, where multi-objective problems would have to compare two lists of numbers)

Main Exception Message:

The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

First Case: stop_criteria="saturate_{num}" I tried using stop_criteria="saturate_100" in combination with a multi-objective problem - it fails excactly at generation 100. That holds true for any number {num} - when generation {num} is reached, the stop criterion is checked for the first time and fails because it is not ready to handle multi-objective problems. This is the resulting Exception Message: Traceback (most recent call last): File "C:\path\to\pygad\pygad.py", line 2143, in run if (self.best_solutions_fitness[self.generations_completed - criterion[1]] - self.best_solutions_fitness[self.generations_completed - 1]) == 0: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Second Case: mutation_type="adaptive" Adaptive Mutation already fails on the first generation, but for the same underlying reason. When offspring_fitness is first evaluated, this Exception is thrown:

Traceback (most recent call last): File "C:\path\to\pygad\pygad.py", line 2062, in run self.last_generation_offspring_mutation = self.mutation(self.last_generation_offspring_crossover) File "C:\path\to\pygad\utils\mutation.py", line 556, in adaptive_mutation offspring = self.adaptive_mutation_probs_by_space(offspring) File "C:\path\to\pygad\utils\mutation.py", line 794, in adaptive_mutation_probs_by_space if offspring_fitness[offspring_idx] < average_fitness: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

There may be more instances of the same Exception popping up at other places throughout PyGAD, I haven't really searched. Would be great to have this fixed, so adaptive mutation and other great features become usable with multi-objective problems.

Cheers, Matthias

ahmedfgad commented 8 months ago

Thanks, Matthias :)

It is indeed reported that multi-objective has some bugs. Despite that some of them were solved, there are some other bugs existing. With your help, there will be less bugs.

Hopefully all the issues are solved in the next release.