Skip to content

Commit

Permalink
None solution index
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedfgad committed Aug 16, 2023
1 parent b84d062 commit 01cb71c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
21 changes: 13 additions & 8 deletions docs/source/gann.rst
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,10 @@ its complete code is listed below.
def fitness_func(ga_instance, solution, sol_idx):
global GANN_instance, data_inputs, data_outputs
# If adaptive mutation is used, sometimes sol_idx is None.
if sol_idx == None:
sol_idx = 1
predictions = pygad.nn.predict(last_layer=GANN_instance.population_networks[sol_idx],
data_inputs=data_inputs)
correct_predictions = numpy.where(predictions == data_outputs)[0].size
Expand All @@ -659,7 +663,7 @@ its complete code is listed below.
def callback_generation(ga_instance):
global GANN_instance, last_fitness
population_matrices = pygad.gann.population_as_matrices(population_networks=GANN_instance.population_networks,
population_matrices = pygad.gann.population_as_matrices(population_networks=GANN_instance.population_networks,
population_vectors=ga_instance.population)
GANN_instance.update_population_trained_weights(population_trained_weights=population_matrices)
Expand All @@ -680,9 +684,9 @@ its complete code is listed below.
[0, 0]])
# Preparing the NumPy array of the outputs.
data_outputs = numpy.array([0,
1,
1,
data_outputs = numpy.array([0,
1,
1,
0])
# The length of the input vector for each sample (i.e. number of neurons in the input layer).
Expand Down Expand Up @@ -712,21 +716,21 @@ its complete code is listed below.
num_generations = 500 # Number of generations.
mutation_percent_genes = 5 # Percentage of genes to mutate. This parameter has no action if the parameter mutation_num_genes exists.
mutation_percent_genes = [5, 10] # Percentage of genes to mutate. This parameter has no action if the parameter mutation_num_genes exists.
parent_selection_type = "sss" # Type of parent selection.
crossover_type = "single_point" # Type of the crossover operator.
mutation_type = "random" # Type of the mutation operator.
mutation_type = "adaptive" # Type of the mutation operator.
keep_parents = 1 # Number of parents to keep in the next population. -1 means keep all parents and 0 means keep nothing.
init_range_low = -2
init_range_high = 5
ga_instance = pygad.GA(num_generations=num_generations,
num_parents_mating=num_parents_mating,
ga_instance = pygad.GA(num_generations=num_generations,
num_parents_mating=num_parents_mating,
initial_population=initial_population,
fitness_func=fitness_func,
mutation_percent_genes=mutation_percent_genes,
Expand All @@ -736,6 +740,7 @@ its complete code is listed below.
crossover_type=crossover_type,
mutation_type=mutation_type,
keep_parents=keep_parents,
suppress_warnings=True,
on_generation=callback_generation)
ga_instance.run()
Expand Down
29 changes: 17 additions & 12 deletions examples/gann/example_XOR_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
def fitness_func(ga_instance, solution, sol_idx):
global GANN_instance, data_inputs, data_outputs

# If adaptive mutation is used, sometimes sol_idx is None.
if sol_idx == None:
sol_idx = 1

predictions = pygad.nn.predict(last_layer=GANN_instance.population_networks[sol_idx],
data_inputs=data_inputs)
correct_predictions = numpy.where(predictions == data_outputs)[0].size
Expand All @@ -16,16 +20,16 @@ def fitness_func(ga_instance, solution, sol_idx):
def callback_generation(ga_instance):
global GANN_instance, last_fitness

population_matrices = pygad.gann.population_as_matrices(population_networks=GANN_instance.population_networks,
population_matrices = pygad.gann.population_as_matrices(population_networks=GANN_instance.population_networks,
population_vectors=ga_instance.population)

GANN_instance.update_population_trained_weights(population_trained_weights=population_matrices)

print("Generation = {generation}".format(generation=ga_instance.generations_completed))
print("Fitness = {fitness}".format(fitness=ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1]))
print("Change = {change}".format(change=ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1] - last_fitness))
print("Fitness = {fitness}".format(fitness=ga_instance.best_solution()[1]))
print("Change = {change}".format(change=ga_instance.best_solution()[1] - last_fitness))

last_fitness = ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1].copy()
last_fitness = ga_instance.best_solution()[1].copy()

# Holds the fitness value of the previous generation.
last_fitness = 0
Expand All @@ -37,9 +41,9 @@ def callback_generation(ga_instance):
[0, 0]])

# Preparing the NumPy array of the outputs.
data_outputs = numpy.array([0,
1,
1,
data_outputs = numpy.array([0,
1,
1,
0])

# The length of the input vector for each sample (i.e. number of neurons in the input layer).
Expand Down Expand Up @@ -69,21 +73,21 @@ def callback_generation(ga_instance):

num_generations = 500 # Number of generations.

mutation_percent_genes = 5 # Percentage of genes to mutate. This parameter has no action if the parameter mutation_num_genes exists.
mutation_percent_genes = [5, 10] # Percentage of genes to mutate. This parameter has no action if the parameter mutation_num_genes exists.

parent_selection_type = "sss" # Type of parent selection.

crossover_type = "single_point" # Type of the crossover operator.

mutation_type = "random" # Type of the mutation operator.
mutation_type = "adaptive" # Type of the mutation operator.

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

init_range_low = -2
init_range_high = 5

ga_instance = pygad.GA(num_generations=num_generations,
num_parents_mating=num_parents_mating,
ga_instance = pygad.GA(num_generations=num_generations,
num_parents_mating=num_parents_mating,
initial_population=initial_population,
fitness_func=fitness_func,
mutation_percent_genes=mutation_percent_genes,
Expand All @@ -93,6 +97,7 @@ def callback_generation(ga_instance):
crossover_type=crossover_type,
mutation_type=mutation_type,
keep_parents=keep_parents,
suppress_warnings=True,
on_generation=callback_generation)

ga_instance.run()
Expand All @@ -101,7 +106,7 @@ def callback_generation(ga_instance):
ga_instance.plot_fitness()

# Returning the details of the best solution.
solution, solution_fitness, solution_idx = ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)
solution, solution_fitness, solution_idx = ga_instance.best_solution()
print("Parameters of the best solution : {solution}".format(solution=solution))
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))
Expand Down

0 comments on commit 01cb71c

Please sign in to comment.