Skip to content

Commit d062234

Browse files
committed
Support object data type
1 parent 08c0840 commit d062234

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import pygad
2+
import numpy
3+
4+
function_inputs = [4,-2,3.5,5,-11,-4.7] # Function inputs.
5+
desired_output = 44 # Function output.
6+
7+
def fitness_func(ga_instance, solution, solution_idx):
8+
output = numpy.sum(solution*function_inputs)
9+
fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001)
10+
return fitness
11+
12+
last_fitness = 0
13+
def on_generation(ga_instance):
14+
global last_fitness
15+
print(f"Generation = {ga_instance.generations_completed}")
16+
print(f"Fitness = {ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1]}")
17+
print(f"Change = {ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1] - last_fitness}")
18+
last_fitness = ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1]
19+
20+
if __name__ == '__main__':
21+
ga_instance = pygad.GA(num_generations=100,
22+
num_parents_mating=10,
23+
sol_per_pop=20,
24+
num_genes=len(function_inputs),
25+
fitness_func=fitness_func,
26+
on_generation=on_generation,
27+
# parallel_processing=['process', 2],
28+
parallel_processing=['thread', 2]
29+
)
30+
31+
# Running the GA to optimize the parameters of the function.
32+
ga_instance.run()
33+
34+
# Returning the details of the best solution.
35+
solution, solution_fitness, solution_idx = ga_instance.best_solution(ga_instance.last_generation_fitness)
36+
print(f"Parameters of the best solution : {solution}")
37+
print(f"Fitness value of the best solution = {solution_fitness}")
38+
print(f"Index of the best solution : {solution_idx}")
39+

pygad/pygad.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ class GA(utils.parent_selection.ParentSelection,
2020
visualize.plot.Plot):
2121

2222
supported_int_types = [int, numpy.int8, numpy.int16, numpy.int32, numpy.int64,
23-
numpy.uint, numpy.uint8, numpy.uint16, numpy.uint32, numpy.uint64]
24-
supported_float_types = [
25-
float, numpy.float16, numpy.float32, numpy.float64]
23+
numpy.uint, numpy.uint8, numpy.uint16, numpy.uint32, numpy.uint64,
24+
object]
25+
supported_float_types = [float, numpy.float16, numpy.float32, numpy.float64,
26+
object]
2627
supported_int_float_types = supported_int_types + supported_float_types
2728

2829
def __init__(self,
@@ -726,7 +727,7 @@ def __init__(self,
726727
if self.mutation_probability is None:
727728
if not self.suppress_warnings:
728729
warnings.warn(
729-
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.")
730+
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.")
730731
mutation_num_genes = 1
731732

732733
elif type(mutation_percent_genes) in GA.supported_int_float_types:
@@ -745,7 +746,7 @@ def __init__(self,
745746
if mutation_num_genes == 0:
746747
if self.mutation_probability is None:
747748
if not self.suppress_warnings:
748-
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.")
749+
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.")
749750
mutation_num_genes = 1
750751
else:
751752
self.valid_parameters = False
@@ -771,7 +772,7 @@ def __init__(self,
771772
# 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.
772773
if mutation_num_genes[idx] == 0:
773774
if not self.suppress_warnings:
774-
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.")
775+
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.")
775776
mutation_num_genes[idx] = 1
776777
if mutation_percent_genes[0] < mutation_percent_genes[1]:
777778
if not self.suppress_warnings:

0 commit comments

Comments
 (0)