Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save the instance of a GA during fitness evaluation #242

Open
AntonisKoutounidis opened this issue Nov 3, 2023 · 1 comment
Open

Save the instance of a GA during fitness evaluation #242

AntonisKoutounidis opened this issue Nov 3, 2023 · 1 comment
Labels
question Further information is requested

Comments

@AntonisKoutounidis
Copy link

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 ahmedfgad added the question Further information is requested label Jan 27, 2024
@ahmedfgad
Copy link
Owner

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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants