From d06223483386af0a4d1f24de57e6117c0b83e917 Mon Sep 17 00:00:00 2001 From: Ahmed Gad Date: Sun, 28 Jan 2024 00:55:27 -0500 Subject: [PATCH] Support object data type --- examples/example_parallel_processing.py | 39 +++++++++++++++++++++++++ pygad/pygad.py | 13 +++++---- 2 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 examples/example_parallel_processing.py diff --git a/examples/example_parallel_processing.py b/examples/example_parallel_processing.py new file mode 100644 index 0000000..9efd1ea --- /dev/null +++ b/examples/example_parallel_processing.py @@ -0,0 +1,39 @@ +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 + +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=100, + num_parents_mating=10, + sol_per_pop=20, + num_genes=len(function_inputs), + fitness_func=fitness_func, + on_generation=on_generation, + # parallel_processing=['process', 2], + parallel_processing=['thread', 2] + ) + + # Running the GA to optimize the parameters of the function. + ga_instance.run() + + # Returning the details of the best solution. + solution, solution_fitness, solution_idx = ga_instance.best_solution(ga_instance.last_generation_fitness) + print(f"Parameters of the best solution : {solution}") + print(f"Fitness value of the best solution = {solution_fitness}") + print(f"Index of the best solution : {solution_idx}") + diff --git a/pygad/pygad.py b/pygad/pygad.py index 6a6a75e..e02a9ad 100644 --- a/pygad/pygad.py +++ b/pygad/pygad.py @@ -20,9 +20,10 @@ class GA(utils.parent_selection.ParentSelection, visualize.plot.Plot): supported_int_types = [int, numpy.int8, numpy.int16, numpy.int32, numpy.int64, - numpy.uint, numpy.uint8, numpy.uint16, numpy.uint32, numpy.uint64] - supported_float_types = [ - float, numpy.float16, numpy.float32, numpy.float64] + numpy.uint, numpy.uint8, numpy.uint16, numpy.uint32, numpy.uint64, + object] + supported_float_types = [float, numpy.float16, numpy.float32, numpy.float64, + object] supported_int_float_types = supported_int_types + supported_float_types def __init__(self, @@ -726,7 +727,7 @@ def __init__(self, if self.mutation_probability is None: if not self.suppress_warnings: warnings.warn( - f"The percentage of genes to mutate (mutation_percent_genes={mutation_percent_genes}) resulted in selecting ({mutation_num_genes}) genes. The number of genes to mutate is set to 1 (mutation_num_genes=1).\nIf you do not want to mutate any gene, please set mutation_type=None.") + f"The percentage of genes to mutate (mutation_percent_genes={mutation_percent_genes}) resutled in selecting ({mutation_num_genes}) genes. The number of genes to mutate is set to 1 (mutation_num_genes=1).\nIf you do not want to mutate any gene, please set mutation_type=None.") mutation_num_genes = 1 elif type(mutation_percent_genes) in GA.supported_int_float_types: @@ -745,7 +746,7 @@ def __init__(self, if mutation_num_genes == 0: if self.mutation_probability is None: if not self.suppress_warnings: - warnings.warn(f"The percentage of genes to mutate (mutation_percent_genes={mutation_percent_genes}) resulted in selecting ({mutation_num_genes}) genes. The number of genes to mutate is set to 1 (mutation_num_genes=1).\nIf you do not want to mutate any gene, please set mutation_type=None.") + warnings.warn(f"The percentage of genes to mutate (mutation_percent_genes={mutation_percent_genes}) resutled in selecting ({mutation_num_genes}) genes. The number of genes to mutate is set to 1 (mutation_num_genes=1).\nIf you do not want to mutate any gene, please set mutation_type=None.") mutation_num_genes = 1 else: self.valid_parameters = False @@ -771,7 +772,7 @@ def __init__(self, # Based on the mutation percentage of genes, if the number of selected genes for mutation is less than the least possible value which is 1, then the number will be set to 1. if mutation_num_genes[idx] == 0: if not self.suppress_warnings: - warnings.warn(f"The percentage of genes to mutate ({mutation_percent_genes[idx]}) resulted in selecting ({mutation_num_genes[idx]}) genes. The number of genes to mutate is set to 1 (mutation_num_genes=1).\nIf you do not want to mutate any gene, please set mutation_type=None.") + warnings.warn(f"The percentage of genes to mutate ({mutation_percent_genes[idx]}) resutled in selecting ({mutation_num_genes[idx]}) genes. The number of genes to mutate is set to 1 (mutation_num_genes=1).\nIf you do not want to mutate any gene, please set mutation_type=None.") mutation_num_genes[idx] = 1 if mutation_percent_genes[0] < mutation_percent_genes[1]: if not self.suppress_warnings: