Accessing population of previous generation in fitness_func
I am trying to find a way to access the entire population of the previous generation (not just the calculated fitness or kept parents) to use in the evaluation of fitness in each generation. Is this possible in the current release?
There is no property that saves the population in the previous generation is not saved. But you can still access it. Here are 2 ways.
First way
- Set
save_solutions=Truein the constructor of thepygad.GAclass. This saves the solutions from each population in thesolutionsproperty. - For each generation, find the index of the first and last solution in the population. Let's assume the indices are
xandy. - Return the last generation's population:
ga_instance.solutions[x:y, :]
If you have 10 solutions per population, then the first and last solution indices are:
0and9for the first generation.10and19for the second generation.20and29for the third generation.
And so on.
Second way
Create a new property in the ga_instance attribute that saves the previous generation's population. This can be done at the end of each generation inside the on_generation() callback function/method.
Hope this helps.
Does pygad automatically skipping evaluate solution that has been calculated from previous solution?
Does pygad automatically skipping evaluate solution that has been calculated from previous solution?
@yasirroni, PyGAD skips evaluation the fitness for a previous solution if any of these conditions hold:
- The solution is a parent in the last population. The
keep_parentsparameter must be-1or a positive integer. - The solution is an elitism in the last population. The
keep_elitismparameter must be a positive integer. - The solution exists in the
self.solutionslist given thatsave_solutions=True. - The solution exists in the
self.best_solutionslist given thatsave_best_solutions=True.
@ahmedfgad Adding this kind of information to the docs seems good I think. Thanks for clarifying.
The documentation is already updated to reflect this: https://github.com/ahmedfgad/GeneticAlgorithmPython/commit/d3b9ec0533d0d87b91298e8db1a6852947c93570
Vote to close this issue.