Get a random subtree from the program. Parameters ---------- random_state : RandomState instance The random number generator. program : list, optional (default=None) The flattened tree representation of the program. If None, the
(self, random_state, program=None)
| 562 | return self.raw_fitness_ - penalty |
| 563 | |
| 564 | def get_subtree(self, random_state, program=None): |
| 565 | """Get a random subtree from the program. |
| 566 | |
| 567 | Parameters |
| 568 | ---------- |
| 569 | random_state : RandomState instance |
| 570 | The random number generator. |
| 571 | |
| 572 | program : list, optional (default=None) |
| 573 | The flattened tree representation of the program. If None, the |
| 574 | embedded tree in the object will be used. |
| 575 | |
| 576 | |
| 577 | Returns |
| 578 | ------- |
| 579 | start, end : tuple of two ints |
| 580 | The indices of the start and end of the random subtree. |
| 581 | |
| 582 | """ |
| 583 | if program is None: |
| 584 | program = self.program |
| 585 | # Choice of crossover points follows Koza's (1992) widely used approach |
| 586 | # of choosing functions 90% of the time and leaves 10% of the time. |
| 587 | probs = np.array([0.9 if isinstance(node, _Function) else 0.1 |
| 588 | for node in program]) |
| 589 | probs = np.cumsum(probs / probs.sum()) |
| 590 | start = np.searchsorted(probs, random_state.uniform()) |
| 591 | |
| 592 | stack = 1 |
| 593 | end = start |
| 594 | while stack > end - start: |
| 595 | node = program[end] |
| 596 | if isinstance(node, _Function): |
| 597 | stack += node.arity |
| 598 | end += 1 |
| 599 | |
| 600 | return start, end |
| 601 | |
| 602 | |
| 603 | def reproduce(self): |