2021-10-17 11:21:09 +02:00
|
|
|
import algorithms.dijkstra as dijkstra
|
2021-11-02 15:17:18 +01:00
|
|
|
import algorithms.a_star as a_star
|
|
|
|
import algorithms.bidirectional as bidirectional
|
2022-09-25 16:41:41 +02:00
|
|
|
import dataset.generate_graph as gen_graph
|
|
|
|
import dataset.generate_grid as gen_grid
|
|
|
|
import time
|
|
|
|
import tracemalloc
|
|
|
|
import os
|
|
|
|
import random as rand
|
2021-09-04 16:19:59 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2022-09-25 16:41:41 +02:00
|
|
|
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") 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") 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") 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") 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("\\end{tabular}\n")
|
|
|
|
file.write("\\end{table}\n")
|
|
|
|
|
|
|
|
with open("output/graphs_memory.out", "a") 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("\\end{tabular}\n")
|
|
|
|
file.write("\\end{table}\n")
|
|
|
|
|
|
|
|
with open("output/graph_weights.out", "a") 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") 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") 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") 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") 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("\\end{tabular}\n")
|
|
|
|
file.write("\\end{table}\n")
|
|
|
|
|
|
|
|
with open("output/grids_memory.out", "a") 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("\\end{tabular}\n")
|
|
|
|
file.write("\\end{table}\n")
|
|
|
|
|
|
|
|
with open("output/grids_weights.out", "a") 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")
|