Esempio n. 1
0
def p_expr(p):
    '''Expression : UnaryExpr
                              | Expression BinaryOp Expression'''
    p[0] = Node('Expression')
    if len(p) == 2:
        p[0].typeList = p[1].typeList
        p[0].placeList = p[1].placeList
        p[0].sizeList = p[1].sizeList
        p[0].code = p[1].code
    else:
        if p[1].typeList[0] != p[3].typeList[0]:
            compilation_errors.add('TypeMismatch', line_number.get()+1, 'Type should be same across binary operator')
        elif p[1].typeList[0][0] not in p[2].extra:
            compilation_errors.add('TypeMismatch', line_number.get()+1, 'Invalid type for binary expression')
        else:
            if len(p[2].typeList) > 0:
                # for boolean
                p[0].typeList = p[2].typeList
            else:
                p[0].typeList = p[1].typeList
            newVar = helper.newVar(p[0].typeList[0], p[1].sizeList[0])
            p[0].code = p[1].code
            p[0].code += p[3].code
            if len(p[2].extra) < 3:
                p[0].code.append([p[2].extra['opcode'], newVar, p[1].placeList[0], p[3].placeList[0]])
            else:
                p[0].code.append([p[2].extra['opcode'] + p[1].typeList[0][0], newVar, p[1].placeList[0], p[3].placeList[0]])
            p[0].sizeList = p[1].sizeList
            p[0].placeList.append(newVar)
Esempio n. 2
0
def sum_lists(ll_a: Node, ll_b: Node) -> Node:
    """
    Calculates the sum of numbers that represents by two linked lists

    :param ll_a: Linked list
    :param ll_b: Linked list
    :return: Linked list
    """

    n1, n2 = ll_a, ll_b
    ll = head = None
    carry = 0
    while n1 or n2:
        result = carry
        if n1:
            result += n1.value
            n1 = n1.next
        if n2:
            result += n2.value
            n2 = n2.next

        node = Node(result % 10)

        if ll is None:
            ll = head = node
        else:
            ll = ll.next = node

        carry = result // 10

    if carry:
        ll = ll.next = Node(carry)

    return head
Esempio n. 3
0
def a_star(grid:np.array,start:tuple,end:tuple) -> Tuple[list,list]:
    """ A function that searches for the shortest path in a grid using the A* algorithm

        This function searches through a grid searching for the shortest path using the A* algorithm. The function first
        sets up a VisitedNodes object (visited) and a PriorityQueue object (priority_queue) to keep track of checked
        nodes. A Node object for the first starting position is created and pushed into the priority queue.
        While there are still objects in the priority_queue, the function will pull the first item from the
        priority_queue and store it in visited. The function will check that the current node's position is not equal
        to the end position; if it does then a visited list and path list is created by calling the appropriate methods
        from visited. Otherwise, the function iterates on the current node's children, and checks if they are already in
        visited or the priority_queue, and that the child is accessible (accessible > 0). If true, then child of the
        current node is added to the priority_queue along with the child's information.

        Parameters
        ----------
            grid : np.array
                a numpy array detailing the grid
            start : tuple
                a tuple detailing the starting node's position
            end : tuple
                a tuple detailing the ending node's position
        Returns
        -------
            Tuple[list,list]
                A list of visited nodes and a list of nodes that are included in the path

    """

    # heuristics used; diagonal_distance is used by default
    def euclidean_distance(child_node, end):
        return math.sqrt((pow(child_node[0]-end[0],2))+(pow(child_node[1]-end[1],2)))

    def manhattan_distance(child_node,end):
        return (abs(child_node[0] - end[0]) + abs(child_node[1]-end[1])) * 10

    def diagonal_distance(child_node,end):
        dx = abs(child_node[0] - end[0])
        dy = abs(child_node[1] - end[1])

        return 10 * (dx+dy) + (14 - 2 * 10) * min(dx,dy)

    if grid[start[0]][start[1]] == 1 or grid[end[0]][end[1]] == 1:
        return [None],[None]

    visited = VisitedNodes()
    priority_queue = PriorityQueue()
    priority_queue.push(Node(grid, start, neighbors="8_wind", cost=0, heuristic=diagonal_distance(start, end)))

    while len(priority_queue) > 0:
        node = priority_queue.pop()
        visited._store_node(node)
        if node.pos == end:
            visited_list,path_list = visited.create_path(node.pos, start)
            return visited_list,path_list
        else:
            for child in node.children:
                if child["node"] not in visited.visited_nodes and child["node"] not in priority_queue.queue_list and child["accessibility"]:
                    priority_queue.push(Node(grid, child["node"], parent=node.pos, neighbors="8_wind", cost=child["cost"],
                                             heuristic=diagonal_distance(child["node"], end)))
    return [None],[None]
