Visualization implemented

main
Gasper Spagnolo 2022-11-12 22:36:54 +01:00
parent dc7634ecc2
commit 2fb92caa42
6 changed files with 119 additions and 7 deletions

View File

@ -74,9 +74,9 @@ class Maze:
self.log_experiment()
#print("The shortest path is", self.shortest_path, self.ga_iteration)
print("The shortest path is", self.shortest_path, self.ga_iteration)
#self.print_shortest_path()
self.print_shortest_path()
def walk_through_maze(self, solution_matrix, critical_situation):
queue = [[self.start_pos]]

View File

@ -89,13 +89,13 @@ class Maze:
gene_space=[0, 1])
ga_instance.run()
self.end_time = time.time()
self.log_experiment()
#self.log_experiment()
solution, solution_fitness, solution_idx = ga_instance.best_solution()
#print("The shortest path is", self.shortest_path, self.ga_iteration)
#self.print_shortest_path()
print("The shortest path is", self.shortest_path, self.ga_iteration)
self.print_shortest_path()
def walk_through_maze(self, solution_matrix, critical_situation):
queue = [[self.start_pos]]
@ -170,7 +170,7 @@ class Maze:
f.close()
def read_mazes():
with open('./mazes_harder.txt', 'r') as f:
with open('./mazes_classic.txt', 'r') as f:
mazes = []
maze = []
for line in f:
@ -227,7 +227,7 @@ def main():
mazes = []
text_mazes = read_mazes()
LOGFILE = 'log_t2_harder.txt'
prepare_log(LOGFILE)
#prepare_log(LOGFILE)
for i in range(len(text_mazes)):
print('MAZE: ', i)
maze_ix = i

112
a1/code/visualization.py Normal file
View File

@ -0,0 +1,112 @@
from matplotlib import pyplot as plt
def read_classic_data():
task_one_classic_data = []
task_two_classic_data = []
with open("log_t1_classic.txt", "r") as f:
# Read csv file
data = f.read()
# Split data into lines
data = data.splitlines()
# Split lines into columns
data = [line.split(",") for line in data]
data = data[1:]
# Remove third column
for row in data:
task_one_classic_data.append(row[:2])
with open("log_t2_classic.txt", "r") as f:
# Read csv file
data = f.read()
# Split data into lines
data = data.splitlines()
# Split lines into columns
data = [line.split(",") for line in data]
data = data[1:]
# Remove third column
for row in data:
task_two_classic_data.append(row[:2])
return task_one_classic_data, task_two_classic_data
def plot_classic_data(task_one_classic_data, task_two_classic_data):
# Plot data
x = [int(row[0]) for row in task_one_classic_data]
y_task_one = [float(row[1]) for row in task_one_classic_data]
y_task_two = [float(row[1]) for row in task_two_classic_data]
plt.plot(x, y_task_one,label="Task 1")
plt.plot(x, y_task_two, label="Task 2")
plt.suptitle(f"Comparison between task 1 and task 2 code (n_tests: {len(x)})")
plt.xlabel("Maze")
plt.ylabel("Time (s)")
plt.legend()
# Save plot
plt.savefig("../report/images/provided_tests_comparrison.png")
plt.clf()
def read_harder_data():
harder_data = []
with open("log_t2_harder.txt", "r") as f:
# Read csv file
data = f.read()
# Split data into lines
data = data.splitlines()
# Split lines into columns
data = [line.split(",") for line in data]
data = data[1:]
# Remove third column
for row in data:
harder_data.append(row[:2])
return harder_data
def plot_harder_data(harder_data):
# Plot data
x = [int(i) for i in range(50, 950, 50)]
y = [float(row[1]) for row in harder_data]
plt.plot(x, y)
plt.suptitle(f"How maze size affects time (n_tests: {len(x)})")
plt.xlabel("Maze size (N) -> (N x N)")
plt.ylabel("Time (s)")
# Save plot
plt.savefig("../report/images/custom_tests_comparrison.png")
plt.clf()
def read_treasure_data():
treasure_data = []
with open("log_t3_treasure_hunt.txt", "r") as f:
# Read csv file
data = f.read()
# Split data into lines
data = data.splitlines()
# Split lines into columns
data = [line.split(",") for line in data]
data = data[1:]
# Remove third column
for row in data:
treasure_data.append(row[:2])
return treasure_data
def plot_treasure_data(treasure_data):
# Plot data
x = [int(row[0]) for row in treasure_data]
y = [float(row[1]) for row in treasure_data]
plt.plot(x, y)
plt.suptitle(f"Treasure hunt for provided tests (n_tests: {len(x)})")
plt.xlabel("Maze")
plt.ylabel("Time (s)")
# Save plot
plt.savefig("../report/images/treasures_comparrison.png")
plt.clf()
def main():
task_one_classic_data, task_two_classic_data = read_classic_data()
plot_classic_data(task_one_classic_data, task_two_classic_data)
harder_data = read_harder_data()
plot_harder_data(harder_data)
treasure_data = read_treasure_data()
plot_treasure_data(treasure_data)
if __name__ == "__main__":
main()

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB