Perform the crossover genetic operation on the program. Crossover selects a random subtree from the embedded program to be replaced. A donor also has a subtree selected at random and this is inserted into the original parent to form an offspring. Parameters
(self, donor, random_state)
| 605 | return copy(self.program) |
| 606 | |
| 607 | def crossover(self, donor, random_state): |
| 608 | """Perform the crossover genetic operation on the program. |
| 609 | |
| 610 | Crossover selects a random subtree from the embedded program to be |
| 611 | replaced. A donor also has a subtree selected at random and this is |
| 612 | inserted into the original parent to form an offspring. |
| 613 | |
| 614 | Parameters |
| 615 | ---------- |
| 616 | donor : list |
| 617 | The flattened tree representation of the donor program. |
| 618 | |
| 619 | random_state : RandomState instance |
| 620 | The random number generator. |
| 621 | |
| 622 | Returns |
| 623 | ------- |
| 624 | program : list |
| 625 | The flattened tree representation of the program. |
| 626 | |
| 627 | """ |
| 628 | # Get a subtree to replace |
| 629 | start, end = self.get_subtree(random_state) |
| 630 | removed = range(start, end) |
| 631 | |
| 632 | if isinstance(self.program[start], _Function): |
| 633 | # Get a subtree to donate |
| 634 | donor_start, donor_end = get_subtree_func(random_state, donor) |
| 635 | elif isinstance(self.program[start], int): |
| 636 | donor_start, donor_end = get_subtree_var(random_state, donor) |
| 637 | else: |
| 638 | if get_subtree_con(random_state, donor) == None: |
| 639 | return self.program, [], [] |
| 640 | else: |
| 641 | donor_start, donor_end = get_subtree_con(random_state, donor) |
| 642 | |
| 643 | donor_removed = list(set(range(len(donor))) - |
| 644 | set(range(donor_start, donor_end))) |
| 645 | # Insert genetic material from donor |
| 646 | return (self.program[:start] + |
| 647 | donor[donor_start:donor_end] + |
| 648 | self.program[end:]), removed, donor_removed |
| 649 | |
| 650 | def subtree_mutation(self, random_state): |
| 651 | """Perform the subtree mutation operation on the program. |
no test coverage detected