ahmedfgad / GeneticAlgorithmPython

Source code of PyGAD, a Python 3 library for building the genetic algorithm and training machine learning algorithms (Keras & PyTorch).
https://pygad.readthedocs.io
BSD 3-Clause "New" or "Revised" License
1.89k stars 465 forks source link

Request: attributes of best solution from all generations #107

Open Stoops-ML opened 2 years ago

Stoops-ML commented 2 years ago

From PyGAD's documentation the best_solutions attribute saves the best solution of every generation:

save_best_solutions=False: When True, then the best solution after each generation is saved into an attribute named best_solutions. If False (default), then no solutions are saved and the best_solutions attribute will be empty. Supported in PyGAD 2.9.0.

I would like an attribute to exist which would be the single best solution from all generations, rather than the existing best_solutions attribute that is a list of the best solutions from every generation. This would be helpful because the single best solution is not always the best solution of the last generation.

Something on the lines of (untested example):

def get_best_solution(self):
    best_gen = np.argmax(self.best_solutions_fitness)
    return self.best_solutions[best_gen]
DCML685 commented 2 years ago

I'm also interested in this.

ahmedfgad commented 1 year ago

You can also call the best_solution() method which returns the best solution from all generations.