Example #1
0
def test_random():
    """
        Test all approaches on random graph.
    """
    num_of_nodes = 7
    graph = Graph(num_of_nodes)

    starting_node = next(iter(graph.graph))

    print("{:-^100}".format(" TESTING RANDOM PATH "))

    dfs = DFS(graph)
    paths_dfs = dfs.depth_first_search(starting_node)
    print("DEPTH FIRST SEARCH")
    print("Number of Hamilton Cycles found:", len(paths_dfs))
    print("Shortest path:", graph.get_shortest_path(paths_dfs), end="\n\n")

    bfs = BFS(graph)
    paths_bfs = bfs.breadth_first_search(starting_node)
    print("BREADTH FIRST SEARCH")
    print("Number of Hamilton Cycles found:", len(paths_bfs))
    print("Shortest path:", graph.get_shortest_path(paths_bfs), end="\n\n")

    greedy = Greedy(graph)
    path_greedy = greedy.greedy_approach(starting_node)
    print("GREEDY APPROACH")
    print("Path length:",
          (path_greedy, graph.calculate_path_length(path_greedy)),
          end="\n\n")

    a_star_custom = AStarCustom(graph)
    path_a_star = a_star_custom.a_star(starting_node)
    print("A* APPROACH")
    print("Path length:",
          (path_a_star, graph.calculate_path_length(path_a_star)),
          end="\n")

    print("{:-^100}".format(" END OF TEST "), end="\n\n")
Example #2
0
def try_all_approaches(num_of_samples: int, num_of_nodes: int):
    """
        Execute all approaches on the same graphs for given number of nodes and samples (the more samples the more
        statistically accurate results).
            Returns: Achieved path lengths and execution times for all approaches.
    """
    graph = Graph(0)

    dfs = DFS(graph)
    dfs_path_lengths = []
    dfs_execution_times = []

    bfs = BFS(graph)
    bfs_path_lengths = []
    bfs_execution_times = []

    greedy = Greedy(graph)
    greedy_path_lengths = []
    greedy_execution_times = []

    a_star_custom = AStarCustom(graph)
    a_star_path_lengths = []
    a_star_execution_times = []
    for i in range(a_star_custom.num_of_heuristics):
        a_star_path_lengths.append([])
        a_star_execution_times.append([])

    for j in range(num_of_samples):
        graph = Graph(num_of_nodes)
        starting_node = next(iter(graph.graph))

        start = time.time()
        dfs.graph = graph
        paths_dfs = dfs.depth_first_search(starting_node)
        dfs_path_lengths.append(Graph.get_shortest_path(paths_dfs)[1])
        execution_time = time.time() - start
        dfs_execution_times.append(execution_time)

        start = time.time()
        bfs.graph = graph
        paths_bfs = bfs.breadth_first_search(starting_node)
        bfs_path_lengths.append(Graph.get_shortest_path(paths_bfs)[1])
        execution_time = time.time() - start
        bfs_execution_times.append(execution_time)

        start = time.time()
        greedy.graph = graph
        path_greedy = greedy.greedy_approach(starting_node)
        greedy_path_lengths.append(Graph.calculate_path_length(path_greedy))
        execution_time = time.time() - start
        greedy_execution_times.append(execution_time)

        for i in range(a_star_custom.num_of_heuristics):
            start = time.time()
            a_star_custom.graph = graph
            a_star_custom.heuristic_choice = i
            path_a_star = a_star_custom.a_star(starting_node)
            execution_time = time.time() - start
            a_star_path_lengths[i].append(
                Graph.calculate_path_length(path_a_star))
            a_star_execution_times[i].append(execution_time)

    dfs_avg_results = sum(dfs_path_lengths) / num_of_samples, sum(
        dfs_execution_times) / num_of_nodes, "DFS Recurrent"
    bfs_avg_results = sum(bfs_path_lengths) / num_of_samples, sum(
        bfs_execution_times) / num_of_nodes, "BFS"
    greedy_avg_results = sum(greedy_path_lengths) / num_of_samples, sum(
        greedy_execution_times) / num_of_nodes, "Greedy"
    a_star_avg_results = []
    for i in range(a_star_custom.num_of_heuristics):
        label = "A* heuristic " + str(i)
        a_star_avg_results.append(
            (sum(a_star_path_lengths[i]) / num_of_samples,
             sum(a_star_execution_times[i]) / num_of_samples, label))

    data = [dfs_avg_results, bfs_avg_results, greedy_avg_results]
    data.extend(a_star_avg_results)

    return data