Exemplo n.º 1
0
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
Exemplo n.º 2
0
def optimize_evolution(tree, trees, X, labels, X_valid, y_valid, CR):
    trial = ClassificationTree.copy_tree(tree)
    '''
    n_nodes = len(X[0])/(2**tree.depth - 1)
    if n_nodes < 1:
        n_nodes = 1


    p = np.random.uniform()
    for node in nodes:
        if p < CR and not node.is_leaf:
            node.feature = np.random.randint(0, len(X[0]))
            node.threshold = np.random.uniform(np.min(X[node.feature]),np.max(X[node.feature]))
    '''
    crossover(trial, trees, CR, X)
    ClassificationTree.build_idxs_of_subtree(X, range(len(labels)), trial.tree[0], oblique)
    #partners = random.sample(trees, 2)
    #optimize_crossover(trial, ClassificationTree.copy_tree(partners[0]), ClassificationTree.copy_tree(partners[1]), depth, X, labels, oblique)
    tao_opt(trial, X, labels)
    loss = regularized_loss(trial.tree[0], X, labels, X_valid, y_valid, range(len(labels)), oblique)
    #Se il nuovo individuo è migliore del padre li scambio
    if loss < regularized_loss(tree.tree[0], X, labels, X_valid, y_valid, range(len(labels)), oblique):
    #if loss < ClassificationTree.misclassification_loss(tree.tree[0], X, labels, range(len(labels)), oblique):
        tree = trial