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

How to get multiple best solutions in a generation? #304

Open
JimmyChine opened this issue Nov 20, 2024 · 1 comment
Open

How to get multiple best solutions in a generation? #304

JimmyChine opened this issue Nov 20, 2024 · 1 comment
Labels
question Further information is requested

Comments

@JimmyChine
Copy link

Hi All,

I am a new on in PyGAD, and now I am using PyGAD to implement my multiple objective optimization problem. In your example, I learned how to the funcion on_generation() to get one best fitness of each generation:

def on_generation(ga_instance):
global last_fitness
print("Generation = {generation}".format(generation=ga_instance.generations_completed))
print("Fitness = {fitness}".format(fitness=ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1]))
print("Change = {change}".format(change=ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1] - last_fitness))
solution, solution_fitness, solution_idx = ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)

My question is that: can I get multiple best solutions (for example, I want to get best three solutions) in PyGAD?

Any suggestions would be greatly appreciated. Thanks

@ahmedfgad
Copy link
Owner

This can be achieved by calling the pygad.utils.ParentSelection.steady_state_selection() method.

class ParentSelection:

    def __init__():
        pass

    def steady_state_selection(self, fitness, num_parents):
        ...
    return parents, numpy.array(fitness_sorted[:num_parents])

If you need the top 3 solutions, you can set num_parents=3. The method returns the selected parents and their fitness.

@ahmedfgad ahmedfgad added the question Further information is requested label Jan 7, 2025
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