6
6
def fitness_func (ga_instance , solution , sol_idx ):
7
7
global GANN_instance , data_inputs , data_outputs
8
8
9
+ # If adaptive mutation is used, sometimes sol_idx is None.
10
+ if sol_idx == None :
11
+ sol_idx = 1
12
+
9
13
predictions = pygad .nn .predict (last_layer = GANN_instance .population_networks [sol_idx ],
10
14
data_inputs = data_inputs )
11
15
correct_predictions = numpy .where (predictions == data_outputs )[0 ].size
@@ -16,16 +20,16 @@ def fitness_func(ga_instance, solution, sol_idx):
16
20
def callback_generation (ga_instance ):
17
21
global GANN_instance , last_fitness
18
22
19
- population_matrices = pygad .gann .population_as_matrices (population_networks = GANN_instance .population_networks ,
23
+ population_matrices = pygad .gann .population_as_matrices (population_networks = GANN_instance .population_networks ,
20
24
population_vectors = ga_instance .population )
21
25
22
26
GANN_instance .update_population_trained_weights (population_trained_weights = population_matrices )
23
27
24
28
print ("Generation = {generation}" .format (generation = ga_instance .generations_completed ))
25
- print ("Fitness = {fitness}" .format (fitness = ga_instance .best_solution (pop_fitness = ga_instance . last_generation_fitness )[1 ]))
26
- print ("Change = {change}" .format (change = ga_instance .best_solution (pop_fitness = ga_instance . last_generation_fitness )[1 ] - last_fitness ))
29
+ print ("Fitness = {fitness}" .format (fitness = ga_instance .best_solution ()[1 ]))
30
+ print ("Change = {change}" .format (change = ga_instance .best_solution ()[1 ] - last_fitness ))
27
31
28
- last_fitness = ga_instance .best_solution (pop_fitness = ga_instance . last_generation_fitness )[1 ].copy ()
32
+ last_fitness = ga_instance .best_solution ()[1 ].copy ()
29
33
30
34
# Holds the fitness value of the previous generation.
31
35
last_fitness = 0
@@ -37,9 +41,9 @@ def callback_generation(ga_instance):
37
41
[0 , 0 ]])
38
42
39
43
# Preparing the NumPy array of the outputs.
40
- data_outputs = numpy .array ([0 ,
41
- 1 ,
42
- 1 ,
44
+ data_outputs = numpy .array ([0 ,
45
+ 1 ,
46
+ 1 ,
43
47
0 ])
44
48
45
49
# The length of the input vector for each sample (i.e. number of neurons in the input layer).
@@ -69,21 +73,21 @@ def callback_generation(ga_instance):
69
73
70
74
num_generations = 500 # Number of generations.
71
75
72
- mutation_percent_genes = 5 # Percentage of genes to mutate. This parameter has no action if the parameter mutation_num_genes exists.
76
+ mutation_percent_genes = [ 5 , 10 ] # Percentage of genes to mutate. This parameter has no action if the parameter mutation_num_genes exists.
73
77
74
78
parent_selection_type = "sss" # Type of parent selection.
75
79
76
80
crossover_type = "single_point" # Type of the crossover operator.
77
81
78
- mutation_type = "random " # Type of the mutation operator.
82
+ mutation_type = "adaptive " # Type of the mutation operator.
79
83
80
84
keep_parents = 1 # Number of parents to keep in the next population. -1 means keep all parents and 0 means keep nothing.
81
85
82
86
init_range_low = - 2
83
87
init_range_high = 5
84
88
85
- ga_instance = pygad .GA (num_generations = num_generations ,
86
- num_parents_mating = num_parents_mating ,
89
+ ga_instance = pygad .GA (num_generations = num_generations ,
90
+ num_parents_mating = num_parents_mating ,
87
91
initial_population = initial_population ,
88
92
fitness_func = fitness_func ,
89
93
mutation_percent_genes = mutation_percent_genes ,
@@ -93,6 +97,7 @@ def callback_generation(ga_instance):
93
97
crossover_type = crossover_type ,
94
98
mutation_type = mutation_type ,
95
99
keep_parents = keep_parents ,
100
+ suppress_warnings = True ,
96
101
on_generation = callback_generation )
97
102
98
103
ga_instance .run ()
@@ -101,7 +106,7 @@ def callback_generation(ga_instance):
101
106
ga_instance .plot_fitness ()
102
107
103
108
# Returning the details of the best solution.
104
- solution , solution_fitness , solution_idx = ga_instance .best_solution (pop_fitness = ga_instance . last_generation_fitness )
109
+ solution , solution_fitness , solution_idx = ga_instance .best_solution ()
105
110
print ("Parameters of the best solution : {solution}" .format (solution = solution ))
106
111
print ("Fitness value of the best solution = {solution_fitness}" .format (solution_fitness = solution_fitness ))
107
112
print ("Index of the best solution : {solution_idx}" .format (solution_idx = solution_idx ))
0 commit comments