Skip to content

Commit 01cb71c

Browse files
committed
None solution index
1 parent b84d062 commit 01cb71c

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

docs/source/gann.rst

+13-8
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,10 @@ its complete code is listed below.
649649
def fitness_func(ga_instance, solution, sol_idx):
650650
global GANN_instance, data_inputs, data_outputs
651651
652+
# If adaptive mutation is used, sometimes sol_idx is None.
653+
if sol_idx == None:
654+
sol_idx = 1
655+
652656
predictions = pygad.nn.predict(last_layer=GANN_instance.population_networks[sol_idx],
653657
data_inputs=data_inputs)
654658
correct_predictions = numpy.where(predictions == data_outputs)[0].size
@@ -659,7 +663,7 @@ its complete code is listed below.
659663
def callback_generation(ga_instance):
660664
global GANN_instance, last_fitness
661665
662-
population_matrices = pygad.gann.population_as_matrices(population_networks=GANN_instance.population_networks,
666+
population_matrices = pygad.gann.population_as_matrices(population_networks=GANN_instance.population_networks,
663667
population_vectors=ga_instance.population)
664668
665669
GANN_instance.update_population_trained_weights(population_trained_weights=population_matrices)
@@ -680,9 +684,9 @@ its complete code is listed below.
680684
[0, 0]])
681685
682686
# Preparing the NumPy array of the outputs.
683-
data_outputs = numpy.array([0,
684-
1,
685-
1,
687+
data_outputs = numpy.array([0,
688+
1,
689+
1,
686690
0])
687691
688692
# The length of the input vector for each sample (i.e. number of neurons in the input layer).
@@ -712,21 +716,21 @@ its complete code is listed below.
712716
713717
num_generations = 500 # Number of generations.
714718
715-
mutation_percent_genes = 5 # Percentage of genes to mutate. This parameter has no action if the parameter mutation_num_genes exists.
719+
mutation_percent_genes = [5, 10] # Percentage of genes to mutate. This parameter has no action if the parameter mutation_num_genes exists.
716720
717721
parent_selection_type = "sss" # Type of parent selection.
718722
719723
crossover_type = "single_point" # Type of the crossover operator.
720724
721-
mutation_type = "random" # Type of the mutation operator.
725+
mutation_type = "adaptive" # Type of the mutation operator.
722726
723727
keep_parents = 1 # Number of parents to keep in the next population. -1 means keep all parents and 0 means keep nothing.
724728
725729
init_range_low = -2
726730
init_range_high = 5
727731
728-
ga_instance = pygad.GA(num_generations=num_generations,
729-
num_parents_mating=num_parents_mating,
732+
ga_instance = pygad.GA(num_generations=num_generations,
733+
num_parents_mating=num_parents_mating,
730734
initial_population=initial_population,
731735
fitness_func=fitness_func,
732736
mutation_percent_genes=mutation_percent_genes,
@@ -736,6 +740,7 @@ its complete code is listed below.
736740
crossover_type=crossover_type,
737741
mutation_type=mutation_type,
738742
keep_parents=keep_parents,
743+
suppress_warnings=True,
739744
on_generation=callback_generation)
740745
741746
ga_instance.run()

examples/gann/example_XOR_classification.py

+17-12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
def 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):
1620
def 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.
3135
last_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

7074
num_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

7478
parent_selection_type = "sss" # Type of parent selection.
7579

7680
crossover_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

8084
keep_parents = 1 # Number of parents to keep in the next population. -1 means keep all parents and 0 means keep nothing.
8185

8286
init_range_low = -2
8387
init_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

98103
ga_instance.run()
@@ -101,7 +106,7 @@ def callback_generation(ga_instance):
101106
ga_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()
105110
print("Parameters of the best solution : {solution}".format(solution=solution))
106111
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
107112
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))

0 commit comments

Comments
 (0)