def get_grandchildren(node, position):
    if position == 0:
        if node.left_grandchildren is None:
            node.left_grandchildren = set(full_flatten(node.children[0]))
        return node.left_grandchildren
    else:
        if node.right_grandchildren is None:
            node.right_grandchildren = set(full_flatten(node.children[1]))
        return node.right_grandchildren
Beispiel #2
0
 def full_right(self):
     if self._full_right is None:
         self._full_right = [
             NestedClassWrapper(c)
             for c in full_flatten(self.solution.children[1])
         ]
         self._full_right.sort(key=lambda x: hash(x))
     return self._full_right
    def run(self):
        root_nodes = {}
        for node in full_flatten(self.optimal_solutions):
            self.signature_update(root_nodes, node)

        for signature, nodes in root_nodes.items():
            self.root.mark(signature, nodes)

            for c in self.recurse(self.root):
                yield self.enum_tree.traverse()
Beispiel #4
0
    def merge(first, second):
        if NestedClass.is_empty(second):
            return first
        if NestedClass.is_empty(first):
            return second

        children = set()
        for solution in [first, second]:
            for child in full_flatten(solution):
                child_wrapped = NestedClassWrapper(child)
                children.add(child_wrapped)

        NestedClassWrapper.reduce(children)

        for a in children:
            for b in children:
                if a < b or a == b:
                    continue
        return NestedClassWrapper.unwrap(children)
    def fill_matrices_at(self, parasite, host):
        association = Association(parasite, host)
        row, column = parasite.index, host.index
        event = self.events[parasite]

        if host.is_leaf():
            if event == NestedSolution.DUPLICATION:
                best_solution = self.duplication_leaf_solution(
                    parasite, host, association)
            elif event == NestedSolution.HOST_SWITCH:
                best_solution = self.transfer_solution(parasite, host,
                                                       association)
            else:
                best_solution = self.solution_generator.empty_solution()

            self.main_matrix[row][column] = best_solution
            self.subtree_matrix[row][column] = best_solution

        else:
            if event == NestedSolution.DUPLICATION:
                best_solution = self.duplication_solution(
                    parasite, host, association)
            elif event == NestedSolution.HOST_SWITCH:
                best_solution = self.transfer_solution(parasite, host,
                                                       association)
            else:
                best_solution = self.cospeciation_solution(
                    parasite, host, association)

            self.main_matrix[row][column] = best_solution

            loss_solution_left, loss_solution_right = self.subtree_loss_solutions(
                parasite, host)
            best_subtree_solution = self.solution_generator.best_solution(
                [best_solution, loss_solution_left, loss_solution_right])
            self.subtree_matrix[row][column] = best_subtree_solution

        # force the given mapping
        if self.task == 3:
            h = self.mapping[parasite]
            if h is not None:
                if host != h:
                    self.main_matrix[row][
                        column] = self.solution_generator.empty_solution()
                children = []
                for node in full_flatten(self.subtree_matrix[row][column]):
                    if node.cost < float('Inf'):
                        if node.association.host == h:
                            children.append(node)

                if not children:
                    self.subtree_matrix[row][
                        column] = self.solution_generator.empty_solution()
                elif len(children) == 1:
                    self.subtree_matrix[row][column] = children[0]
                else:
                    # this is the only place where a solution object is created outside of a solution generator
                    self.subtree_matrix[row][column] = NestedSolution(
                        children[0].cost,
                        None,
                        NestedSolution.MULTIPLE,
                        None,
                        self.accumulate,
                        children=children)