Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exceptions when running multi-objective problems #238

Open
sigtrygg-space opened this issue Oct 25, 2023 · 1 comment
Open

Exceptions when running multi-objective problems #238

sigtrygg-space opened this issue Oct 25, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@sigtrygg-space
Copy link

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 ahmedfgad added the bug Something isn't working label Nov 2, 2023
@ahmedfgad
Copy link
Owner

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.

ahmedfgad added a commit that referenced this issue Jan 29, 2024
Release Date 29 January 2024
1. Solve bugs when multi-objective optimization is used. #238
2. When the `stop_ciiteria` parameter is used with the `reach` keyword, then multiple numeric values can be passed when solving a multi-objective problem. For example, if a problem has 3 objective functions, then `stop_criteria="reach_10_20_30"` means the GA stops if the fitness of the 3 objectives are at least 10, 20, and 30, respectively. The number values must match the number of objective functions. If a single value found (e.g. `stop_criteria=reach_5`) when solving a multi-objective problem, then it is used across all the objectives. #238
3. The `delay_after_gen` parameter is now deprecated and will be removed in a future release. If it is necessary to have a time delay after each generation, then assign a callback function/method to the `on_generation` parameter to pause the evolution.
4. Parallel processing now supports calculating the fitness during adaptive mutation. #201
5. The population size can be changed during runtime by changing all the parameters that would affect the size of any thing used by the GA. For more information, check the [Change Population Size during Runtime](https://pygad.readthedocs.io/en/latest/pygad_more.html#change-population-size-during-runtime) section. #234
6. When a dictionary exists in the `gene_space` parameter without a step, then mutation occurs by adding a random value to the gene value. The random vaue is generated based on the 2 parameters `random_mutation_min_val` and `random_mutation_max_val`. For more information, check the [How Mutation Works with the gene_space Parameter?](https://pygad.readthedocs.io/en/latest/pygad_more.html#how-mutation-works-with-the-gene-space-parameter) section. #229
7. Add `object` as a supported data type for int (GA.supported_int_types) and float (GA.supported_float_types). #174
8. Use the `raise` clause instead of the `sys.exit(-1)` to terminate the execution. #213
9. Fix a bug when multi-objective optimization is used with batch fitness calculation (e.g. `fitness_batch_size` set to a non-zero number).
10. Fix a bug in the `pygad.py` script when finding the index of the best solution. It does not work properly with multi-objective optimization where `self.best_solutions_fitness` have multiple columns.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants