Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to save specific solutions based on conditions each generation #251

Unanswered
maxgombair asked this question in Q&A
Discussion options

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.

You must be logged in to vote

Replies: 1 comment

Comment options

Here is an example but it uses a global variable. It creates a list named filtered_solutions which only filters the solutions that have a fitness between 10 and 20, exclusive.

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()
You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested

AltStyle によって変換されたページ (->オリジナル) /