Exemplo n.º 1
0
    def __copy__(self):
        """Performs a deep copy of this object, except for the state evaluator."""
        other = type(self)(self.config)
        super(base_controller_class.BaseController, other).__init__()
        other.config = self.config
        other.max_fp_constant = float(self.config.settings['max fp constant'])
        other.state_evaluator = tree_class.Tree(self.config)
        other.state_evaluator.list[:] = [tree_class.TreeNode(node.index, node.value) if node else None for node in self.state_evaluator]

        return other
    def init_state_evaluator(self):
        """Initializes this controller's state evaluator tree using the ramped half-and-half method."""
        target_height_met = False

        def init_state_evaluator_recursive(parent_node, current_depth=1):
            nonlocal target_height_met
            nonlocal allow_premature_end

            if current_depth + 1 == target_height:
                # End this branch
                target_height_met = True
                self.state_evaluator.add_node_left(
                    parent_node, self.get_rand_terminal_node())
                self.state_evaluator.add_node_right(
                    parent_node, self.get_rand_terminal_node())
                return

            if allow_premature_end and target_height_met and current_depth < target_height and random.random(
            ) < float(
                    self.config.settings['pacman premature end probability']):
                # Prematurely end this branch
                self.state_evaluator.add_node_left(
                    parent_node, self.get_rand_terminal_node())
                self.state_evaluator.add_node_right(
                    parent_node, self.get_rand_terminal_node())
                return

            # Continue constructing the tree
            self.state_evaluator.add_node_left(parent_node,
                                               self.get_rand_function_node())
            self.state_evaluator.add_node_right(parent_node,
                                                self.get_rand_function_node())

            init_state_evaluator_recursive(
                self.state_evaluator.get_left_child(parent_node),
                current_depth + 1)
            init_state_evaluator_recursive(
                self.state_evaluator.get_right_child(parent_node),
                current_depth + 1)

        target_height = random.randint(
            2, int(self.config.settings['pacman max tree generation height']))
        self.state_evaluator = tree_class.Tree(self.config,
                                               self.get_rand_function_node())

        if random.random() < float(
                self.config.settings['ramped half-and-half probability']):
            # Use full initialization method
            allow_premature_end = False

        else:
            # Use grow initialization method
            allow_premature_end = True

        init_state_evaluator_recursive(self.state_evaluator.get_root())