8000 Adaptive mutation bug fix & more tests · madprog/GeneticAlgorithmPython@f7b8bd3 · GitHub
[go: up one dir, main page]

Skip to content

Commit f7b8bd3

Browse files
committed
Adaptive mutation bug fix & more tests
1 parent 2865a2c commit f7b8bd3

File tree

3 files changed

+806
-127
lines changed

3 files changed

+806
-127
lines changed

pygad/utils/mutation.py

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -578,10 +578,31 @@ def adaptive_mutation_by_space(self, offspring):
578578
# Adaptive mutation changes one or more genes in each offspring randomly.
579579
# The number of genes to mutate depends on the solution's fitness value.
580580
for offspring_idx in range(offspring.shape[0]):
581-
if offspring_fitness[offspring_idx] < average_fitness:
582-
adaptive_mutation_num_genes = self.mutation_num_genes[0]
581+
## TODO Make edits to work with multi-objective optimization.
582+
# Compare the fitness of each offspring to the average fitness of each objective function.
583+
fitness_comparison = offspring_fitness[offspring_idx] < average_fitness
584+
585+
# Check if the problem is single or multi-objective optimization.
586+
if type(fitness_comparison) in [bool, numpy.bool_]:
587+
# Single-objective optimization problem.
588+
if offspring_fitness[offspring_idx] < average_fitness:
589+
adaptive_mutation_num_genes = self.mutation_num_genes[0]
590+
else:
591+
adaptive_mutation_num_genes = self.mutation_num_genes[1]
583592
else:
584-
adaptive_mutation_num_genes = self.mutation_num_genes[1]
593+
# Multi-objective optimization problem.
594+
595+
# Get the sum of the pool array (result of comparison).
596+
# True is considered 1 and False is 0.
597+
fitness_comparison_sum = sum(fitness_comparison)
598+
# Check if more than or equal to 50% of the objectives have fitness greater than the average.
599+
# If True, then use the first percentage.
600+
# If False, use the second percentage.
601+
if fitness_comparison_sum >= len(fitness_comparison)/2:
602+
adaptive_mutation_num_genes = self.mutation_num_genes[0]
603+
else:
604+
adaptive_mutation_num_genes = self.mutation_num_genes[1]
605+
585606
mutation_indices = numpy.array(random.sample(range(0, self.num_genes), adaptive_mutation_num_genes))
586607
for gene_idx in mutation_indices:
587608

@@ -703,6 +724,7 @@ def adaptive_mutation_randomly(self, offspring):
703724
## TODO Make edits to work with multi-objective optimization.
704725
# Compare the fitness of each offspring to the average fitness of each objective function.
705726
fitness_comparison = offspring_fitness[offspring_idx] < average_fitness
727+
706728
# Check if the problem is single or multi-objective optimization.
707729
if type(fitness_comparison) in [bool, numpy.bool_]:
708730
# Single-objective optimization problem.
@@ -791,10 +813,30 @@ def adaptive_mutation_probs_by_space(self, offspring):
791813
# Adaptive random mutation changes one or more genes in each offspring randomly.
792814
# The probability of mutating a gene depends on the solution's fitness value.
793815
for offspring_idx in range(offspring.shape[0]):
794-
if offspring_fitness[offspring_idx] < average_fitness:
795-
adaptive_mutation_probability = self.mutation_probability[0]
816+
## TODO Make edits to work with multi-objective optimization.
817+
# Compare the fitness of each offspring to the average fitness of each objective function.
818+
fitness_comparison = offspring_fitness[offspring_idx] < average_fitness
819+
820+
# Check if the problem is single or multi-objective optimization.
821+
if type(fitness_comparison) in [bool, numpy.bool_]:
822+
# Single-objective optimization problem.
823+
if offspring_fitness[offspring_idx] < average_fitness:
824+
adaptive_mutation_probability = self.mutation_probability[0]
825+
else:
826+
adaptive_mutation_probability = self.mutation_probability[1]
796827
else:
797-
adaptive_mutation_probability = self.mutation_probability[1]
828+
# Multi-objective optimization problem.
829+
830+
# Get the sum of the pool array (result of comparison).
831+
# True is considered 1 and False is 0.
832+
fitness_comparison_sum = sum(fitness_comparison)
833+
# Check if more than or equal to 50% of the objectives have fitness greater than the average.
834+
# If True, then use the first percentage.
835+
# If False, use the second percentage.
836+
if fitness_comparison_sum >= len(fitness_comparison)/2:
837+
adaptive_mutation_probability = self.mutation_probability[0]
838+
else:
839+
adaptive_mutation_probability = self.mutation_probability[1]
798840

799841
probs = numpy.random.random(size=offspring.shape[1])
800842
for gene_idx in range(offspring.shape[1]):
@@ -914,10 +956,30 @@ def adaptive_mutation_probs_randomly(self, offspring):
914956
# Adaptive random mutation changes one or more genes in each offspring randomly.
915957
# The probability of mutating a gene depends on the solution's fitness value.
916958
for offspring_idx in range(offspring.shape[0]):
917-
if offspring_fitness[offspring_idx] < average_fitness:
918-
adaptive_mutation_probability = self.mutation_probability[0]
959+
## TODO Make edits to work with multi-objective optimization.
960+
# Compare the fitness of each offspring to the average fitness of each objective function.
961+
fitness_comparison = offspring_fitness[offspring_idx] < average_fitness
962+
963+
# Check if the problem is single or multi-objective optimization.
964+
if type(fitness_comparison) in [bool, numpy.bool_]:
965+
# Single-objective optimization problem.
966+
if offspring_fitness[offspring_idx] < average_fitness:
967+
adaptive_mutation_probability = self.mutation_probability[0]
968+
else:
969+
adaptive_mutation_probability = self.mutation_probability[1]
919970
else:
920-
adaptive_mutation_probability = self.mutation_probability[1]
971+
# Multi-objective optimization problem.
972+
973+
# Get the sum of the pool array (result of comparison).
974+
# True is considered 1 and False is 0.
975+
fitness_comparison_sum = sum(fitness_comparison)
976+
# Check if more than or equal to 50% of the objectives have fitness greater than the average.
977+
# If True, then use the first percentage.
978+
# If False, use the second percentage.
979+
if fitness_comparison_sum >= len(fitness_comparison)/2:
980+
adaptive_mutation_probability = self.mutation_probability[0]
981+
else:
982+
adaptive_mutation_probability = self.mutation_probability[1]
921983

922984
probs = numpy.random.random(size=offspring.shape[1])
923985
for gene_idx in range(offspring.shape[1]):

0 commit comments

Comments
 (0)
0