Added function decorators

This commit is contained in:
SimpleArt
2020-11-19 21:16:51 -05:00
parent d90618a614
commit 0bd08b94a9

View File

@ -6,37 +6,49 @@ class Mutation_Methods:
class Population: class Population:
"""Methods for selecting chromosomes to mutate""" """Methods for selecting chromosomes to mutate"""
def __loop_selections(selection_method):
def helper(ga):
# Loop until enough mutations occur
for n in range(ceil(len(ga.population)*ga.chromosome_mutation_rate)):
selection_method(ga)
return helper
@__loop_selections
def random_selection(ga): def random_selection(ga):
"""Selects random chromosomes""" """Selects random chromosomes"""
# Loop until enough mutations occur
for n in range(ceil(len(ga.population)*ga.chromosome_mutation_rate)):
index = random.randint(0, len(ga.population)-1) index = random.randint(0, len(ga.population)-1)
ga.population[index] = ga.mutation_individual_impl(ga, ga.population[index]) ga.population[index] = ga.mutation_individual_impl(ga, ga.population[index])
@__loop_selections
def random_selection_then_cross(ga): def random_selection_then_cross(ga):
"""Selects random chromosomes and self-crosses with parent""" """Selects random chromosomes and self-crosses with parent"""
# Loop until enough mutations occur
for n in range(ceil(len(ga.population)*ga.chromosome_mutation_rate)):
index = random.randint(0, len(ga.population)-1) index = random.randint(0, len(ga.population)-1)
chromosome = ga.population[index] chromosome = ga.population[index]
# Cross the chromosome with its mutation
ga.population[index] = ga.crossover_individual_impl(ga, chromosome, ga.mutation_individual_impl(ga, chromosome)) ga.population[index] = ga.crossover_individual_impl(ga, chromosome, ga.mutation_individual_impl(ga, chromosome))
class Individual: class Individual:
"""Methods for mutating a single chromosome""" """Methods for mutating a single chromosome"""
def __loop_mutations(mutation_method):
def individual_genes(ga, old_chromosome): def helper(ga, old_chromosome):
"""Mutates a random gene in the chromosome and resets the fitness."""
chromosome = ga.make_chromosome(list(old_chromosome)) chromosome = ga.make_chromosome(list(old_chromosome))
# Loops until enough mutations occur # Loops until enough mutations occur
for n in range(ceil(len(chromosome)*ga.gene_mutation_rate)): for n in range(ceil(len(chromosome)*ga.gene_mutation_rate)):
mutation_method(ga, chromosome)
return chromosome
return helper
@__loop_mutations
def individual_genes(ga, chromosome):
"""Mutates a random gene in the chromosome and resets the fitness."""
index = random.randint(0, len(chromosome)-1) index = random.randint(0, len(chromosome)-1)
# Using the chromosome_impl # Using the chromosome_impl
@ -49,25 +61,18 @@ class Mutation_Methods:
# Exit because no gene creation method specified # Exit because no gene creation method specified
else: else:
print("You did not specify any initialization constraints.") raise Exception("Did not specify any initialization constraints.")
break
return chromosome
class Permutation: class Permutation:
"""Methods for mutating a chromosome """Methods for mutating a chromosome
by changing the order of the genes.""" by changing the order of the genes."""
def swap_genes(ga, old_chromosome): @Individual._Individual__loop_mutations
def swap_genes(ga, chromosome):
"""Mutates a random gene in the chromosome and resets the fitness.""" """Mutates a random gene in the chromosome and resets the fitness."""
chromosome = ga.make_chromosome(list(old_chromosome))
# Loops until enough mutations occur
for n in range(ceil(len(chromosome)*ga.gene_mutation_rate)):
index_one = random.randint(0, len(chromosome)-1) index_one = random.randint(0, len(chromosome)-1)
index_two = random.randint(0, len(chromosome)-1) index_two = random.randint(0, len(chromosome)-1)
chromosome[index_one], chromosome[index_two] = chromosome[index_two], chromosome[index_one] chromosome[index_one], chromosome[index_two] = chromosome[index_two], chromosome[index_one]
return chromosome