244 lines
12 KiB
Python
244 lines
12 KiB
Python
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():
|
|
current_group = []
|
|
dijkstra_mean_time, dijkstra_mean_memory, dijkstra_mean_error = [], [], []
|
|
a_star_mean_time, a_star_mean_memory, a_star_mean_weight = [], [], []
|
|
bidirectional_mean_time, bidirectional_mean_memory, bidirectional_mean_weight = [], [], []
|
|
graph_groups1 = os.listdir("dataset/grids/")
|
|
graph_groups2 = ["200", "500", "1000", "2000", "5000", "10000", "20000", "50000"]
|
|
grid_groups = os.listdir("dataset/grids/")
|
|
print(grid_groups)
|
|
|
|
|
|
def get_measurements_graph(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)
|
|
print(filename)
|
|
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, \
|
|
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():
|
|
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):
|
|
|
|
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()
|
|
|
|
|
|
def write_output_table_graph():
|
|
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("grupa & (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("grupa & (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")
|
|
|
|
|
|
def write_output_table_grid():
|
|
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")
|
|
|
|
|
|
if __name__ == '__main__':
|
|
do_experiments()
|