Esempio n. 4
0
def cluster_score(dp):
    trust_model = config['data']['file-trust']
    cols_use = config['parameters']['cols-to-be-used']
    df = pd.read_csv(trust_model)
    dl = df.values.tolist()
    cols = df.columns.tolist()
    root = Node(name='R', data=df, level=0, parent=None)
    nodes = []
    max_cmdk = -99999
    uni_desc = helper.describe_attrs(df)  #description of universal (root) data
    uni_len = len(df)
    sum_rate = 0
    sum_cmdk = 0
    for p in set(df['partition']):
        part_data = df[df['partition'] == p]
        part = Node(name=p, data=part_data, level=1, parent=root)
        cm_d_k = helper.category_match(
            part, dp, uni_desc, uni_len, cols
        )  # calculate category match measure for this dp to belong to this partion
        # if cm_d_k > max_cmdk: # dp should be a part of partition with highest measure of cmdk for it
        # 	max_cmdk = cm_d_k
        # 	max_part = part.name
        rate = list(part_data['trust'])[0]
        sum_rate += rate * cm_d_k
        sum_cmdk += cm_d_k
    return sum_rate / sum_cmdk
Esempio n. 5
0
def p_array_length(p):
    ''' ArrayLength : INT_LITERAL
                            | epsilon'''
    p[0] = Node('ArrayLength')
    if isinstance(p[1], str):
        p[0].extra['count'] = int(p[1])
    else:
        p[0].extra['count'] = -208016
Esempio n. 6
0
def p_break(p):
    '''BreakStmt : BREAK'''
    p[0] = Node('BreakStmt')
    scope_ = helper.getNearest('for')
    if scope_ == -1:
        compilation_errors.add('Scope Error', line_number.get()+1, 'break is not in a loop')
        return
    symTab = helper.symbolTables[scope_]
    p[0].code = [['goto', symTab.metadata['end']]]
Esempio n. 7
0
def p_continue(p):
    '''ContinueStmt : CONTINUE'''
    p[0] = Node('ContinueStmt')
    scope_ = helper.getNearest('for')
    if scope_ == -1:
        compilation_errors.add('Scope Error', line_number.get()+1, 'continue is not in a loop')
        return
    symTab = helper.symbolTables[scope_]
    p[0].code = [['goto', symTab.metadata['update']]]
Esempio n. 8
0
    def gen_list_from_array(self, lst):
        head = tail = Node(lst[0])

        for i in range(1, len(lst)):
            node = Node(lst[i])
            tail.next = node
            tail = tail.next

        return head
Esempio n. 9
0
def testing_prune_labels():
    patch = Node(0, False, False, 0, 0, 0, [1, 2, 3, 4, 5], None, {
        1: 7,
        2: 9,
        3: 6,
        4: 8,
        5: 10
    }, False)  # 3 1 4 2 5
    patch.prune_labels(1)
    print(patch.pruned_labels)
Esempio n. 10
0
def to_linked_list(lst):
    if not lst:
        return None
    head = Node(lst[0])
    current = head
    for i in range(len(lst) - 1):
        current.next = Node(lst[i + 1])
        current = current.next

    return head
Esempio n. 11
0
def p_return(p):
    '''ReturnStmt : RETURN ExpressionListPureOpt'''
    p[0] = Node('ReturnStmt')
    # TODO: return data should also be handled
    scope_ = helper.getNearest('func')
    if scope_ == -1:
        compilation_errors.add('Scope Error', line_number.get()+1, 'return is not in a function')
        return
    symTab = helper.symbolTables[scope_]
    p[0].code = [['goto', symTab.metadata['end']]]
Esempio n. 12
0
 def test_iteration_double(self):
     node1, node2, node3 = Node(1), Node(2), Node(3)
     node1.next, node2.next, node3.next = node2, node3, None
     node1.prev, node2.prev, node3.prev = None, node1, node2
     self.double.head = node1
     self.double.tail = node3
     # test __iter__
     self.assertEqual([str(i) for i in self.double], [str(i) for i in range(1, 4)])
     # test iterating over reversed
     self.assertEqual([str(i) for i in reversed(self.double)], [str(i) for i in range(3, 0, -1)])
Esempio n. 13
0
 def test_double_slicing(self):
     node1, node2, node3 = Node(1), Node(2), Node(3)
     node1.next, node2.next, node3.next = node2, node3, None
     node1.prev, node2.prev, node3.prev = None, node1, node2
     self.double.head = node1
     self.double.tail = node3
     self.assertEqual(self.double[0].value, 1)
     self.assertEqual(self.double[1].value, 2)
     self.assertEqual(self.double[2].value, 3)
     self.assertRaises(IndexError, lambda val: self.double[val], 3)
Esempio n. 14
0
def remove_node(node: LinkedList) -> None:
    """
    Removes given node from linked list

    :param node:
    :return:
    """

    if node and node.next:
        node.value = node.next.value
        node.next = node.next.next
Esempio n. 15
0
def p_array_type(p):
    '''ArrayType : LBRACK ArrayLength RBRACK ElementType'''
    p[0] = Node('ArrayType')
    if p[2].extra['count'] == -208016:
        # slice
        p[0].typeList.append(['slice', p[4].typeList[0], 0])
        p[0].sizeList.append(0)
    else:
        if p[2].extra['count'] < 0:
            compilation_errors.add('Size Error', line_number.get()+1, 'array bound must be non-negative')
            return
        p[0].typeList.append(['array', p[4].typeList[0], p[2].extra['count']])
        p[0].sizeList.append(int(p[2].extra['count']*p[4].sizeList[0]))
    p[0].name = 'ArrayType'
Esempio n. 16
0
def p_struct_type(p):
    '''StructType : STRUCT LBRACE FieldDeclRep RBRACE'''
    p[0] = Node('StructType')
    for index_ in range(len(p[3].identList)):
        if p[3].identList[index_] in p[3].identList[:index_]:
            compilation_errors.add('Redeclaration Error',line_number.get()+1, 'Field %s redeclared'%p[3].identList[index_])
            return
    p[0] = p[3]
    dict_ = {}
    offset_ = 0
    for index_ in range(len(p[3].identList)):
        dict_[p[3].identList[index_]] = {'type':p[3].typeList[index_], 'size': p[3].sizeList[index_], 'offset':offset_}
        offset_ += p[3].sizeList[index_]
    p[0].typeList = [['struct', dict_]]
    p[0].sizeList = [sum(p[3].sizeList)]
Esempio n. 17
0
    def gen_list(self, _from, _to):
        head = Node(_from)
        current = head

        if _from > _to:
            for i in range(_from - 1, _to - 1, -1):
                current.next = Node(i)
                current = current.next

        elif _from < _to:
            for i in range(_from + 1, _to + 1):
                current.next = Node(i)
                current = current.next

        return head
Esempio n. 18
0
    def step(self, frontier, explored, last_node):
        if not frontier.empty:
            current_node: Node[T] = frontier.pop()
            current_state: T = current_node.state
            if isinstance(frontier, Stack):
                self._frontier_listbox.delete(END, END)
            elif isinstance(frontier, Queue):
                self._frontier_listbox.delete(0, 0)
            self._grid[current_state.row][current_state.column] = Cell.CURRENT
            if last_node is not None:
                self._grid[last_node.state.row][last_node.state.column] = Cell.EXPLORED
            # if we found the goal, we're done
            if self.goal_test(current_state):
                path = node_to_path(current_node)
                self.mark(path)
                self._display_grid()
                return
            # check where we can go next and haven't explored
            for child in self.successors(current_state):
                if child in explored:  # skip children we already explored
                    continue
                explored.add(child)

                frontier.push(Node(child, current_node))
                # update GUI
                self._grid[child.row][child.column] = Cell.FRONTIER
                self._explored_listbox.insert(END, str(child))
                self._frontier_listbox.insert(END, str(child))
                self._explored_listbox.select_set(END)
                self._explored_listbox.yview(END)
                self._frontier_listbox.select_set(END)
                self._frontier_listbox.yview(END)
            self._display_grid()
            num_delay = int(self._interval_box.get()) * 1000
            self.root.after(num_delay, self.step, frontier, explored, current_node)
def minimum_path(graph:list, source:int, dst = None)->list:
    """Runs a Djikstra algorithm and returns the path subgraph"""
    min_tree = [Node(i) for i in range(len(graph))]
    for node in graph:
        node.weight = float("inf")
        node.parent = None
    graph[source-1].weight = 0
    queue = [graph[i] for i in range(len(graph))]
    heapq.heapify(queue)
    while(queue):
        currentNode = heapq.heappop(queue)
        #currentNode.color = Color.gray
        min_tree[currentNode.id-1] = currentNode
        if dst is not None:
            if(currentNode.id == dst):
                break
        for i in range(len(currentNode.neighbors)):
            neighbor = graph[currentNode.neighbors[i]-1]
            if neighbor.weight > (currentNode.weight + currentNode.costs[i]):
                neighbor.weight = currentNode.weight + currentNode.costs[i]
                neighbor.parent = currentNode.id
        heapq.heapify(queue)

    root = source
    if dst is not None:
        root, min_tree = minpath_reconstruction(min_tree, dst)
    else:
        root, min_tree = minpath_reconstruction(min_tree)

    return root, min_tree
def minpath_reconstruction(graph:list, dst = None)->list:
    tree = [Node(i) for i in range(1, len(graph)+1)]
    root = dst
    if dst is not None:
        last = dst
        while(graph[last-1].parent is not None):
            node = graph[last-1]
            origin = node.parent
            edge_cost = node.weight - graph[origin-1].weight
            tree[last-1].parent = origin
            tree[last-1].weight = node.weight
            tree[last-1].add(origin, edge_cost)
            tree[origin-1].add(node.id, edge_cost)
            last = origin
        if graph[last-1].weight < 1:
            root = graph[last-1]
            tree[last-1].weight = 0
    else:
        for i in range(len(graph)):
            if(graph[i].parent is not None):
                node = graph[i]
                origin = node.parent
                edge_cost = node.weight - graph[origin-1].weight
                tree[i].parent = origin
                tree[i].weight = node.weight
                tree[i].add(origin, edge_cost)
                tree[origin-1].add(node.id, edge_cost)
            elif(graph[i].weight < 1):
                root = graph[i]
                tree[i].weight = 0
    return root.id, tree
Esempio n. 21
0
def p_short_var_decl(p):
    ''' ShortVarDecl : IDENT DEFINE Expression '''
    p[0] = Node('ShortVarDecl')

    if helper.checkId(p[1],'current'):
        compilation_errors.add("Redeclaration Error", line_number.get()+1,\
            "%s already declared"%p[1])
    try:
        helper.symbolTables[helper.getScope()].add(p[1],p[3].typeList[0])
        helper.symbolTables[helper.getScope()].update(p[1], 'offset', helper.getOffset())
        helper.symbolTables[helper.getScope()].update(p[1], 'size', p[3].sizeList[0])
        helper.updateOffset(p[3].sizeList[0])
        p[0].code = p[3].code
        p[0].code.append(['=', p[1], p[3].placeList[0]])
    except:
        pass
def minimum_spanning_tree(graph:list)->list:
    mst = [Node(i) for i in range(1, len(graph)+1)]
    for node in graph:
        node.weight = float("inf")
        node.parent = None

    graph[0].weight = 0
    last = graph[0]
    queue = [graph[i] for i in range(len(graph))]
    heapq.heapify(queue)
    on_queue = [True for i in range(len(graph))]
    while(queue):
        node = heapq.heappop(queue)
        if on_queue[node.id-1]:
            for i in range(len(node.neighbors)):
                neighbor = graph[node.neighbors[i]-1]
                if(on_queue[neighbor.id-1] and (node.costs[i] < neighbor.weight)):
                    neighbor.parent = node.id
                    neighbor.weight = node.costs[i]

                heapq.heapify(queue)
            on_queue[node.id-1] = False
            mst[node.id-1] = node
            last = node

    root, mst = mst_reconstruction(mst)
    if(len(mst) != len(graph)):
        print("There's been some weird error in MST")
    #for node in mst:
    #    print(node, node.parent, node.weight)

    return root, mst
