66def fitness_func (ga_instance , solution , sol_idx ):
77 global GANN_instance , data_inputs , data_outputs
88
9+ # If adaptive mutation is used, sometimes sol_idx is None.
10+ if sol_idx == None :
11+ sol_idx = 1
12+
913 predictions = pygad .nn .predict (last_layer = GANN_instance .population_networks [sol_idx ],
1014 data_inputs = data_inputs )
1115 correct_predictions = numpy .where (predictions == data_outputs )[0 ].size
@@ -16,16 +20,16 @@ def fitness_func(ga_instance, solution, sol_idx):
1620def callback_generation (ga_instance ):
1721 global GANN_instance , last_fitness
1822
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 ,
2024 population_vectors = ga_instance .population )
2125
2226 GANN_instance .update_population_trained_weights (population_trained_weights = population_matrices )
2327
2428 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 ))
2731
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 ()
2933
3034# Holds the fitness value of the previous generation.
3135last_fitness = 0
@@ -37,9 +41,9 @@ def callback_generation(ga_instance):
3741 [0 , 0 ]])
3842
3943# 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 ,
4347 0 ])
4448
4549# 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):
6973
7074num_generations = 500 # Number of generations.
7175
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.
7377
7478parent_selection_type = "sss" # Type of parent selection.
7579
7680crossover_type = "single_point" # Type of the crossover operator.
7781
78- mutation_type = "random " # Type of the mutation operator.
82+ mutation_type = "adaptive " # Type of the mutation operator.
7983
8084keep_parents = 1 # Number of parents to keep in the next population. -1 means keep all parents and 0 means keep nothing.
8185
8286init_range_low = - 2
8387init_range_high = 5
8488
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 ,
8791 initial_population = initial_population ,
8892 fitness_func = fitness_func ,
8993 mutation_percent_genes = mutation_percent_genes ,
@@ -93,6 +97,7 @@ def callback_generation(ga_instance):
9397 crossover_type = crossover_type ,
9498 mutation_type = mutation_type ,
9599 keep_parents = keep_parents ,
100+ suppress_warnings = True ,
96101 on_generation = callback_generation )
97102
98103ga_instance .run ()
@@ -101,7 +106,7 @@ def callback_generation(ga_instance):
101106ga_instance .plot_fitness ()
102107
103108# 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 ()
105110print ("Parameters of the best solution : {solution}" .format (solution = solution ))
106111print ("Fitness value of the best solution = {solution_fitness}" .format (solution_fitness = solution_fitness ))
107112print ("Index of the best solution : {solution_idx}" .format (solution_idx = solution_idx ))
0 commit comments