Skip to content

None index with adaptive mutation #226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 6 additions & 0 deletions pygad/utils/mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,9 @@ def adaptive_mutation_population_fitness(self, offspring):
if self.fitness_batch_size in [1, None]:
# Calculate the fitness for each individual solution.
for idx in range(first_idx, last_idx):
# We cannot return the index of the solution within the population.
# Because the new solution (offspring) does not yet exist in the population.
# The user should handle this situation if the solution index is used anywhere.
fitness[idx] = self.fitness_func(self,
temp_population[idx],
None)
Expand All @@ -495,6 +498,9 @@ def adaptive_mutation_population_fitness(self, offspring):
batch_last_index = first_idx + (batch_idx + 1) * self.fitness_batch_size

# Calculate the fitness values for the batch.
# We cannot return the index/indices of the solution(s) within the population.
# Because the new solution(s) (offspring) do(es) not yet exist in the population.
# The user should handle this situation if the solution index is used anywhere.
fitness_temp = self.fitness_func(self,
temp_population[batch_first_index:batch_last_index],
None)
Expand Down