Open
Description
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:
- run this file
- uncomment the line marked with XXX
- 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?