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

fitness function is being saved? #263

Open
acidnik opened this issue Jan 15, 2024 · 2 comments
Open

fitness function is being saved? #263

acidnik opened this issue Jan 15, 2024 · 2 comments
Labels
question Further information is requested

Comments

@acidnik
Copy link

acidnik commented Jan 15, 2024

import numpy
import pygad


function_inputs = [4, -2, 3.5, 5, -11, -4.7]  # Function inputs.
desired_output = 44  # Function output.


def fitness_func(ga_instance, solution, solution_idx):
    # XXX UNCOMMENT THIS
    # print('TEST TEST TEST')
    output = numpy.sum(solution * function_inputs)
    fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001)
    return fitness


num_generations = 100  # Number of generations.
num_parents_mating = 10  # Number of solutions to be selected as parents in the mating pool.

sol_per_pop = 20  # Number of solutions in the population.
num_genes = len(function_inputs)


# Running the GA to optimize the parameters of the function.
try:
    ga_instance = pygad.load('test')
except:
    ga_instance = pygad.GA(
        num_generations=1,
        num_parents_mating=num_parents_mating,
        sol_per_pop=sol_per_pop,
        num_genes=num_genes,
        fitness_func=fitness_func,
    )
for _ in range(10):
    ga_instance.run()
    ga_instance.save('test')

steps to reproduce:

  1. run this file
  2. uncomment the line marked with XXX
  3. run this file again

expected results:
TEST TEST TEST being printed

actual results:
nothing happens

does this means that the code of fitness function is saved too?
is there a way to update my code after the ga was saved?

@acidnik
Copy link
Author

acidnik commented Jan 15, 2024

found the workaround myself:

try:
    ga_instance = pygad.load('test')
    ga_instance.fitness_func = fitness_func

still, would be nice to reflect this in documentation

@ahmedfgad ahmedfgad added the question Further information is requested label Jan 27, 2024
@ahmedfgad
Copy link
Owner

PyGAD uses the cloudpickle library which is able to pickle not only the objects but also the functions. So, yes the fitness function is pickled.

When you run the code for the first time with the commented print statement, then the cloudpickle library saves the fitness function with the print line commented.

After loading the saved cloudpickle object, it loads the saved fitness function with the commented print statement.

As you suggested, you can force change the fitness function by setting the fitness_func attribute.

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