From 3e9c2fc7842373012c1db02619465882261574c4 Mon Sep 17 00:00:00 2001 From: SimpleArt <71458112+SimpleArt@users.noreply.github.com> Date: Thu, 12 Nov 2020 17:13:23 -0500 Subject: [PATCH] Multiple updates - Removed obsolete whole_chromosome method. - Renamed single_gene to individual_genes. - Rewrote single_genes to use copied data instead of modifying original data. - Added random_selection_then_cross. --- src/mutation/mutation_methods.py | 49 ++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/src/mutation/mutation_methods.py b/src/mutation/mutation_methods.py index 51a5c40..6ac246f 100644 --- a/src/mutation/mutation_methods.py +++ b/src/mutation/mutation_methods.py @@ -21,33 +21,38 @@ class Mutation_Methods: ga.population.set_chromosome(ga.mutation_individual_impl(ga, ga.population.get_chromosome(index)), index) + def random_selection_then_cross(ga): + """Selects random chromosomes and self-crosses with parent""" + mutation_count = 0 + + # Loop until enough mutations occur + while mutation_count < ga.population.size()*ga.chromosome_mutation_rate: + + # Loop through the population + for index in range(ga.population.size()): + + chromosome = ga.population.get_chromosome(index) + + # Randomly apply mutations + if random.uniform(0, 1) < ga.chromosome_mutation_rate: + mutation_count += 1 + ga.population.set_chromosome( + ga.crossover_individual_impl( + ga, + chromosome, + ga.mutation_individual_impl(ga, chromosome) + ), + index + ) + + class Individual: """Methods for mutating a single chromosome""" - def whole_chromosome(ga, chromosome): - """Makes a completely random chromosome filled with new genes.""" - # Using the chromosome_impl to set every index inside of the chromosome - if ga.chromosome_impl != None: - return ga.make_chromosome([ - ga.make_gene(value) - for value in ga.chromosome_impl()]) - - # Using the gene_impl - elif ga.gene_impl != None: - return ga.make_chromosome([ - ga.make_gene(ga.gene_impl()) - for j in range(chromosome.size())]) - - # Exit because no gene creation method specified - else: - print("You did not specify any initialization constraints.") - return None - - - def single_gene(ga, chromosome): + def individual_genes(ga, old_chromosome): """Mutates a random gene in the chromosome and resets the fitness.""" - chromosome.set_fitness(None) + chromosome = ga.make_chromosome(old_chromosome.get_gene_list()) mutation_count = 0 # Loops until enough mutations occur