-
-
Notifications
You must be signed in to change notification settings - Fork 481
Method best_solution() does not always return correct solution_idx #144
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
I have a example where best_solution() as far as I can tell return a random index, but most of the time 0. Furthermore, saving the best index by hand in ff results in wrong results as well. I am stumped. I am pretty new to machine learning, so please forgive me if my code is ridiculous in any way, and tell me where I went wrong. Any help is greatly appreciated.
|
@borisarloff hi, you talked about the solution_idx, in my test, the solution and solution fitness could also be wrong. For example, with stochastic fitness function, we have a such setting:
generation 0: [0.5,0.5,0.5,0.4] fitness of the best_solution() be [0.6] rather than [0.8], I am confused. |
Could you share a code sample please? Just to replicate the issue on my end. |
@ahmedfgad hi, I guess due to the same problem you have answered here. I think the multiple times of calculations for the same solution will influence my best_fitness across the generation because I am using the stochastic fitness function. By the way, when can I use the correct version? Thanks a lot. |
This would be the reason why you have this behavior. Please give a try when the new release is published. |
problem solved in Pygad new release (2.19.2) yes, here is the output of
Here, 0.7557456777209327 is not kept to the end. Same problem here |
In the event where multiple best solutions happen to have the same best fitness value, then best_solution() returns the first generation it finds with that same high fitness value. That index however does not necessarily correspond to the returned best solution.
A work-around could be to retrieve the index of the best solution, rather than relying on best_solution() for that index. However, this could also fail when in the rare case of more than one generation with a same best solution. It would be unclear as to which index is being returned. On the other hand, such would not matter when the fitness is the same best fitness with a deterministic and not a stochastic fitness function (FF).
To reproduce, create a FF which repeatedly generates same few fitness values. Call best_solution() and compare with:
print(f"Generation: {ga_instance.generations_completed} Best solution fitness: {ga_inst.best_solution()[1]}") output from on_generation callback function.
The text was updated successfully, but these errors were encountered: