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.85k stars 462 forks source link

Fix check for undefined pop fitness when using parallel processing #237

Closed MBradbury closed 11 months ago

MBradbury commented 11 months ago

When using parallel processing and multiple objectives are returned from the fitness function, then the following exception will be raised. This PR fixes the issue by avoiding comparisons between the undefined string and a numpy array. This would fix #236.

ERROR: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Traceback (most recent call last):
  File ".venv/lib/python3.10/site-packages/pygad/pygad.py", line 1790, in cal_pop_fitness
    if pop_fitness[sol_idx] == "undefined":
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Version information:

$ python
Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygad
>>> pygad.__version__
'3.2.0'