1 """Base selection class from which all Selectors should derive.
2 """
3
5 """Base class for Selector classes.
6
7 This classes provides useful functions for different selector classes
8 and also defines the functions that all selector classes must
9 implement.
10
11 This class should not be used directly, but rather should be subclassed.
12 """
13 - def __init__(self, mutator, crossover, repairer = None):
14 """Initialize a selector.
15
16 Arguments:
17
18 o mutator -- A Mutation object which will perform mutation
19 on an individual.
20
21 o crossover -- A Crossover object which will take two
22 individuals and produce two new individuals which may
23 have had crossover occur.
24
25 o repairer -- A class which can do repair on rearranged genomes
26 to eliminate infeasible individuals. If set at None, so repair
27 will be done.
28 """
29 self._mutator = mutator
30 self._crossover = crossover
31 self._repairer = repairer
32
34 """Perform mutation and crossover on the two organisms.
35
36 This uses the classes mutator and crossover functions to
37 perform the manipulations.
38
39 If a repair class is available, then the rearranged genomes will
40 be repaired to make them feasible.
41
42 The newly created individuals are returned.
43 """
44
45 cross_org_1, cross_org_2 = self._crossover.do_crossover(org_1, org_2)
46
47
48 final_org_1 = self._mutator.mutate(cross_org_1)
49 final_org_2 = self._mutator.mutate(cross_org_2)
50
51
52 if self._repairer is not None:
53 final_org_1 = self._repairer.repair(final_org_1)
54 final_org_2 = self._repairer.repair(final_org_2)
55
56 return final_org_1, final_org_2
57
59 raise NotImplementedError("Derived classes must implement.")
60