Skip to content

Throw exception instead of sys.exit in GA.__init__ #213

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

Closed
holasjuraj opened this issue Jun 22, 2023 · 2 comments
Closed

Throw exception instead of sys.exit in GA.__init__ #213

holasjuraj opened this issue Jun 22, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@holasjuraj
Copy link

Use-case:

Minimal example:

def run_ga(**kwargs):
    try:
        ga = pygad.GA(**kwargs)
        ga.run()
        return ga
    except Exception as e:
        my_custom_logger.print(f"GA failed with kwargs = {kwargs} with exception {e}")
        return None

my_ga = run_ga(num_generations=-1)

Desired behavior:

Program runs, and logs an exception with details about the error - i.e. that num_generations has to be positive.

Current implementation:

With the current sys.exit(-1) in GA.__init__, this will not be caught by the except block. Even if we change the except Exception to except SystemExit (which nobody should ever do), then it would log only the SystemExit with no details into my_custom_logger.

Proposed change:

In the GA.__init__, we can replace the end of the function. Instead of the current:

 except Exception as e:
    self.logger.exception(e)
    sys.exit(-1)

We can rather use:

 except Exception as e:
    self.logger.exception(e)
    raise e

This way, the exception will be logged properly into self.logger, but then it will not kill the whole python process, but rather re-raise the exception. The caller of pygad.GA() can then decide whether this exception will be left uncaught and will exit the program, or they will catch it and act upon it.

@ahmedfgad ahmedfgad added the enhancement New feature or request label Jan 28, 2024
@ahmedfgad
Copy link
Owner

Done in a recent commit 1615baf and will be supported in the next release!

@holasjuraj
Copy link
Author

Thank you. Closed as resolved.

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
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants