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 if __name__ == '__main__': current_graph = [] nodes_chosen = [] dijkstra_time, dijkstra_memory, dijkstra_weight = [], [], [] a_star_time, a_star_memory, a_star_weight = [], [], [] bidirectional_time, bidirectional_memory, bidirectional_weight = [], [], [] for i in range(1, len(os.listdir('dataset/graphs')) + 1): filename = "graph" + str(i) print(filename) N, A, A_b, w = gen_graph.read_graph_from_file(filename, path="dataset/graphs/") for j in range(3): current_graph.append(filename) 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 nodes_chosen.append((start_node, goal_node)) 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() with open("output/graphs_times.out", "w", encoding="utf-8") as file: file.write("\\begin{table}[]\n") file.write("\\begin{tabular}{|l|l|l|l|l|}\n") file.write("\\hline\n") file.write("nazwa grafu & (s, t) & Dijkstra & A* & Bi A* \\\\ \\hline\n") with open("output/graphs_memory.out", "w", encoding="utf-8") as file: file.write("\\begin{table}[]\n") file.write("\\begin{tabular}{|l|l|l|l|l|}\n") file.write("\\hline\n") file.write("nazwa grafu & (s, t) & Dijkstra & A* & Bi A* \\\\ \\hline\n") with open("output/graph_weights.out", "w", encoding="utf-8") as file: file.write("\\begin{table}[]\n") file.write("\\begin{tabular}{|l|l|l|l|l|}\n") file.write("\\hline\n") file.write("nazwa grafu & (s, t) & Dijkstra & A* & Bi A* \\\\ \\hline\n") with open("output/graphs_times.out", "a", encoding="utf-8") as file: for j in range(len(dijkstra_time)): file.write(" " + current_graph[j] + " & " + str(nodes_chosen[j]) + " & " + str(round(dijkstra_time[j], 5)) + " & " + str(round(a_star_time[j], 5)) + " & " + str(round(bidirectional_time[j], 5)) + " \\\\ \\hline\n") file.write("\\hline\n") file.write(" & średnia & " + str(round(sum(dijkstra_time) / len(dijkstra_time), 5)) + " & " + str(round(sum(a_star_time) / len(a_star_time), 5)) + " & " + str(round(sum(bidirectional_time) / len(bidirectional_time), 5)) + " \\\\ \\hline\n") file.write("\\end{tabular}\n") file.write("\\end{table}\n") with open("output/graphs_memory.out", "a", encoding="utf-8") as file: for j in range(len(dijkstra_time)): file.write(" " + current_graph[j] + " & " + str(nodes_chosen[j]) + " & " + str(dijkstra_memory[j]) + " & " + str(a_star_memory[j]) + " & " + str(bidirectional_memory[j]) + " \\\\ \\hline\n") file.write("\\hline\n") file.write(" & średnia & " + str(sum(dijkstra_memory) // len(dijkstra_memory)) + " & " + str(sum(a_star_memory) // len(a_star_memory)) + " & " + str(sum(bidirectional_memory) // len(bidirectional_memory)) + " \\\\ \\hline\n") file.write("\\end{tabular}\n") file.write("\\end{table}\n") with open("output/graph_weights.out", "a", encoding="utf-8") as file: for j in range(len(dijkstra_time)): file.write(" " + current_graph[j] + " & " + str(nodes_chosen[j]) + " & " + str(dijkstra_weight[j]) + " & " + str(a_star_weight[j]) + " & " + str(bidirectional_weight[j]) + " \\\\ \\hline\n") file.write("\\end{tabular}\n") file.write("\\end{table}\n") current_graph = [] nodes_chosen = [] dijkstra_time, dijkstra_memory, dijkstra_weight = [], [], [] a_star_time, a_star_memory, a_star_weight = [], [], [] bidirectional_time, bidirectional_memory, bidirectional_weight = [], [], [] for i in range(1, len(os.listdir('dataset/grids')) + 1): filename = "grid" + str(i) print(filename) N, A, A_b, w = gen_grid.read_grid_graph_from_file(filename, path="dataset/grids/") for j in range(3): current_graph.append(filename) 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 nodes_chosen.append((start_node, goal_node)) 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() 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|l|}\n") file.write("\\hline\n") file.write("nazwa siatki & (s, t) & 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|l|}\n") file.write("\\hline\n") file.write("nazwa siatki & (s, t) & 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|l|}\n") file.write("\\hline\n") file.write("nazwa siatki & (s, t) & 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( " " + current_graph[j] + " & " + str(nodes_chosen[j]) + " & " + str(round(dijkstra_time[j], 5)) + " & " + str(round(a_star_time[j], 5)) + " & " + str(round(bidirectional_time[j], 5)) + " \\\\ \\hline\n") file.write("\\hline\n") file.write(" & średnia & " + str(round(sum(dijkstra_time) / len(dijkstra_time), 5)) + " & " + str(round(sum(a_star_time) / len(a_star_time), 5)) + " & " + str(round(sum(bidirectional_time) / len(bidirectional_time), 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(" " + current_graph[j] + " & " + str(nodes_chosen[j]) + " & " + str(dijkstra_memory[j]) + " & " + str(a_star_memory[j]) + " & " + str(bidirectional_memory[j]) + " \\\\ \\hline\n") file.write("\\hline\n") file.write(" & średnia & " + str(sum(dijkstra_memory) // len(dijkstra_memory)) + " & " + str(sum(a_star_memory) // len(a_star_memory)) + " & " + str(sum(bidirectional_memory) // len(bidirectional_memory)) + " \\\\ \\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(" " + current_graph[j] + " & " + str(nodes_chosen[j]) + " & " + str(dijkstra_weight[j]) + " & " + str(a_star_weight[j]) + " & " + str(bidirectional_weight[j]) + " \\\\ \\hline\n") file.write("\\end{tabular}\n") file.write("\\end{table}\n")