Index error on running GA, looks like best match idx is looked in a zero size array, what can be the reason?
~/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()
Hi @Rakesh-Raushan,
Could you share the complete code to debug the error?
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:

pop_fitness is an array. If one of its values is nan, numpy.where(pop_fitness == numpy.max(pop_fitness)) returns an empty array.
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.
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]