-
-
Notifications
You must be signed in to change notification settings - Fork 481
when set mutation_type="adaptive", it ran into error. #65
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
Comments
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 ([email protected]) if this is better for you. |
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
<#65 (comment)>,
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>.
|
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? |
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
<#65 (comment)>,
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))
|
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
> <#65 (comment)>,
> 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>.
>
>
|
Thanks for opening this issue. I figured out where is the problem which is related to the 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. |
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
<#65 (comment)>,
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>.
|
1. Reuse the fitness of previously explored solutions rather than recalculating them. This feature only works if `save_solutions=True`. 2. The user can use the `tqdm` library to show a progress bar. #50 ```python 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 num_generations = 10000 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=lambda _: pbar.update(1)) ga_instance.run() ga_instance.plot_result() ``` 3. Solved the issue of unequal length between the `solutions` and `solutions_fitness` when the `save_solutions` parameter is set to `True`. Now, the fitness of the last population is appended to the `solutions_fitness` array. #64 4. There was an issue of getting the length of these 4 variables (`solutions`, `solutions_fitness`, `best_solutions`, and `best_solutions_fitness`) doubled after each call of the `run()` method. This is solved by resetting these variables at the beginning of the `run()` method. #62 5. Bug fixes when adaptive mutation is used (`mutation_type="adaptive"`). #65
The text was updated successfully, but these errors were encountered: