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

Error when run parallel_processing "process" #250

Open
henryfpassagem opened this issue Nov 14, 2023 · 3 comments
Open

Error when run parallel_processing "process" #250

henryfpassagem opened this issue Nov 14, 2023 · 3 comments

Comments

@henryfpassagem
Copy link

I am getting an error when I try to run with parallel_processing = ["process", 2] the first example on the PyGad webpage. Could somebody please help me with this?

Screenshot 2023-11-14 at 16 55 25 Screenshot 2023-11-14 at 17 02 08
@ahmedfgad
Copy link
Owner

This is a working example.

import pygad
import numpy

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):
    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)

last_fitness = 0
def on_generation(ga_instance):
    global last_fitness
    print(f"Generation = {ga_instance.generations_completed}")
    print(f"Fitness    = {ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1]}")
    print(f"Change     = {ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1] - last_fitness}")
    last_fitness = ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1]

if __name__ == '__main__':
    ga_instance = pygad.GA(num_generations=num_generations,
                           num_parents_mating=num_parents_mating,
                           sol_per_pop=sol_per_pop,
                           num_genes=num_genes,
                           suppress_warnings=True,
                           parallel_processing=['process', 2],
                           fitness_func=fitness_func,
                           on_generation=on_generation)
    
    # Running the GA to optimize the parameters of the function.
    ga_instance.run()
    
    ga_instance.plot_fitness()

@steve-allwright
Copy link

Good afternoon
Thanks for this example, which I adapt to illustrate a continuation run problem that I encounter.

When I perform the :
ga_instance.run()
command repeatedly, then the continuation run works giving me generations 101-200, 201-300 etc.

However, if I include the keyword
save_best_solutions=True,
then the continuation run fails with the message :-
File ~\miniconda3\Lib\site-packages\pygad\pygad.py:1682 in cal_pop_fitness
elif (self.save_best_solutions) and (len(self.best_solutions) > 0) and (list(sol) in self.best_solutions):
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all

Is this a known problem ?
Best regards
Steve

@ahmedfgad
Copy link
Owner

In the code, I tried calling the run() method in a loop and it is still working without exceptions.

for i in range(5):
        ga_instance.run()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants