Exemplo n.º 1
0
def handle_tree(args, configs):
    from Tree import tree

    parser = argparse.ArgumentParser(description=cmd_description_tree,
                                     usage=cmd_usage_tree)
    if tree_args is not None:
        [parser.add_argument(*arg[0], **arg[1]) for arg in tree_args]
    my_args = parser.parse_args(args)

    tree(my_args)
Exemplo n.º 2
0
def game():
    board = np.zeros((6, 7), dtype=int)
    ai = np.zeros((6, 7), dtype=int)
    player = np.zeros((6, 7), dtype=int)
    draw(board)
    i = 0
    j = 0
    turn = input("0 to play first, 1 to play second ")
    while True:
        order = int(turn)
        if order == 0:
            col = input("Enter Col: ")
            if i == 0:
                _, board = make_move(int(col), board, 4)
                draw(np.flipud(board))
                root = tree(board, turn)
                _ = alphabeta(root, 0, False, -999999, 999999)
                draw(np.flipud(play.br))
            else:
                _, board = make_move(int(col), play.br, 4)
                draw(np.flipud(board))
                root = tree(board, turn)
                _ = alphabeta(root, 0, False, -999999, 999999)
                draw(np.flipud(play.br))
            i = i + 1
        else:
            if i == 0:
                _, board = make_move(3, board, 1)
                draw(np.flipud(board))
            else:
                col = input("Enter Col:: ")
                if j == 0:
                    _, board = make_move(int(col), board, 4)
                    draw(np.flipud(board))
                    root = tree(board, turn)
                    _ = alphabeta(root, 0, False, -999999, 999999)
                    draw(np.flipud(play.br))
                else:
                    _, board = make_move(int(col), play.br, 4)
                    draw(np.flipud(board))
                    root = tree(board, turn)
                    _ = alphabeta(root, 0, False, -999999, 999999)
                    draw(np.flipud(play.br))
                    value = connect4(np.flipud(play.br))
                    if value == 1:
                        print("You Lose")
                        break
                j = j + 1
            i = i + 1
Exemplo n.º 3
0
def game():

    board = np.zeros((6, 7), dtype=int)
    print(" ")
    print(" ")
    draw(board)
    c = input((
        "please press pl if you want to start or press co if you want computer to play"
    ))
    if c == "pl":
        i = 0
    else:
        i = 1
    print(
        "please enter the column number you want to play in every turn from 0 to 6"
    )
    while True:
        print(" ")
        print(" ")
        col = input(" ")
        if i == 0:

            _, board = make_move(int(col), board, 4)
            print(" ")
            print(" ")
            draw(np.flipud(board))

            root = tree(board)

            _ = alphabeta(root, 0, False, -999999, 999999)
            print(" ")
            print(" ")
            draw(np.flipud(play.br))

        else:

            _, board = make_move(int(col), play.br, 4)
            print(" ")
            print(" ")
            draw(np.flipud(board))

            root = tree(board)

            _ = alphabeta(root, 0, False, -999999, 999999)
            print(" ")
            print(" ")
            draw(np.flipud(play.br))

        i = i + 1
Exemplo n.º 4
0
    def __init__(self, symbol, probability, treeNode=None):
        self._symbol = symbol
        self._probability = probability

        if treeNode is None:
            self._root = tree()
        else:
            self._root = treeNode
Exemplo n.º 5
0
 def __init__(self):
     self.file = ''
     self.declerations = []
     self.listOfTags = []
     self.listOfText = []
     self.listOfAll = []
     self.xmlTree = tree()
     self.numberOfComments = 0
     self.declerationsComments = 0
     self.errors = 0
