Skip to content

'mutation_type = None' not allowed #40

Closed

Description

@mathlusiverse

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.

Activity

  1. ahmedfgad commented on May 2, 2021

    @ahmedfgad
    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.

  2. self-assigned this
    on May 2, 2021
  3. added
    bug
    on May 2, 2021
  4. mathlusiverse commented on May 2, 2021

    @mathlusiverse
    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.

  5. ahmedfgad commented on May 3, 2021

    @ahmedfgad
    Owner

    Thank you.

  6. ahmedfgad commented on May 3, 2021

    @ahmedfgad
    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.

  7. ahmedfgad commented on May 19, 2021

    @ahmedfgad
    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

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions