Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Accept user defined functions for crossover and mutate #50

Closed
b8raoult started this conversation in Ideas
Discussion options

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):
 ...
You must be logged in to vote

Replies: 3 comments 6 replies

Comment options

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.

You must be logged in to vote
5 replies
Comment options

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()
Comment options

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.

Comment options

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é

Comment options

Thanks @dayd! The issue is solved by defining a function. The following code works:

import pygad
import numpy
import tqdm
equation_inputs = [4,-2,3.5]
desired_output = 44
def fitness_func(solution, solution_idx):
 output = numpy.sum(solution * equation_inputs)
 fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001)
 return fitness
def on_generation_progress(ga):
 pbar.update(1)
num_generations = 100
with tqdm.tqdm(total=num_generations) as pbar:
 ga_instance = pygad.GA(num_generations=num_generations,
 sol_per_pop=5,
 num_parents_mating=2,
 num_genes=len(equation_inputs),
 fitness_func=fitness_func,
 on_generation=on_generation_progress)
 ga_instance.run()
ga_instance.plot_result()
ga_instance.save("test")
Comment options

Thanks, that's perfect and works like a charm.
André

Comment options

Hello, I am using the PyGAD library for the clustering task. I wanted to use a custom crossover operator, therefore I tried checking the ling https://pygad.readthedocs.io/en/latest/README_pygad_ReadTheDocs.html#user-defined-crossover-mutation-and-parent-selection-operators, though it seems it got deleted. Was it move somewhere else?
Please, let me know if there are any examples for the custom cross over operators.
Thank you very much.

You must be logged in to vote
1 reply
Comment options

I assume that you would have found it already, but since I was looking for the same when arrived to this thread, I leave the link for the future:
https://pygad.readthedocs.io/en/latest/utils.html#user-defined-crossover-mutation-and-parent-selection-operators

Comment options

Thank you very much for the support. Antonio Politano Da: priack ***@***.***> Inviato: venerdì 5 gennaio 2024 12:15 A: ahmedfgad/GeneticAlgorithmPython ***@***.***> Cc: POLITANO ANTONIO ***@***.***>; Comment ***@***.***> Oggetto: Re: [ahmedfgad/GeneticAlgorithmPython] Accept user defined functions for crossover and mutate (Discussion #50) I assume that you would have found it already, but since I was looking for the same when arrived to this thread, I leave the link for the future: https://pygad.readthedocs.io/en/latest/utils.html#user-defined-crossover-mutation-and-parent-selection-operators - Reply to this email directly, view it on GitHub<#50 (reply in thread)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/ANITL4OJXLRYNXVYMVBKPP3YM7OERAVCNFSM465MQ2JKU5DIOJSWCZC7NNSXTOKENFZWG5LTONUW63SDN5WW2ZLOOQ5TQMBSGIYDSOI>. You are receiving this because you commented.Message ID: ***@***.***>
You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /