Package Bio :: Package GA :: Package Mutation :: Module Simple
[hide private]
[frames] | no frames]

Source Code for Module Bio.GA.Mutation.Simple

 1  """Perform Simple mutations on an organism's genome. 
 2  """ 
 3  # standard modules 
 4  import random 
 5   
 6   
7 -class SinglePositionMutation(object):
8 """Perform a conversion mutation, but only at a single point in the genome. 9 10 This does not randomize the genome as much as ConversionMutation, since 11 only one change is allowed per genome at the specified mutation rate. 12 """
13 - def __init__(self, mutation_rate = 0.001):
14 """Initialize a mutator. 15 16 Arguments: 17 18 o mutation_rate - The chance of a mutation happening once in the 19 genome. 20 """ 21 self._mutation_rate = mutation_rate 22 # a random number generator to test if we have a mutation 23 self._mutation_rand = random.Random() 24 # a random number generator to switch to a new alphabet letter 25 self._switch_rand = random.Random() 26 # a random number generator to find the mutation position 27 self._pos_rand = random.Random()
28
29 - def mutate(self, organism):
30 """Mutate the organisms genome. 31 """ 32 mutated_org = organism.copy() 33 gene_choices = mutated_org.genome.alphabet.letters 34 35 mutation_chance = self._mutation_rand.random() 36 if mutation_chance <= self._mutation_rate: 37 # pick a gene position to mutate at 38 mutation_pos = \ 39 self._pos_rand.choice(range(len(mutated_org.genome))) 40 41 # get a new letter to replace the position at 42 new_letter = self._switch_rand.choice(gene_choices) 43 44 mutated_org.genome[mutation_pos] = new_letter 45 46 return mutated_org
47 48
49 -class ConversionMutation(object):
50 """Potentially mutate any item to another in the alphabet. 51 52 This just performs switching mutation -- changing one gene of a genome 53 to any other potential gene, at some defined frequency. If the organism 54 is determined to mutate, then the alphabet item it is equally likely 55 to switch to any other letter in the alphabet. 56 """
57 - def __init__(self, mutation_rate = 0.001):
58 """Inititialize a mutator. 59 60 Arguments: 61 62 o mutation_rate -- The chance of a mutation happening at any 63 position in the genome. 64 """ 65 self._mutation_rate = mutation_rate 66 # a random number generator to test if we have a mutation 67 self._mutation_rand = random.Random() 68 # a random number generator to switch to a new alphabet letter 69 self._switch_rand = random.Random()
70
71 - def mutate(self, organism):
72 """Mutate the organisms genome. 73 """ 74 mutated_org = organism.copy() 75 76 gene_choices = mutated_org.genome.alphabet.letters 77 78 # potentially mutate any gene in the genome 79 for gene_index in range(len(mutated_org.genome)): 80 mutation_chance = self._mutation_rand.random() 81 # if we have a mutation 82 if mutation_chance <= self._mutation_rate: 83 # get a new letter 84 new_letter = self._switch_rand.choice(gene_choices) 85 mutated_org.genome[gene_index] = new_letter 86 87 return mutated_org
88