Skip to content

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

Open
Rakesh-Raushan opened this issue Oct 6, 2021 · 6 comments
Labels
bug Something isn't working

Comments

@Rakesh-Raushan
Copy link

~/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:

num_generations = 1500
num_parents_mating = 20

sol_per_pop = 50
num_genes = num_customers
gene_type = int

init_range_low = 0
init_range_high = 6
gene_space= np.arange(6)

parent_selection_type = "sss"
keep_parents = 20

crossover_type = "single_point"

mutation_type = "random"
mutation_num_genes= [3, 1]
mutation_probability = [0.25, 0.1]
mutation_percent_genes = [20,10]

# create an instance of the pygad.GA class 
global ga_instance
ga_instance = pygad.GA(num_generations=num_generations,
                   fitness_func=fitness_func,
                   num_parents_mating=4,
                       gene_space = gene_space,
                   sol_per_pop=50,
                   num_genes=num_genes,
                    gene_type = gene_type,
                   mutation_type="adaptive",
                   mutation_num_genes=(3, 1),
                      save_solutions= True)

ga_instance.run()
@ahmedfgad
Copy link
Owner

Hi @Rakesh-Raushan,

Could you share the complete code to debug the error?

@jhsbo
Copy link

jhsbo commented Dec 29, 2021

This is just a guess. Make sure you are not returning an invalid number from your fitness_func. I was having the same issue and the cause was I am using a C++ library to calculate the fitness and in somewhere in the code I was dividing by zero generating an invalid float. Try to print all the values that come out of your fitness_func.

Example of a case where nan returned from the fitness function:
Example

pop_fitness is an array. If one of its values is nan, numpy.where(pop_fitness == numpy.max(pop_fitness)) returns an empty array.

@ahmedfgad
Copy link
Owner

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.

ahmedfgad added a commit that referenced this issue Feb 3, 2022
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
@ahmedfgad ahmedfgad added the bug Something isn't working label Feb 25, 2023
@ViniCL
Copy link

ViniCL commented Sep 17, 2023

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:
Class: GA
Method: best_solution
Line:
best_match_idx = numpy.where(
pop_fitness == numpy.max(pop_fitness))[0][0]

@garncarz
Copy link

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):

  File "/home/ondra/.local/lib/python3.10/site-packages/pygad/pygad.py", line 1673, in cal_pop_fitness
    fitness = self.best_solutions_fitness[solution_idx]
IndexError: list index out of range
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> /home/ondra/.local/lib/python3.10/site-packages/pygad/pygad.py(1673)cal_pop_fitness()
   1672                         solution_idx = self.best_solutions.index(list(sol))
-> 1673                         fitness = self.best_solutions_fitness[solution_idx]
   1674                     elif (self.keep_elitism > 0) and (self.last_generation_elitism is not None) and (len(self.last_generation_elitism) > 0) and (list(sol) in last_generation_elitism_as_list):

ipdb> solution_idx
1000
ipdb> len(self.best_solutions_fitness)
1000

It seems the error is in the value of solution_idx, is it computed on reload or is it saved?

Actually, most of the time reloading and rerunning works ok.

@garncarz
Copy link

Oh, I can see:

ipdb> len(self.best_solutions)
1001

So it seems there's more saved best solutions than best solutions fitness values. I'm saving the GA instance in the on_generation callback after each generation is finished.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants