Skip to content

Commit cd57312

Browse files
authored
Merge pull request ahmedfgad#244 from ahmedfgad/github-actions
GitHub actions
2 parents adf7758 + c4769bb commit cd57312

8 files changed

+1922
-277
lines changed

pygad/pygad.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ def __init__(self,
880880
self.select_parents = self.rank_selection
881881
else:
882882
self.valid_parameters = False
883-
raise TypeError(f"Undefined parent selection type: {parent_selection_type}. \nThe assigned value to the 'parent_selection_type' parameter does not refer to one of the supported parent selection techniques which are: \n-sss (for steady state selection)\n-rws (for roulette wheel selection)\n-sus (for stochastic universal selection)\n-rank (for rank selection)\n-random (for random selection)\n-tournament (for tournament selection).\n")
883+
raise TypeError(f"Undefined parent selection type: {parent_selection_type}. \nThe assigned value to the 'parent_selection_type' parameter does not refer to one of the supported parent selection techniques which are: \n-sss (steady state selection)\n-rws (roulette wheel selection)\n-sus (stochastic universal selection)\n-rank (rank selection)\n-random (random selection)\n-tournament (tournament selection)\n-tournament_nsga2: (Tournament selection for NSGA-II)\n-nsga2: (NSGA-II parent selection).\n")
884884

885885
# For tournament selection, validate the K value.
886886
if (parent_selection_type == "tournament"):

pygad/utils/mutation.py

+71-9
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)