1 """Perform Simple mutations on an organism's genome.
2 """
3
4 import random
5
6
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
23 self._mutation_rand = random.Random()
24
25 self._switch_rand = random.Random()
26
27 self._pos_rand = random.Random()
28
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
38 mutation_pos = \
39 self._pos_rand.choice(range(len(mutated_org.genome)))
40
41
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
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
67 self._mutation_rand = random.Random()
68
69 self._switch_rand = random.Random()
70
72 """Mutate the organisms genome.
73 """
74 mutated_org = organism.copy()
75
76 gene_choices = mutated_org.genome.alphabet.letters
77
78
79 for gene_index in range(len(mutated_org.genome)):
80 mutation_chance = self._mutation_rand.random()
81
82 if mutation_chance <= self._mutation_rate:
83
84 new_letter = self._switch_rand.choice(gene_choices)
85 mutated_org.genome[gene_index] = new_letter
86
87 return mutated_org
88