예제 #1
0
def hc_expand(heap):

    path = heap.pop()
    nodes = path.get_vertexes()
    # nodes.reverse()  # must reverse for the new path to work
    node_to_be_expanded = path.get_end()

    connections = node_to_be_expanded.get_connection()
    connections = list(connections)
    connections.sort(key=lambda x: x.id, reverse=False)

    for c in connections:
        if c not in nodes:  # because node in 'nodes' are 'visited'

            new_path = Path()

            for n in nodes:
                # print("adding to new path: " + n.id)
                new_path.add_vertex(n)

            new_path.add_vertex(c)
            heap.push_hc(new_path)

    heap.hc_sort(
    )  # find whether path to explore next after pushing all children
예제 #2
0
def bms_expand(heap, beam_width, visited):

    if heap.size() > beam_width:
        # print("Before trimming: "+ heap.str_heuristic())

        # heap.heuristic_sort()
        heap.trim(beam_width)

        # print("After trimming: " + heap.str_heuristic())

    path = heap.pop()

    nodes = path.get_vertexes()
    # nodes.reverse()  # must reverse for the new path to work
    node_to_be_expanded = path.get_end()

    connections = node_to_be_expanded.get_connection()
    connections = list(connections)
    connections.sort(key=lambda x: x.id, reverse=False)

    for c in connections:
        if c not in nodes and c not in visited:  # because node in 'nodes' are 'visited'

            new_path = Path()

            for n in nodes:
                # print("adding to new path: " + n.id)
                new_path.add_vertex(n)

            new_path.add_vertex(c)
            heap.push_bms(
                new_path)  # use the same push as HC (simple insertion)
예제 #3
0
    def dfs_search(self, source, dest):
        '''
        Finds all paths from the source vertex to the dettination vertex,
        creates a Path object for each of these paths, and adds those Paths
        to dfs_paths in order of discovery.

        @param sourcs: The ID of the source vertex.
        @param dest: The ID of the destination vertex.
        '''
        stack = [source]
        while (len(stack) > 0):
            current_vertex = self.vertex_list[stack[len(stack)-1]]
            next = current_vertex.get_next()
            if (next == dest):
                path = Path()
                path.add_vertices(stack)
                path.add_vertex(next)
                self.dfs_paths.append(path)
            elif (next != -1):
                stack.append(next)
            else:
                self.vertex_list[stack.pop()].reset_visited()
예제 #4
0
def General_Search(graph, search_method, d):

    start = time.time()

    print("Running: " + search_method)
    if search_method == 'IDDFS':
        print("L = " + str(d) + "     Expanded     Queue")
    else:
        print("         Expanded     Queue")

    # root = g.get_vertex('S')
    # destination = g.get_vertex('G')

    root = g.get_vertex('S')
    destination = g.get_vertex('G')

    # queue for unweighted search
    queue = Queue()
    queue.push([root])

    #heap for weighted search
    path = Path()
    path.add_vertex(root)
    heap = Heap()
    heap.push(path)

    visited = []  # visited used by BMS

    while 1:

        if search_method == 'UCS' or search_method == 'GS' or search_method == 'AS'\
                or search_method == 'HC' or search_method == 'BMS':
            nodes = heap.get_left_peek().get_vertexes()
            nodes.reverse()
        else:
            nodes = queue.get_left_peek()  # get the left peek without popping

        print('         ' + nodes[0].id, end='              ')
        # use the given search method
        # DFS
        if search_method == 'DFS':
            print(queue)
            dfs_expand(queue)
        # BFS
        elif search_method == 'BFS':
            print(queue)
            bfs_expand(queue)

        elif search_method == "DLS":
            print(queue)
            dls_expand(queue, 2)

        elif search_method == 'IDDFS':
            print(queue)
            dls_expand(queue, d)

        elif search_method == 'UCS':
            print(heap)
            ucs_expand(heap)

        elif search_method == 'GS':
            print(heap.str_heuristic())
            gs_expand(heap)

        elif search_method == 'AS':
            print(heap.str_a_star())
            as_expand(heap)

        elif search_method == 'HC':
            # if len(nodes) > 1:
            #     unexplored_children = list(nodes[0].get_connection())
            #     unexplored_children.remove(nodes[1])
            #
            #     # print(unexplored_children)
            #
            #     if len(unexplored_children) != 0: # there is unvisited node(s) from this node
            #         print('         ' + nodes[0].id, end='              ')
            #         print(heap.str_heuristic())  # use the same on as the Greedy Search
            #
            #         hc_expand(heap)
            #
            # else:
            print(heap.str_heuristic())  # use the same on as the Greedy Search

            hc_expand(heap)

        elif search_method == 'BMS':
            visited.append(nodes[0])
            print(heap.str_heuristic())  # use the same on as the Greedy Search
            bms_expand(heap, 2, visited)

        # the if's must be in this order: check destination before searching
        # otherwise IDDFS won't work
        if nodes[0] == destination:
            end = time.time()
            print(
                "         goal reached! " + "Time taken by " + search_method +
                " is", end - start, "seconds")
            return nodes[0]

        if queue.isEmpty() or heap.isEmpty():
            if search_method == 'IDDFS':
                d = d + 1
                General_Search(g, 'IDDFS', d)  # use recursion for IDDFS

            # print("No solution, terminated")
            return