Esempio n. 23
0
def p_type_spec_rep(p):
    '''TypeSpecRep : TypeSpecRep TypeSpec SEMICOLON
                               | epsilon'''
    if len(p) == 2:
        p[0] = Node('TypeSpecRep')
        # TODO ommitting RHS why?
    else:
        p[0] = p[1]
Esempio n. 24
0
def p_binary_op(p):
    '''BinaryOp : LOR
                            | LAND
                            | RelOp
                            | AddMulOp'''
    
    if isinstance(p[1], str):
        p[0] = Node('BinaryOp')
        p[0].extra['opcode'] = p[1]
        p[0].extra['bool'] = True
        p[0].typeList.append(['bool'])
    elif p[1].name == 'RelOp':
        p[0] = p[1]
        p[0].typeList.append(['bool'])
    else:
        p[0] = p[1]
    p[0].name = 'BinaryOp'
Esempio n. 25
0
def p_basic_lit_3(p):
    '''StringLit : STRING_LITERAL'''
    p[0] = Node('StringLit')
    p[0].typeList.append(['string'])
    newVar = helper.newVar(['string'], size_mp['string'])
    p[0].code.append(['=', newVar, p[1]])
    p[0].placeList.append(newVar)
    p[0].sizeList.append(size_mp['string'])
Esempio n. 26
0
def p_basic_lit_2(p):
    '''FloatLit : FLOAT_LITERAL'''
    p[0] = Node('FloatLit')
    p[0].typeList.append(['float'])
    newVar = helper.newVar(['float'], size_mp['float'])
    p[0].code.append(['=', newVar, p[1]])
    p[0].placeList.append(newVar)
    p[0].sizeList.append(size_mp['float'])
Esempio n. 27
0
 def predict(self, snt_list, example_list, example, labeled):
     if labeled:
         return self.predict_labeled(snt_list, example)
     else:
         for tup in example:
             tup2 = (tup[0], tup[1], 'EDGE')
             if tup[1].index - tup[0].index == 1:
                 return [(tup2, 1.0)]
         return [((Node(), tup[1], 'EDGE'), 1.0)]
Esempio n. 28
0
 def bfs(self):
     self.clear()
     self._display_grid()
     # frontier is where we've yet to go
     frontier: Queue[Node[T]] = Queue()
     frontier.push(Node(self.start, None))
     # explored is where we've been
     explored: Set[T] = {self.start}
     self.step(frontier, explored, None)
Esempio n. 29
0
def p_rel_op(p):
    '''RelOp : EQL
                     | NEQ
                     | LSS
                     | GTR
                     | LEQ
                     | GEQ'''
    p[0] = Node('RelOp')
    p[0].extra['opcode'] = p[1]
    if p[1] in ['==', '!=']:
        p[0].extra['bool'] = True
        p[0].extra['int'] = True
        p[0].extra['string'] = True
        p[0].extra['float'] = True
    else:
        p[0].extra['int'] = True 
        p[0].extra['float'] = True 
        p[0].extra['string'] = True 
Esempio n. 30
0
def testing_OOP():
    patches_list = []

    for i in range(5):
        p = Node(i, True, True, i, i)
        patches_list.append(p)

    for i in range(5):
        print(patches_list[i].node_id)
Esempio n. 31
0
def p_basic_lit_4(p):
    '''BoolLit : TRUE
                    | FALSE'''
    p[0] = Node('BoolLit')
    p[0].typeList.append(['bool'])
    newVar = helper.newVar(['bool'], size_mp['bool'])
    p[0].code.append(['=', newVar, p[1]])
    p[0].placeList.append(newVar)
    p[0].sizeList.append(size_mp['bool'])
Esempio n. 32
0
def p_basic_lit_1(p):
    '''IntLit : INT_LITERAL'''
    p[0] = Node('IntLit')
    p[0].typeList.append(['int'])
    newVar = helper.newVar(['int'], size_mp['int'])

    p[0].code.append(['=', newVar, p[1]])
    p[0].placeList.append(newVar)
    p[0].sizeList.append(size_mp['int'])