Beispiel #1
0
    def evolve(self, X, y, n_iter=7, min_size_prune=1):
        self.X = X
        self.y = y
        for i in range(n_iter):
            #print("TAO iter ", i, " di ", n_iter)
            for depth in reversed(range(self.classification_tree.depth + 1)):
                #print("Ottimizzo depth", depth, "....")
                T = self.classification_tree
                nodes = ClassificationTree.get_nodes_at_depth(depth, T)
                #print ([node.id for node in nodes])

                for node in nodes:
                    self.optimize_nodes(node)
                #pool = Pool(4)
                #pool.map(self.optimize_nodes, nodes)
                #pool.close()
                #pool.join()

                #for node in nodes:
                #self.optimize_nodes(node)

                #Rimetto apposto i punti associati ad ogni nodo
                self.classification_tree.build_idxs_of_subtree(
                    X, range(len(X)), T.tree[0], oblique=T.oblique)
        #Effettua il pruning finale per togliere dead branches e pure subtrees
        #self.prune(min_size = min_size_prune)
        ClassificationTree.restore_tree(self.classification_tree)
def optimize_crossover(tree_a, tree_b, tree_c, depth, X, labels, oblique):
    d = random.choice(range(depth))
    #for d in reversed(range(depth)):
    nodes_a = ClassificationTree.get_nodes_at_depth(d, tree_a)
    nodes_a = [node for node in nodes_a if not node.is_leaf]
    all_nodes = ClassificationTree.get_nodes_at_depth(d, tree_a)
    all_nodes.extend(ClassificationTree.get_nodes_at_depth(d, tree_b))
    all_nodes.extend(ClassificationTree.get_nodes_at_depth(d, tree_c))
    all_nodes = [node for node in all_nodes if not node.is_leaf]
    #print (node.id for node in nodes_a)
    #worst = nodes_a[np.argmax([ClassificationTree.misclassification_loss(node, X, labels, node.data_idxs, oblique) for node in nodes_a])]
    for node in nodes_a:
        if len(node.data_idxs) > 0:
            best = find_best_branch(all_nodes,  node.data_idxs, X, labels, oblique)
            best.data_idxs = node.data_idxs
            ClassificationTree.replace_node(node, best, tree_a)
def crossover(tree, trees, CR, X):
    partners = random.sample(trees, 3)
    partners = [ClassificationTree.copy_tree(partners[0]), ClassificationTree.copy_tree(partners[1]), ClassificationTree.copy_tree(partners[2])]
    tree_nodes = list(tree.tree.values())
    d = random.choice(range(1, depth))
    #for d in reversed(range(tree.depth-1)):
    nodes_at_depth = ClassificationTree.get_nodes_at_depth(d, tree)
    other_nodes_at_depth = ClassificationTree.get_nodes_at_depth(d, partners[0])
    #other_nodes_at_depth.extend(ClassificationTree.get_nodes_at_depth(d, partners[1]))
    #other_nodes_at_depth.extend(ClassificationTree.get_nodes_at_depth(d, partners[2]))
    #for node in nodes_at_depth:
    node = random.choice(nodes_at_depth)
    p = np.random.uniform()
    if p < CR:
        choice = random.choice(other_nodes_at_depth)

        if choice.left_node != None and not choice.left_node.is_leaf and choice.right_node != None and not choice.right_node.is_leaf:
            p2 = np.random.uniform()
            if p2 < 0.5:
                node.feature = choice.left_node.feature
                node.threshold = choice.left_node.threshold
                #ClassificationTree.replace_node(node, choice.left_node, tree)
            else:
                node.feature = choice.right_node.feature
                node.threshold = choice.right_node.threshold
                #ClassificationTree.replace_node(node, choice.right_node, tree)

        else:
            #ClassificationTree.replace_node(node, choice, tree)
            node.feature = choice.feature
            node.threshold = choice.threshold
        '''
        if choice.parent_id != -1:
            node.feature = tree.tree[choice.parent_id].feature
            node.threshold = tree.tree[choice.parent_id].threshold
        '''

    else:
        node.feature = random.choice(range(len(X[0])))
        node.threshold = 0