From 97cb4e869cd75d938cad0018a4a68ba2d17ae167 Mon Sep 17 00:00:00 2001 From: SimpleArt <71458112+SimpleArt@users.noreply.github.com> Date: Sun, 20 Dec 2020 14:01:26 -0500 Subject: [PATCH] Added another mutation method --- src/mutation/mutation_methods.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/mutation/mutation_methods.py b/src/mutation/mutation_methods.py index 20b13c9..5a5111e 100644 --- a/src/mutation/mutation_methods.py +++ b/src/mutation/mutation_methods.py @@ -192,3 +192,24 @@ class Mutation_Methods: # Swap genes chromosome[index_one], chromosome[index_two] = chromosome[index_two], chromosome[index_one] + + + @_check_gene_mutation_rate + @_reset_fitness + def swap_segments(ga, chromosome): + """Splits the chromosome into 3 segments and shuffle them.""" + + # Chromosome too short to mutate + if len(chromosome) < 3: + return + + # Indexes to split the chromosome + index_two = random.randrange(2, len(chromosome)) + index_one = random.randrange(1, index_two) + + # Extract segments and shuffle them + segments = [chromosome[:index_one], chromosome[index_one:index_two], chromosome[index_two:]] + random.shuffle(segments) + + # Put segments back together + chromosome.gene_list = segments[0] + segments[1] + segments[2]