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.79k stars 451 forks source link

Save the instance of a GA during fitness evaluation #242

Open AntonisKoutounidis opened 8 months ago

AntonisKoutounidis commented 8 months ago

How can I save the instance of a GA during the fitness evaluation of the population? In other words, if it has evaluated the fitness of 15 individuals over a population of 30, I would like to save the progress and, in the future, consider only the remaining 15.

I need that cause the evaluation of fitness, in my case, is done by a resource-intensive simulation where the fitness evaluation of the whole population takes a day. So if something happens and the run is terminated before the evaluation of every individual then I lose calculations that might worth up to one day.

ahmedfgad commented 5 months ago

Saving the GA instance during the fitness evaluation will not save the fitness of the of the subset of solutions that have their fitness already calculated. At the middle of calculating the fitness, the fitness is temporarily saved. It will be saved into an instance attribute called last_generation_fitness only after the fitness is calculated for all the solutions.

But you can add a new attribute to the GA instance before saving it. This attribute saves the fitness of the subset of solution with their fitness calculated.

This is an example that saves the solutions with their fitness calculated into the solutions_fitness attribute. If you stopped the GA before calculating the fitness for all the solutions, then the fitness is saved into the solutions_fitness attribute.

import pygad
import numpy

function_inputs = [4,-2,3.5,5,-11,-4.7]
desired_output = 44

solutions_fitness = []
def fitness_func(ga_instance, solution, solution_idx):
    global solutions_fitness
    output = numpy.sum(solution*function_inputs)
    fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001)
    solutions_fitness.append(fitness)

    ga_instance.save('test')

    return fitness

def on_generation(ga_instance):
    global solutions_fitness
    print(f"Generation = {ga_instance.generations_completed}")
    solutions_fitness = []

ga_instance = pygad.GA(num_generations=50,
                       num_parents_mating=5,
                       sol_per_pop=10,
                       num_genes=len(function_inputs),
                       fitness_func=fitness_func,
                       on_generation=on_generation,
                       suppress_warnings=True)

ga_instance.run()