-
-
Notifications
You must be signed in to change notification settings - Fork 481
Index error on running GA, looks like best match idx is looked in a zero size array, what can be the reason? #67
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 @Rakesh-Raushan, Could you share the complete code to debug the error? |
Thanks @jhonasb! From your side, you may validate the value returned from the fitness function. From my side, the calculated fitness value should be validated. Maybe an exception would be raised if something is wrong. |
PyGAD 2.16.2 1. A new instance attribute called `previous_generation_fitness` added in the `pygad.GA` class. It holds the fitness values of one generation before the fitness values saved in the `last_generation_fitness`. 2. Issue in the `cal_pop_fitness()` method in getting the correct indices of the previous parents. This is solved by using the previous generation's fitness saved in the new attribute `previous_generation_fitness` to return the parents' fitness values. Thanks to Tobias Tischhauser (M.Sc. - [Mitarbeiter Institut EMS, Departement Technik, OST – Ostschweizer Fachhochschule, Switzerland](https://www.ost.ch/de/forschung-und-dienstleistungen/technik/systemtechnik/ems/team)) for detecting this bug. PyGAD 2.16.3 1. Validate the fitness value returned from the fitness function. An exception is raised if something is wrong. #67
This error occurs because of Nan or 0 values within GA. I solved this using Pandas' fillna(-1) method. For example. In the code, this error is thrown at: |
Great library, just this error seems to be happening to me too, after saving a PyGAD instance with 1000 generations (it exited normally), and reloading it, it ends up with this (trying to use ipdb to solve it):
It seems the error is in the value of Actually, most of the time reloading and rerunning works ok. |
Oh, I can see:
So it seems there's more saved best solutions than best solutions fitness values. I'm saving the GA instance in the |
~/pypi_local/pygad/pygad.py in run(self)
1261 self.last_generation_fitness = self.cal_pop_fitness()
1262
-> 1263 best_solution, best_solution_fitness, best_match_idx = self.best_solution(pop_fitness=self.last_generation_fitness)
1264
1265 # Appending the best solution in the current generation to the best_solutions list.
~/pypi_local/pygad/pygad.py in best_solution(self, pop_fitness)
3115 pop_fitness = self.cal_pop_fitness()
3116 # Then return the index of that solution corresponding to the best fitness.
-> 3117 best_match_idx = numpy.where(pop_fitness == numpy.max(pop_fitness))[0][0]
3118
3119 best_solution = self.population[best_match_idx, :].copy()
IndexError: index 0 is out of bounds for axis 0 with size 0
This is how I have created my GA instance before run:
The text was updated successfully, but these errors were encountered: