Skip to content

Accept user defined functions for crossover and mutate #50

b8raoult started this conversation in Ideas
Accept user defined functions for crossover and mutate #50
Jun 18, 2021 · 1 comments · 5 replies

Accept that parameters mutation_type or crossover_type (and even parent_selection_type) are callable instead of just str.

Line 339:

if (crossover_type is None):            
    self.crossover = None       
elif callable(crossover_type):
     self.crossover = crossover_type
elif not (type(crossover_type) is str):
  ...

Replies

1 comment
·
5 replies

Thanks for opening this conversation.

PyGAD 2.16.0 is released right now to support this feature.

This is the release note:
A user-defined function can be passed to the mutation_type, crossover_type, and parent_selection_type parameters in the pygad.GA class to create a custom mutation, crossover, and parent selection operators. Check the User-Defined Crossover, Mutation, and Parent Selection Operators section in the documentation: https://pygad.readthedocs.io/en/latest/README_pygad_ReadTheDocs.html#user-defined-crossover-mutation-and-parent-selection-operators

The example_custom_operators.py script in the GitHub project gives an example of building and using custom functions for the 3 operators.

Please let me know if you have any other feature to be supported.

5 replies
@b8raoult

Thanks, that is great. You may also want to add to the documentation the integration with tqdm.

from tqdm import tqdm

num_generations = 100
with tqdm(total=num_generations) as pbar:
    ga_instance = pygad.GA(
        num_generations=num_generations,
        ...,
        on_generation=lambda _: pbar.update(1),
    )
    ga_instance.run()
@ahmedfgad

That is really helpful! Thank you.

I will definitely refer to this discussion in the documentation. It is so fast in monitoring the progress compared to printing the the generation number in a new function.

Please let me know if you have any other suggestion or bugs to fix.

@dayd

Hi & thanks for this great tool.
The Progress bar is a great feature but when using the progress bar i can no longer save the results with the save() fct.
I get this error message:
pickle.dump(self, file)
_pickle.PicklingError: Can't pickle <function at 0x1232a59d0>: attribute lookup on main failed

Regards
André

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet
3 participants