Skip to content

'mutation_type = None' not allowed #40

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
mathlusiverse opened this issue May 2, 2021 · 5 comments
Closed

'mutation_type = None' not allowed #40

mathlusiverse opened this issue May 2, 2021 · 5 comments
Assignees
Labels
bug Something isn't working

Comments

@mathlusiverse
Copy link

I did not use mutation, pygad print the following warning:

If you do not want to mutate any gene, please set mutation_type=None.

But when I set mutation_type=None, or mutation_type="None", then pygad crashed:

  File "C:\My\MyPythonProject\GeneticAlgo\venv\lib\site-packages\pygad\pygad.py", line 282, in __init__
    raise TypeError("The expected type of the 'mutation_type' parameter is str but ({mutation_type}) found.".format(mutation_type=type(mutation_type)))
TypeError: The expected type of the 'mutation_type' parameter is str but (<class 'NoneType'>) found.

I looked at the source code, it seems that line 280 does not allow the possibility of mutation_type being None. Yet line 295 does allow such possibility.

ahmedfgad added a commit that referenced this issue May 2, 2021
Solves issue #40 when either crossover_type and mutation_type is set to None: #40
@ahmedfgad
Copy link
Owner

Thank you. The issue with assigning None values for crossover_type and mutation_type is solved. The updated pygad.py module is available at GitHub and a new release will be published to PyPI later. Please try the updated module if there a chance.

@ahmedfgad ahmedfgad self-assigned this May 2, 2021
@ahmedfgad ahmedfgad added the bug Something isn't working label May 2, 2021
@mathlusiverse
Copy link
Author

Thanks, @ahmedfgad for the quick response! Please keep up the good work.

I will test it after you make it available on your web page.

@ahmedfgad
Copy link
Owner

Thank you.

@ahmedfgad
Copy link
Owner

Hi @ahmedfgad thanks a lot for this wonderful tool. I tried to run the example code from

/home/sikari/Desktop/temp_proj_pygad/PYGAD/pygad.py:374: UserWarning: The percentage of genes to mutate (mutation_percent_genes=10) resutled in selecting (0) genes. The number of genes to mutate is set to 1 (mutation_num_genes=1).
If you do not want to mutate any gene, please set mutation_type=None.
if not self.suppress_warnings: warnings.warn("The percentage of genes to mutate (mutation_percent_genes={mutation_percent}) resutled in selecting ({mutation_num}) genes. The number of genes to mutate is set to 1 (mutation_num_genes=1).\nIf you do not want to mutate any gene, please set mutation_type=None.".format(mutation_percent=mutation_percent_genes, mutation_num=mutation_num_genes))

solution, solution_fitness, solution_idx = ga_instance.best_solution()
print("Parameters of the best solution : {solution}".format(solution=solution))
Parameters of the best solution : [ 4.46054413 -0.56253708 -0.58246418 1.96274254 -2.26517722 1.62457641]
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
Fitness value of the best solution = 42.05377974846767

`

Hi @asishadhikari ,

This is a normal warning and you can supress all warnings by setting suppress_warnings=True.

The warning is returned because you use the default of mutating 10% of the genes (mutation_percent_genes=10). When you have few genes like only 2, then 10% means 0.2 genes which is rounded to 0. This means you are mutating 0 genes. With that situation, the number of genes is raised to 1 automatically.

This warning tells you that if you intentionally want to mutate no genes, then it is better to set mutation_type=None. Otherwise, there will be 1 gene mutated.

Please let me know if I should clarify something.

ahmedfgad added a commit that referenced this issue May 19, 2021
Changes in PyGAD 2.14.0
1.	Issue #40 is solved. Now, the None value works with the crossover_type and mutation_type parameters: #40
2.	The gene_type parameter supports accepting a list/tuple/numpy.ndarray of numeric data types for the genes. This helps to control the data type of each individual gene. Previously, the gene_type can be assigned only to a single data type that is applied for all genes.
3.	A new bool attribute named gene_type_single is added to the pygad.GA class. It is True when there is a single data type assigned to the gene_type parameter. When the gene_type parameter is assigned a list/tuple/numpy.ndarray, then gene_type_single is set to False.
4.	The mutation_by_replacement flag now has no effect if gene_space exists except for the genes with None values. For example, for gene_space=[None, [5, 6]] the mutation_by_replacement flag affects only the first gene which has None for its value space.
5.	When an element has a value of None in the gene_space parameter (e.g. gene_space=[None, [5, 6]]), then its value will be randomly generated for each solution rather than being generate once for all solutions. Previously, the gene with None value in gene_space is the same across all solutions
6.	Some changes in the documentation according to issue #32: #32
ahmedfgad added a commit that referenced this issue May 19, 2021
1.	Issue #40 is solved. Now, the None value works with the crossover_type and mutation_type parameters: #40
2.	The gene_type parameter supports accepting a list/tuple/numpy.ndarray of numeric data types for the genes. This helps to control the data type of each individual gene. Previously, the gene_type can be assigned only to a single data type that is applied for all genes.
3.	A new bool attribute named gene_type_single is added to the pygad.GA class. It is True when there is a single data type assigned to the gene_type parameter. When the gene_type parameter is assigned a list/tuple/numpy.ndarray, then gene_type_single is set to False.
4.	The mutation_by_replacement flag now has no effect if gene_space exists except for the genes with None values. For example, for gene_space=[None, [5, 6]] the mutation_by_replacement flag affects only the first gene which has None for its value space.
5.	When an element has a value of None in the gene_space parameter (e.g. gene_space=[None, [5, 6]]), then its value will be randomly generated for each solution rather than being generate once for all solutions. Previously, the gene with None value in gene_space is the same across all solutions
6.	Some changes in the documentation according to issue #32: #32
@ahmedfgad
Copy link
Owner

This issue is solved in the new release of PyGAD 2.14.0.
Thank you.

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