-
-
Notifications
You must be signed in to change notification settings - Fork 489
-
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):
...
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 3 comments 6 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.
Beta Was this translation helpful? Give feedback.
All reactions
-
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()
Beta Was this translation helpful? Give feedback.
All reactions
-
❤️ 2
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
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é
Beta Was this translation helpful? Give feedback.
All reactions
-
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")
Beta Was this translation helpful? Give feedback.
All reactions
-
Thanks, that's perfect and works like a charm.
André
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
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
Beta Was this translation helpful? Give feedback.
All reactions
-
❤️ 3
-
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1