How to save specific solutions based on conditions each generation #251
Unanswered
maxgombair
asked this question in
Q&A
Replies: 1 comment
-
Here is an example but it uses a global variable. It creates a list named import pygad
import numpy
function_inputs = [4,-2,3.5,5,-11,-4.7]
desired_output = 44
def fitness_func(ga_instance, solution, solution_idx):
output = numpy.sum(solution*function_inputs)
fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001)
return fitness
filtered_solutions = []
def on_generation(ga_instance):
global filtered_solutions
print(f"Generation = {ga_instance.generations_completed}")
for idx in range(ga_instance.population.shape[0]):
solution = ga_instance.population[idx]
fitness = ga_instance.last_generation_fitness[idx]
if 10 < fitness < 20:
filtered_solutions.append(solution)
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() |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, I would like to save solutions at each generation based on a selection criteria that is different from the fitness function.
The goal of this process is to study some specific solutions, it has nothing to do with parents selection.
I know that there is the save_solutions argument but its take to much time to execute the model while saving all the solutions.
So my question is:
is there a way to filter between each generation the solutions i'm interrested and save them in an array ?
I've fought about using the on_generation argument but i'm struggling to implement this, bc it needs somme kind of global variable.
Beta Was this translation helpful? Give feedback.
All reactions