Changed implementation framework

Instead of a nested approach, selection/crossover/mutation are all called separately and directly by the GA. selection_impl was also separated into parent_selection_impl and survivor_selection_impl, as both are needed separately.
This commit is contained in:
RyleyGG
2020-10-04 17:59:59 -04:00
parent c18a531034
commit e05aa7f62b
5 changed files with 102 additions and 120 deletions

View File

@ -8,7 +8,7 @@ class Mutation_Types:
def random_mutation(self, ga, chromosome_set = None):
if chromosome_set == None:
chromosome_set = ga.population
chromosome_set = ga.population.get_all_chromosomes()
chromosome_mutate_num = int(len(chromosome_set)*ga.mutation_rate)
temp_population = ga.initialization_impl(ga)
@ -18,4 +18,25 @@ class Mutation_Types:
chromosome_mutate_num -= 1
return chromosome_set
def per_gene_mutation(self, ga, chromosome_set = None, gene_mutate_count = 1):
gene_mutate_count_static = int(gene_mutate_count)
if chromosome_set == None:
chromosome_set = ga.population.get_all_chromosomes()
for i in range(len(chromosome_set)):
random_num = random.uniform(0,1)
if (random_num <= ga.mutation_rate):
while gene_mutate_count > 0:
dummy_population = ga.initialization_impl(ga) #Really inefficient, but works for now
random_index = random.randint(0, ga.chromosome_length-1)
chromosome_set[i].get_genes()[random_index] = dummy_population.get_all_chromosomes()[random.randint(0,ga.population_size-1)].get_genes()[random_index]
gene_mutate_count -= 1
gene_mutate_count = int(gene_mutate_count_static)
return chromosome_set