Exemplo n.º 6
0
def get_dep_oracle(heads):
    """
    get shift-reduce actions
    """
    if len(heads) < 1:
        raise ValueError('length of heads should be larger than 0')
    if not isinstance(heads[0], int):
        heads = [int(head) for head in heads]
    arcs = [(heads[mod], mod + 1) for mod in range(len(heads))]
    action = []
    t = tree(range(len(heads) + 1), arcs)

    buffer = range(1, len(arcs) + 1)
    buffer.reverse()
    stack = []

    while not (len(stack) == 1 and len(buffer) == 0):
        if len(stack) < 2:
            # shift
            stack.append(buffer.pop())
            action.append(SHIFT)
        else:
            i = stack[-2]
            j = stack[-1]
            child = None
            if (i, j) in arcs and t.remove_node(j):
                # reduce_r
                action.append(REDUCE_R)
                child = j
                stack.remove(child)
                continue
            if (j, i) in arcs and t.remove_node(i):
                # reduce_l
                action.append(REDUCE_L)
                child = i
                stack.remove(child)
                continue
            if not child:
                # shift
                if len(buffer) == 0:
                    # non projective dependency tree
                    raise ValueError(
                        'Encounter a non-projective/non-single-head tree')
                stack.append(buffer.pop())
                action.append(SHIFT)
    assert len(heads) * 2 - 1 == len(action)
    return action
Exemplo n.º 7
0
    huffman('b', 0.16),
    huffman('c', 0.15),
    huffman('d', 0.19),
    huffman('e', 0.040)
]

heap = minheap(huffman('*', 0.00))

for sym in input:
    heap.insert(sym)

while len(heap) > 1:
    h1 = heap.delete()
    h2 = heap.delete()

    newTree = tree()
    newTree.setLeft(h1)
    newTree.setRight(h2)

    #Star Represents Combined Symbols
    newSym = huffman('*', h1.getProb() + h2.getProb(), newTree)

    heap.insert(newSym)

ansTree = heap.delete()


def printCode(string, treeNode):
    if treeNode is None:
        return
    if treeNode.getTree().getLeft() is not None:
Exemplo n.º 8
0
                       y,
                       y_type,
                       n_classes,
                       n_retry,
                       number_of_trees=100,
                       F=1,
                       min_leaf_size=1)
forest.calculateOutOfBagError()

#~ plt.figure()
#~ plt.plot(y0data[:,0],y0data[:,1],'go')
#~ plt.plot(y1data[:,0],y1data[:,1],'rx')
#~ plt.axis([-0.5,1.5,-0.5,6.5])
#~ plt.show()

t = tree(data, data_type, y, y_type, n_classes, f_num="TEST", f_cat="TEST")
#~ t.init("TEST","TEST")
root = t.root
left = root.left
right = root.right


def run_testerror():
    data = np.array([[0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 5], [0, 6],
                     [1, 3], [1, 4]])
    y = np.array([0, 0, 0, 1, 1, 0, 0, 1, 1])
    data_type = np.zeros(9)
    y_type = 1
    n_classes = np.array([2, 7])

    y0idx = y == 0
Exemplo n.º 9
0
data = sku.shuffle(data, random_state=967).reset_index(drop=True)
training_set = data.iloc[0:1400].reset_index(drop=True)
validation_set = data.iloc[1400:2000].reset_index(drop=True)
test_set = data.iloc[2000:].reset_index(drop=True)
# preparo i parametri da passare a DT_Learn
attr = {}
attr['A'] = np.unique(data['A'])
attr['B'] = np.unique(data['B'])
attr['C'] = np.unique(data['C'])
attr['D'] = np.unique(data['D'])
attr['E'] = np.unique(data['E'])
attr['F'] = np.unique(data['F'])
pData = []
# creo l'albero di decisione
# albero non potato
tree0 = tree(DT_Learn(training_set, attr, pData))
print("Albero Non Potato")
print("Nodi: ", end="")
tree0.countNodes()
print("")
# testo l'albero sul Training-Set
err = TestData(tree0, training_set)
print("Errori sul Training-Set: ", err)
print("Errore Percentuale sul Training-Set: ", (round(
    (err / training_set.__len__()) * 100, 4)), "%")
print("")
# testo l'albero sul Test-Set
err = TestData(tree0, test_set)
print("Errori sul Test-Set: ", err)
print("Errore Percentuale sul Test-Set: ", (round(
    (err / test_set.__len__()) * 100, 4)), "%")