I am using PyGad’s GA function to optimise a neural network. As part of the testing and de-bugging I added some code to the fitness function that would save each solution and its accuracy. I’ve set the initial population to 10 and the number of solutions per population to 10.
I was expecting the fitness function to be called 10 times (once for each solution in the population less the solutions that are carried forward by keep elite and keep parents both of which I have set at 2). However, I’m finding that the fitness function is being called 26 times for the first generation and 16 time for each generation thereafter. The extra 10 calls in the first generation are obviously PyGad initialising. The fitness function is very time consuming, and I would like to limit the calls that are made per generation. Can someone explain why and what these extra calls on the fitness function are and how I can reduce them please. The ga instance is:
ga_instance = pg.GA(initial_population=initial_population,
num_generations=100,
num_parents_mating=2,
fitness_func=fitness,
sol_per_pop=10,
num_genes=18,
gene_type=GENE_TYPE,
gene_space=GENE_SPACE,
parent_selection_type=’tournament’,
K_tournament=5,
keep_parents=2,
keep_elitism=2,
save_solutions=True,
save_best_solutions=True,
crossover_type=’single_point’,
mutation_type=’adaptive’,
allow_duplicate_genes=True,
mutation_num_genes=[4,2],
stop_criteria=’saturate_5’,
on_generation=callback_gen,
suppress_warnings=False)
Your code is OK. The issue is about adaptive mutation. First it calls the fitness function for the offspring. Then inside the evolution loop, the fitness function is called again for the same offspring. Thus, increasing the number of calls.
This issue will be fixed in a later release of PyGAD.