Renamed decorators for consistency

This commit is contained in:
SimpleArt
2020-12-03 00:21:52 -05:00
parent 3271d7d271
commit 85855c2746
5 changed files with 85 additions and 86 deletions

View File

@ -1,7 +1,7 @@
import random
from math import ceil
def check_chromosome_mutation_rate(population_method):
def _check_chromosome_mutation_rate(population_method):
"""Checks if the chromosome mutation rate is a float between 0 and 1 before running."""
def new_method(ga):
@ -18,7 +18,7 @@ def check_chromosome_mutation_rate(population_method):
return new_method
def check_gene_mutation_rate(individual_method):
def _check_gene_mutation_rate(individual_method):
"""Checks if the gene mutation rate is a float between 0 and 1 before running."""
def new_method(ga, index):
@ -35,7 +35,7 @@ def check_gene_mutation_rate(individual_method):
return new_method
def reset_fitness(individual_method):
def _reset_fitness(individual_method):
"""Resets the fitness value of the chromosome."""
def new_method(ga, chromosome):
@ -45,7 +45,7 @@ def reset_fitness(individual_method):
return new_method
def loop_random_selections(population_method):
def _loop_random_selections(population_method):
"""Runs the population method until enough chromosomes are mutated.
Provides the indexes of selected chromosomes to mutate using
random.sample to get all indexes fast.
@ -63,7 +63,7 @@ def loop_random_selections(population_method):
return new_method
def loop_random_mutations(individual_method):
def _loop_random_mutations(individual_method):
"""Runs the individual method until enough
genes are mutated on the indexed chromosome.
"""
@ -83,27 +83,26 @@ def loop_random_mutations(individual_method):
class Mutation_Methods:
# Private method decorators, see above.
_check_chromosome_mutation_rate = check_chromosome_mutation_rate
_check_gene_mutation_rate = check_gene_mutation_rate
_reset_fitness = reset_fitness
_loop_random_selections = loop_random_selections
_loop_random_mutations = loop_random_mutations
_check_chromosome_mutation_rate = _check_chromosome_mutation_rate
_check_gene_mutation_rate = _check_gene_mutation_rate
_reset_fitness = _reset_fitness
_loop_random_selections = _loop_random_selections
_loop_random_mutations = _loop_random_mutations
class Population:
"""Methods for selecting chromosomes to mutate"""
@check_chromosome_mutation_rate
@loop_random_selections
@_check_chromosome_mutation_rate
@_loop_random_selections
def random_selection(ga, index):
"""Selects random chromosomes."""
ga.mutation_individual_impl(ga, ga.population[index])
@check_chromosome_mutation_rate
@loop_random_selections
@_check_chromosome_mutation_rate
@_loop_random_selections
def random_avoid_best(ga, index):
"""Selects random chromosomes while avoiding the best chromosomes. (Elitism)"""
@ -114,9 +113,9 @@ class Mutation_Methods:
class Individual:
"""Methods for mutating a single chromosome."""
@check_gene_mutation_rate
@reset_fitness
@loop_random_mutations
@_check_gene_mutation_rate
@_reset_fitness
@_loop_random_mutations
def individual_genes(ga, chromosome, index):
"""Mutates a random gene in the chromosome."""
@ -137,9 +136,9 @@ class Mutation_Methods:
"""Methods for mutating a chromosome
by numerically modifying the genes."""
@check_gene_mutation_rate
@reset_fitness
@loop_random_mutations
@_check_gene_mutation_rate
@_reset_fitness
@_loop_random_mutations
def reflect_genes(ga, chromosome, index):
"""Reflects genes against the best chromosome."""
@ -152,9 +151,9 @@ class Mutation_Methods:
"""Methods for mutating a chromosome
by changing the order of the genes."""
@check_gene_mutation_rate
@reset_fitness
@loop_random_mutations
@_check_gene_mutation_rate
@_reset_fitness
@_loop_random_mutations
def swap_genes(ga, chromosome, index):
"""Swaps two random genes in the chromosome."""