1 """Methods for performing repairs that will Stabilize genomes.
2
3 These methods perform repair to keep chromosomes from drifting too far in
4 any direction -- ie. bring them back to a stabilizing center. This may be
5 useful in cases where fitness functions alone won't keep chromosomes in
6 check.
7 """
8
9 import random
10
12 """Perform repair to reduce the number of Ambiguous genes in a genome.
13
14 In cases where ambiguous genes are allowed in a genome (for example,
15 where you have a wild card character like '*' that will match
16 anything), these can come to dominate a genome since, really, the
17 best fitness is someting like '*******'. This repair protects against
18 that by changing ambiguous characters into some non-ambiguous gene.
19 """
20 - def __init__(self, ambig_finder, num_ambiguous):
21 """Initialize the repair class.
22
23 Arguments:
24
25 o ambig_finder - A class implementing the function find_ambiguous
26 which will return a list of all ambiguous positions in a sequence.
27 It also must have the function all_unambiguous, which will return
28 all allowed unambiguous letters.
29
30 o num_ambiguous - The minimum number of ambiguous items that are
31 allowed in a genome. If there are more than this present, repair
32 will be performed.
33 """
34 self._ambig_finder = ambig_finder
35 self._num_ambiguous = num_ambiguous
36 self._alphabet_letters = ambig_finder.all_unambiguous()
37
39 """Perform a repair to remove excess ambiguous genes.
40 """
41 new_org = organism.copy()
42
43
44 while 1:
45
46 seq_genome = new_org.genome.toseq()
47 all_ambiguous = self._ambig_finder.find_ambiguous(seq_genome.tostring())
48
49
50 if len(all_ambiguous) <= self._num_ambiguous:
51 break
52
53
54 to_change = random.choice(all_ambiguous)
55 new_gene = random.choice(self._alphabet_letters)
56 new_org.genome[to_change] = new_gene
57
58 return new_org
59