eckity.genetic_operators.crossovers.subtree_crossover

 1from eckity.genetic_operators.genetic_operator import GeneticOperator
 2
 3
 4class SubtreeCrossover(GeneticOperator):
 5    def __init__(self, probability=1, arity=2, events=None):
 6        self.individuals = None
 7        self.applied_individuals = None
 8        super().__init__(probability=probability, arity=arity, events=events)
 9
10    def apply(self, individuals):
11        """
12        Perform subtree crossover between this tree and `other` tree:
13            1. Select random node from `other` tree
14            2. Get subtree rooted at selected node
15            1. Select a random node in this tree
16            2. Place `other` selected subtree at this node, replacing current subtree
17
18        Parameters
19        ----------
20        individuals
21        select_func: callable
22        Selection method used to receive additional individuals to perform crossover on
23
24        individual: Tree
25        tree individual to perform crossover on
26
27        Returns
28        -------
29        a new, modified individual
30        """
31
32        assert len(individuals) == self.arity, f'Expected individuals list of size {self.arity}, got {len(individuals)}'
33
34        self.individuals = individuals
35
36        # select a random subtree from each individual's tree
37        subtrees = [ind.random_subtree() for ind in individuals]
38
39        # assign the next individual's subtree to the current individual's tree
40        for i in range(len(individuals) - 1):
41            individuals[i].replace_subtree(subtrees[i+1])
42
43        # to complete the crossover circle, assign the first subtree to the last individual
44        individuals[-1].replace_subtree(subtrees[0])
45
46        self.applied_individuals = individuals
47        return individuals
class SubtreeCrossover(eckity.genetic_operators.genetic_operator.GeneticOperator):
 5class SubtreeCrossover(GeneticOperator):
 6    def __init__(self, probability=1, arity=2, events=None):
 7        self.individuals = None
 8        self.applied_individuals = None
 9        super().__init__(probability=probability, arity=arity, events=events)
10
11    def apply(self, individuals):
12        """
13        Perform subtree crossover between this tree and `other` tree:
14            1. Select random node from `other` tree
15            2. Get subtree rooted at selected node
16            1. Select a random node in this tree
17            2. Place `other` selected subtree at this node, replacing current subtree
18
19        Parameters
20        ----------
21        individuals
22        select_func: callable
23        Selection method used to receive additional individuals to perform crossover on
24
25        individual: Tree
26        tree individual to perform crossover on
27
28        Returns
29        -------
30        a new, modified individual
31        """
32
33        assert len(individuals) == self.arity, f'Expected individuals list of size {self.arity}, got {len(individuals)}'
34
35        self.individuals = individuals
36
37        # select a random subtree from each individual's tree
38        subtrees = [ind.random_subtree() for ind in individuals]
39
40        # assign the next individual's subtree to the current individual's tree
41        for i in range(len(individuals) - 1):
42            individuals[i].replace_subtree(subtrees[i+1])
43
44        # to complete the crossover circle, assign the first subtree to the last individual
45        individuals[-1].replace_subtree(subtrees[0])
46
47        self.applied_individuals = individuals
48        return individuals
SubtreeCrossover(probability=1, arity=2, events=None)
6    def __init__(self, probability=1, arity=2, events=None):
7        self.individuals = None
8        self.applied_individuals = None
9        super().__init__(probability=probability, arity=arity, events=events)
individuals
applied_individuals
def apply(self, individuals):
11    def apply(self, individuals):
12        """
13        Perform subtree crossover between this tree and `other` tree:
14            1. Select random node from `other` tree
15            2. Get subtree rooted at selected node
16            1. Select a random node in this tree
17            2. Place `other` selected subtree at this node, replacing current subtree
18
19        Parameters
20        ----------
21        individuals
22        select_func: callable
23        Selection method used to receive additional individuals to perform crossover on
24
25        individual: Tree
26        tree individual to perform crossover on
27
28        Returns
29        -------
30        a new, modified individual
31        """
32
33        assert len(individuals) == self.arity, f'Expected individuals list of size {self.arity}, got {len(individuals)}'
34
35        self.individuals = individuals
36
37        # select a random subtree from each individual's tree
38        subtrees = [ind.random_subtree() for ind in individuals]
39
40        # assign the next individual's subtree to the current individual's tree
41        for i in range(len(individuals) - 1):
42            individuals[i].replace_subtree(subtrees[i+1])
43
44        # to complete the crossover circle, assign the first subtree to the last individual
45        individuals[-1].replace_subtree(subtrees[0])
46
47        self.applied_individuals = individuals
48        return individuals

Perform subtree crossover between this tree and other tree: 1. Select random node from other tree 2. Get subtree rooted at selected node 1. Select a random node in this tree 2. Place other selected subtree at this node, replacing current subtree

Parameters
  • individuals
  • select_func (callable):

  • Selection method used to receive additional individuals to perform crossover on

  • individual (Tree):

  • tree individual to perform crossover on

Returns
  • a new, modified individual