import algorithms.dijkstra as dijkstra import algorithms.a_star as a_star import algorithms.bidirectional as bidirectional import dataset.generate_graph as gen_graph import dataset.generate_grid as gen_grid import time import tracemalloc import os import random as rand def do_experiments(): graph_groups1 = ["5n"] graph_groups2 = ["200", "500", "1000", "2000", "5000", "10000", "20000", "50000"] grid_groups = ["20x20", "50x50", "100x100", "150x150", "250x250", "500x500"] print("Starting graph testing") for group1 in graph_groups1: dijkstra_mean_times, dijkstra_mean_memory, dijkstra_mean_errors = [], [], [] a_star_mean_times, a_star_mean_memory, a_star_mean_errors = [], [], [] bidirectional_mean_times, bidirectional_mean_memory, bidirectional_mean_errors = [], [], [] print(group1) for group2 in graph_groups2: print(group2, end=", ") di_mean_time, di_mean_memory, di_mean_error, a_mean_time, a_mean_memory, a_mean_error, bi_mean_time, bi_mean_memory, bi_mean_error = get_measurements_graphs(group1, group2) dijkstra_mean_times.append(di_mean_time) dijkstra_mean_memory.append(di_mean_memory) dijkstra_mean_errors.append(di_mean_error) a_star_mean_times.append(a_mean_time) a_star_mean_memory.append(a_mean_memory) a_star_mean_errors.append(a_mean_error) bidirectional_mean_times.append(bi_mean_time) bidirectional_mean_memory.append(bi_mean_memory) bidirectional_mean_errors.append(bi_mean_error) print() write_output_table_graph(group1, graph_groups2, dijkstra_mean_times, dijkstra_mean_memory, dijkstra_mean_errors, a_star_mean_times, a_star_mean_memory, a_star_mean_errors, bidirectional_mean_times, bidirectional_mean_memory, bidirectional_mean_errors) # print("Starting grid testing") # dijkstra_mean_times, dijkstra_mean_memory, dijkstra_mean_errors = [], [], [] # a_star_mean_times, a_star_mean_memory, a_star_mean_errors = [], [], [] # bidirectional_mean_times, bidirectional_mean_memory, bidirectional_mean_errors = [], [], [] # for group in grid_groups: # print(group, end=", ") # di_mean_time, di_mean_memory, di_mean_error, a_mean_time, a_mean_memory, a_mean_error, bi_mean_time, bi_mean_memory, bi_mean_error = get_measurements_grids(group) # dijkstra_mean_times.append(di_mean_time) # dijkstra_mean_memory.append(di_mean_memory) # dijkstra_mean_errors.append(di_mean_error) # a_star_mean_times.append(a_mean_time) # a_star_mean_memory.append(a_mean_memory) # a_star_mean_errors.append(a_mean_error) # bidirectional_mean_times.append(bi_mean_time) # bidirectional_mean_memory.append(bi_mean_memory) # bidirectional_mean_errors.append(bi_mean_error) # write_output_table_grid(grid_groups, dijkstra_mean_times, dijkstra_mean_memory, dijkstra_mean_errors, # a_star_mean_times, a_star_mean_memory, a_star_mean_errors, bidirectional_mean_times, # bidirectional_mean_memory, bidirectional_mean_errors) def get_measurements_graphs(group1, group2): dijkstra_time, dijkstra_memory, dijkstra_weight = [], [], [] a_star_time, a_star_memory, a_star_weight = [], [], [] bidirectional_time, bidirectional_memory, bidirectional_weight = [], [], [] error_a_star, error_bi = [], [] for i in range(1, len(os.listdir("dataset/graphs/" + group1 + "/" + group2)) + 1): filename = "graph" + str(i) N, A, A_b, w = gen_graph.read_graph_from_file(filename, path="dataset/graphs/" + group1 + "/" + group2 + "/") for j in range(3): start_node_index = rand.randint(0, len(N) - 1) goal_node_index = rand.randint(0, len(N) - 1) while goal_node_index == start_node_index: goal_node_index = rand.randint(0, len(N) - 1) start_node = None goal_node = None for index, node in enumerate(N): if index == goal_node_index: goal_node = node if index == start_node_index: start_node = node if start_node is not None and goal_node is not None: break startTime = time.time() tracemalloc.start() weight = dijkstra.dijkstra_algorithm(start_node, goal_node, N, A, w)[1] dijkstra_memory.append(tracemalloc.get_traced_memory()[1]) dijkstra_time.append((time.time() - startTime)) dijkstra_weight.append(weight) tracemalloc.stop() startTime = time.time() tracemalloc.start() weight = a_star.a_star_algorithm(start_node, goal_node, N, A, w, a_star.heuristic_cost)[1] a_star_memory.append(tracemalloc.get_traced_memory()[1]) a_star_time.append((time.time() - startTime)) a_star_weight.append(weight) tracemalloc.stop() startTime = time.time() tracemalloc.start() weight = bidirectional.bidirectional_algorithm(start_node, goal_node, N, A, A_b, w, a_star.heuristic_cost)[ 1] bidirectional_memory.append(tracemalloc.get_traced_memory()[1]) bidirectional_time.append((time.time() - startTime)) bidirectional_weight.append(weight) tracemalloc.stop() for i in range(len(dijkstra_weight)): error_a_star.append((a_star_weight[i] - dijkstra_weight[i]) / dijkstra_weight[i]) error_bi.append((bidirectional_weight[i] - dijkstra_weight[i]) / dijkstra_weight[i]) return sum(dijkstra_time) / len(dijkstra_time), sum(dijkstra_memory) / len(dijkstra_memory), 0.0, \ sum(a_star_time) / len(a_star_time), sum(a_star_memory) / len(a_star_memory), sum(error_a_star) / len(error_a_star),\ sum(bidirectional_time) / len(bidirectional_time), sum(bidirectional_memory) / len(bidirectional_memory), sum(error_bi) / len(error_bi) def get_measurements_grids(group): dijkstra_time, dijkstra_memory, dijkstra_weight = [], [], [] a_star_time, a_star_memory, a_star_weight = [], [], [] bidirectional_time, bidirectional_memory, bidirectional_weight = [], [], [] error_a_star, error_bi = [], [] for i in range(1, len(os.listdir('dataset/grids/' + group + "/")) + 1): filename = "grid" + str(i) N, A, A_b, w = gen_grid.read_grid_graph_from_file(filename, path="dataset/grids/" + group + "/") for j in range(3): start_node_index = rand.randint(0, len(N) - 1) goal_node_index = rand.randint(0, len(N) - 1) while goal_node_index == start_node_index: goal_node_index = rand.randint(0, len(N) - 1) start_node = None goal_node = None for index, node in enumerate(N): if index == goal_node_index: goal_node = node if index == start_node_index: start_node = node if start_node is not None and goal_node is not None: break startTime = time.time() tracemalloc.start() weight = dijkstra.dijkstra_algorithm(start_node, goal_node, N, A, w)[1] dijkstra_memory.append(tracemalloc.get_traced_memory()[1]) dijkstra_time.append((time.time() - startTime)) dijkstra_weight.append(weight) tracemalloc.stop() startTime = time.time() tracemalloc.start() weight = a_star.a_star_algorithm(start_node, goal_node, N, A, w, a_star.heuristic_cost_manhattan)[1] a_star_memory.append(tracemalloc.get_traced_memory()[1]) a_star_time.append((time.time() - startTime)) a_star_weight.append(weight) tracemalloc.stop() startTime = time.time() tracemalloc.start() weight = bidirectional.bidirectional_algorithm(start_node, goal_node, N, A, A_b, w, a_star.heuristic_cost_manhattan)[1] bidirectional_memory.append(tracemalloc.get_traced_memory()[1]) bidirectional_time.append((time.time() - startTime)) bidirectional_weight.append(weight) tracemalloc.stop() for i in range(len(dijkstra_weight)): error_a_star.append((a_star_weight[i] - dijkstra_weight[i]) / dijkstra_weight[i]) error_bi.append((bidirectional_weight[i] - dijkstra_weight[i]) / dijkstra_weight[i]) return sum(dijkstra_time) / len(dijkstra_time), sum(dijkstra_memory) / len(dijkstra_memory), 0.0, \ sum(a_star_time) / len(a_star_time), sum(a_star_memory) / len(a_star_memory), sum(error_a_star) / len(error_a_star),\ sum(bidirectional_time) / len(bidirectional_time), sum(bidirectional_memory) / len(bidirectional_memory), sum(error_bi) / len(error_bi) def write_output_table_graph(edges_number, nodes_number_list, dijkstra_time, dijkstra_memory, dijkstra_weight, a_star_time, a_star_memory, a_star_weight, bidirectional_time, bidirectional_memory, bidirectional_weight): with open("output/graphs_times_" + edges_number + ".out", "w", encoding="utf-8") as file: file.write("\\begin{table}[]\n") file.write("\\begin{tabular}{|l|l|l|l|}\n") file.write("\\hline\n") file.write("grupa & Dijkstra & A* & Bi A* \\\\ \\hline\n") with open("output/graphs_memory_" + edges_number + ".out", "w", encoding="utf-8") as file: file.write("\\begin{table}[]\n") file.write("\\begin{tabular}{|l|l|l|l|}\n") file.write("\\hline\n") file.write("grupa & Dijkstra & A* & Bi A* \\\\ \\hline\n") with open("output/graphs_errors_" + edges_number + ".out", "w", encoding="utf-8") as file: file.write("\\begin{table}[]\n") file.write("\\begin{tabular}{|l|l|l|l|}\n") file.write("\\hline\n") file.write("grupa & Dijkstra & A* & Bi A* \\\\ \\hline\n") with open("output/graphs_times_" + edges_number + ".out", "a", encoding="utf-8") as file: for j in range(len(dijkstra_time)): file.write(" " + nodes_number_list[j] + " & " + str(round(dijkstra_time[j], 5)) + " & " + str(round(a_star_time[j], 5)) + " & " + str(round(bidirectional_time[j], 5)) + " \\\\ \\hline\n") file.write("\\end{tabular}\n") file.write("\\end{table}\n") with open("output/graphs_memory_" + edges_number + ".out", "a", encoding="utf-8") as file: for j in range(len(dijkstra_time)): file.write(" " + nodes_number_list[j] + " & " + str(round(dijkstra_memory[j], 5)) + " & " + str(round(a_star_memory[j], 5)) + " & " + str(round(bidirectional_memory[j], 5)) + " \\\\ \\hline\n") file.write("\\end{tabular}\n") file.write("\\end{table}\n") with open("output/graphs_errors_" + edges_number + ".out", "a", encoding="utf-8") as file: for j in range(len(dijkstra_time)): file.write(" " + nodes_number_list[j] + " & " + str(round(dijkstra_weight[j], 5)) + " & " + str(round(a_star_weight[j], 5)) + " & " + str(round(bidirectional_weight[j], 5)) + " \\\\ \\hline\n") file.write("\\end{tabular}\n") file.write("\\end{table}\n") def write_output_table_grid(group, dijkstra_time, dijkstra_memory, dijkstra_weight, a_star_time, a_star_memory, a_star_weight, bidirectional_time, bidirectional_memory, bidirectional_weight): with open("output/grids_times.out", "w", encoding="utf-8") as file: file.write("\\begin{table}[]\n") file.write("\\begin{tabular}{|l|l|l|l|}\n") file.write("\\hline\n") file.write("grupa & Dijkstra & A* & Bi A* \\\\ \\hline\n") with open("output/grids_memory.out", "w", encoding="utf-8") as file: file.write("\\begin{table}[]\n") file.write("\\begin{tabular}{|l|l|l|l|}\n") file.write("\\hline\n") file.write("grupa & Dijkstra & A* & Bi A* \\\\ \\hline\n") with open("output/grids_weights.out", "w", encoding="utf-8") as file: file.write("\\begin{table}[]\n") file.write("\\begin{tabular}{|l|l|l|l|}\n") file.write("\\hline\n") file.write("grupa & Dijkstra & A* & Bi A* \\\\ \\hline\n") with open("output/grids_times.out", "a", encoding="utf-8") as file: for j in range(len(dijkstra_time)): file.write( " " + group[j] + " & " + str(round(dijkstra_time[j], 5)) + " & " + str(round(a_star_time[j], 5)) + " & " + str(round(bidirectional_time[j], 5)) + " \\\\ \\hline\n") file.write("\\end{tabular}\n") file.write("\\end{table}\n") with open("output/grids_memory.out", "a", encoding="utf-8") as file: for j in range(len(dijkstra_time)): file.write(" " + group[j] + " & " + str(round(dijkstra_memory[j], 5)) + " & " + str(round(a_star_memory[j], 5)) + " & " + str(round(bidirectional_memory[j], 5)) +" \\\\ \\hline\n") file.write("\\end{tabular}\n") file.write("\\end{table}\n") with open("output/grids_weights.out", "a", encoding="utf-8") as file: for j in range(len(dijkstra_time)): file.write(" " + group[j] + " & " + str(round(dijkstra_weight[j], 5)) + " & " + str(round(a_star_weight[j], 5)) + " & " + str(round(bidirectional_weight[j], 5)) + " \\\\ \\hline\n") file.write("\\end{tabular}\n") file.write("\\end{table}\n") if __name__ == '__main__': do_experiments()