コード例 #1
0
def measure_greedy():
    greedy = Greedy(Graph(0))
    avg_memory_consumption = []

    for i in range(START_COUNTING_FROM_NODE, NUM_OF_NODES):
        mem_usage_data = []

        print(f"Measuring Greedy for {i} nodes.")

        for j in range(NUM_OF_SAMPLES):
            graph = Graph(i)
            greedy.graph = graph
            starting_node = next(iter(graph.graph))

            mem_usage = memory_usage((greedy.greedy_approach, (), {
                'start_node': starting_node
            }))
            mem_usage_data.append(mem_usage[-1])

        avg_memory_consumption.append(sum(mem_usage_data) / NUM_OF_SAMPLES)

    return avg_memory_consumption
コード例 #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