ahmedfgad / GeneticAlgorithmPython

Source code of PyGAD, a Python 3 library for building the genetic algorithm and training machine learning algorithms (Keras & PyTorch).
https://pygad.readthedocs.io
BSD 3-Clause "New" or "Revised" License
1.89k stars 465 forks source link

when set mutation_type="adaptive", it ran into error. #65

Open DailiZhang2010 opened 3 years ago

DailiZhang2010 commented 3 years ago

code error_message

ahmedfgad commented 3 years ago

Hi @DailiZhang2010,

The problem is related to the fitness array. I cannot figure out the problem from the screenshot. Is it possible to share the code? You are welcome to send it by email (ahmed.f.gadd@gmail.com) if this is better for you.

DailiZhang2010 commented 3 years ago

Hi Ahmed, Thanks a lot for the response. For the actual application I am developing, it involves quite some data reading, so I copied the sample on PyGAD doc site, but changed two lines. Please see the attached file. mutation_type = "adaptive" mutation_num_genes=(3, 1) It ran to the same error. Please see the attached file. Best regards, Daili [image: error_log.PNG]

On Tue, Sep 28, 2021 at 12:23 PM Ahmed Gad @.***> wrote:

Hi @DailiZhang2010 https://github.com/DailiZhang2010,

The problem is related to the fitness array. I cannot figure out the problem from the screenshot. Is it possible to share the code? You are welcome to send it by email @.***) if this is better for you.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/ahmedfgad/GeneticAlgorithmPython/issues/65#issuecomment-929453982, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHPLYELU25PBTGWOI7FHG2TUEH2ZXANCNFSM5ESIPFOA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

ahmedfgad commented 3 years ago

I already tested the sample code posted in the documentation with the following 2 lines and everything works well.

mutation_type = "adaptive"
mutation_num_genes=(3, 1)

The problem exists in the fitness function. For the error case, could you check if the fitness function returns a single number?

DailiZhang2010 commented 3 years ago

hmm, thanks. I guess the .py file was not attached. Here is the example I used to test the adaptive mutation_type. Regards, Daili

On Tue, Sep 28, 2021 at 4:59 PM Ahmed Gad @.***> wrote:

I already tested the sample code posted in the documentation with the following 2 lines and everything works well.

mutation_type = "adaptive"mutation_num_genes=(3, 1)

The problem exists in the fitness function. For the error case, could you check if the fitness function returns a single number?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/ahmedfgad/GeneticAlgorithmPython/issues/65#issuecomment-929656322, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHPLYEKJIFRXVT2SCUXHBP3UEI3EFANCNFSM5ESIPFOA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

import numpy import pygad

function_inputs = [4,-2,3.5,5,-11,-4.7] desired_output = 44

def fitness_func(solution, solution_idx): output = numpy.sum(solution*function_inputs) fitness = 1.0 / numpy.abs(output - desired_output) return fitness

fitness_function = fitness_func

num_generations = 50 num_parents_mating = 4

sol_per_pop = 8 num_genes = len(function_inputs)

init_range_low = -2 init_range_high = 5

parent_selection_type = "sss" keep_parents = 1

crossover_type = "single_point"

mutation_type = "adaptive" mutation_num_genes=(3, 1)

mutation_type = "random"

mutation_percent_genes = 10

ga_instance = pygad.GA(num_generations=num_generations, num_parents_mating=num_parents_mating, fitness_func=fitness_function, sol_per_pop=sol_per_pop, num_genes=num_genes, init_range_low=init_range_low, init_range_high=init_range_high, parent_selection_type=parent_selection_type, keep_parents=keep_parents, crossover_type=crossover_type, mutation_type=mutation_type, mutation_num_genes=mutation_num_genes)

ga_instance.run()

solution, solution_fitness, solution_idx = ga_instance.best_solution() print("Parameters of the best solution : {solution}".format(solution=solution)) print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))

prediction = numpy.sum(numpy.array(function_inputs)*solution) print("Predicted output based on the best solution : {prediction}".format(prediction=prediction))

DailiZhang2010 commented 3 years ago

Ahmed, I forgot to mention that if I change the mutation_type="random", it works well. And the error message basically says the # of offspring is more than the ( sol_per_pop-num_parents_mating) . Thanks, Daili

On Tue, Sep 28, 2021 at 5:18 PM Daili Zhang @.***> wrote:

hmm, thanks. I guess the .py file was not attached. Here is the example I used to test the adaptive mutation_type. Regards, Daili

On Tue, Sep 28, 2021 at 4:59 PM Ahmed Gad @.***> wrote:

I already tested the sample code posted in the documentation with the following 2 lines and everything works well.

mutation_type = "adaptive"mutation_num_genes=(3, 1)

The problem exists in the fitness function. For the error case, could you check if the fitness function returns a single number?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/ahmedfgad/GeneticAlgorithmPython/issues/65#issuecomment-929656322, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHPLYEKJIFRXVT2SCUXHBP3UEI3EFANCNFSM5ESIPFOA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

ahmedfgad commented 3 years ago

@DailiZhang2010,

Thanks for opening this issue. I figured out where is the problem which is related to the keep_parents parameter. When all parents are used in the next population (i.e. keep_parents=-1), things work well. When the number of kept parents is less than the number of mating parents, the problem arises.

It is solved now.

The project will be updated soon and a new release of PyGAD will be published. Please have a try when the new release is published and get back if you found any bugs.

DailiZhang2010 commented 3 years ago

Cool. Thanks a lot for such an awesome package and it helped my project a lot. Regards, Daili

On Tue, Sep 28, 2021, 7:00 PM Ahmed Gad @.***> wrote:

@DailiZhang2010 https://github.com/DailiZhang2010,

Thanks for opening this issue. I figured out where is the problem. It is solved now.

The project will be updated soon and a new release of PyGAD will be published. Please have a try when the new release is published and get back if you found any bugs.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/ahmedfgad/GeneticAlgorithmPython/issues/65#issuecomment-929708379, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHPLYELTP3KLV7NP2W2UDHLUEJJI3ANCNFSM5ESIPFOA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.