Fixed scope of function decorators

This commit is contained in:
SimpleArt
2020-11-19 22:28:04 -05:00
parent 46b4dc749d
commit 3d10adb2d9
4 changed files with 85 additions and 81 deletions

View File

@ -1,20 +1,33 @@
import random
from math import ceil
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
def loop_mutations(mutation_method):
def helper(ga, old_chromosome):
chromosome = ga.make_chromosome(list(old_chromosome))
# Loops until enough mutations occur
for n in range(ceil(len(chromosome)*ga.gene_mutation_rate)):
mutation_method(ga, chromosome)
return chromosome
return helper
class Mutation_Methods:
class Population:
"""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
@loop_selections
def random_selection(ga):
"""Selects random chromosomes"""
@ -22,7 +35,7 @@ class Mutation_Methods:
ga.population[index] = ga.mutation_individual_impl(ga, ga.population[index])
@__loop_selections
@loop_selections
def random_selection_then_cross(ga):
"""Selects random chromosomes and self-crosses with parent"""
@ -34,19 +47,8 @@ class Mutation_Methods:
class Individual:
"""Methods for mutating a single chromosome"""
def __loop_mutations(mutation_method):
def helper(ga, old_chromosome):
chromosome = ga.make_chromosome(list(old_chromosome))
# Loops until enough mutations occur
for n in range(ceil(len(chromosome)*ga.gene_mutation_rate)):
mutation_method(ga, chromosome)
return chromosome
return helper
@__loop_mutations
@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)
@ -68,7 +70,7 @@ class Mutation_Methods:
"""Methods for mutating a chromosome
by changing the order of the genes."""
@Individual._Individual__loop_mutations
@loop_mutations
def swap_genes(ga, chromosome):
"""Mutates a random gene in the chromosome and resets the fitness."""