-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvis_tools.py
More file actions
127 lines (86 loc) · 5.08 KB
/
vis_tools.py
File metadata and controls
127 lines (86 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import matplotlib.pyplot as plt
import numpy as np
def harita_gorsellestir(grid_map, tum_konumlar, gbest_rota, iterasyon, algorithm):
plt.figure(figsize=(8,8))
plt.imshow(grid_map.HARITA, cmap='binary', origin='lower')
plt.title(f"PSO Pathfinding - İterasyon {iterasyon + 1}")
plt.xlabel("Y Koordinatı")
plt.ylabel("X Koordinatı")
plt.xticks(np.arange(grid_map.BOYUT))
plt.yticks(np.arange(grid_map.BOYUT))
plt.grid(color='gray', linestyle='-', linewidth=0.5)
for i in range(tum_konumlar.shape[0]):
# X[i] konumunu (x1, y1, x2, y2, ...) listesine dönüştür
rota = tum_konumlar[i].reshape(-1, 2)
tam_rota = [grid_map.START] + rota.tolist() + [grid_map.GOAL]
# Parçacık noktalarını ve rotayı çiz
# Her bir parçacık için rotayı çizmek karmaşık olabilir, sadece noktaları çizelim.
ara_noktalar = np.array(rota)
plt.scatter(ara_noktalar[:, 1], ara_noktalar[:, 0], s=5, color='gray', alpha=0.3)
# 3. Gbest Rotasını Çiz (En İyi Yol)
gbest_tam_rota = [grid_map.START] + gbest_rota + [grid_map.GOAL]
# X ve Y koordinatlarını ayır (Matplotlib için (Y, X) sırasını kullanıyoruz)
gbest_y_koor = [p[1] for p in gbest_tam_rota]
gbest_x_koor = [p[0] for p in gbest_tam_rota]
plt.plot(gbest_y_koor, gbest_x_koor, marker='o', color='red', linestyle='-', linewidth=2, label='Gbest Rota')
# Başlangıç ve Bitiş Noktaları
plt.scatter(grid_map.START[1], grid_map.START[0], s=200, marker='s', color='green', label='Başlangıç (0,0)')
plt.scatter(grid_map.GOAL[1], grid_map.GOAL[0], s=200, marker='*', color='blue', label='GOAL (9,9)')
plt.xlim(-0.5, grid_map.BOYUT - 0.5)
plt.ylim(-0.5, grid_map.BOYUT - 0.5)
plt.gca().invert_yaxis() # Haritayı standart (0,0) sol üst köşede olacak şekilde ayarla
plt.legend()
plt.savefig(f"/home/seda/Documents/QuantumInspiredMethods/outputs/output_{algorithm}.png", dpi=300, bbox_inches="tight")
# plt.show()
def harita_gorsellestir_aco(grid_map, en_iyi_rota, iterasyon):
"""ACO'dan elde edilen ayrık rotayı görselleştirir."""
plt.figure(figsize=(8,8))
# Haritayı çiz (Engeller: Siyah, Boş Alan: Beyaz)
plt.imshow(grid_map.HARITA, cmap='binary', origin='upper')
plt.title(f"ACO Pathfinding - İterasyon {iterasyon + 1}")
plt.xlabel("X Koordinatı")
plt.ylabel("Y Koordinatı")
plt.xticks(np.arange(grid_map.BOYUT))
plt.yticks(np.arange(grid_map.BOYUT))
plt.grid(color='gray', linestyle='-', linewidth=0.5)
if en_iyi_rota:
# 1. En İyi Rotayı Çiz
# Rota (Y, X) koordinatları listesi olarak gelir. Matplotlib için X, Y şeklinde ayırıyoruz.
# Not: Harita (Y, X) formatında olduğu için plot'ta (X, Y) kullanmalıyız.
# En iyi rota zaten (Başlangıç -> ... -> Hedef) formatındadır.
# X ve Y koordinatlarını ayır (Y_grid = row_index, X_grid = col_index)
rota_y = [p[0] for p in en_iyi_rota] # Y koordinatları (satır indeksi)
rota_x = [p[1] for p in en_iyi_rota] # X koordinatları (sütun indeksi)
plt.plot(rota_x, rota_y, marker='o', markersize=5, color='red', linestyle='-', linewidth=2, label='En İyi ACO Rota')
# Başlangıç ve Bitiş Noktaları
plt.scatter(grid_map.START[1], grid_map.START[0], s=200, marker='s', color='green', label='Başlangıç')
plt.scatter(grid_map.GOAL[1], grid_map.GOAL[0], s=200, marker='*', color='blue', label='Hedef')
plt.xlim(-0.5, grid_map.BOYUT - 0.5)
plt.ylim(-0.5, grid_map.BOYUT - 0.5)
plt.gca().invert_yaxis() # (0,0)'ın sol üstte olmasını sağlar
plt.legend()
plt.savefig(f"/home/seda/Documents/QuantumInspiredMethods/outputs/output_ACO.png", dpi=300, bbox_inches="tight")
# plt.show()
def harita_gorsellestir_qaco(grid_map, en_iyi_rota, iterasyon, max_iter):
"""QACO'dan elde edilen ayrık rotayı görselleştirir."""
plt.figure(figsize=(8,8))
plt.imshow(grid_map.HARITA, cmap='binary', origin='upper')
plt.title(f"QACO Pathfinding - İterasyon {iterasyon + 1}/{max_iter}")
plt.xlabel("X Koordinatı")
plt.ylabel("Y Koordinatı")
plt.xticks(np.arange(grid_map.BOYUT))
plt.yticks(np.arange(grid_map.BOYUT))
plt.grid(color='gray', linestyle='-', linewidth=0.5)
if en_iyi_rota:
rota_y = [p[0] for p in en_iyi_rota]
rota_x = [p[1] for p in en_iyi_rota]
plt.plot(rota_x, rota_y, marker='o', markersize=5, color='red', linestyle='-', linewidth=2, label='En İyi QACO Rota')
plt.scatter(grid_map.START[1], grid_map.START[0], s=200, marker='s', color='green', label='Başlangıç')
plt.scatter(grid_map.GOAL[1], grid_map.GOAL[0], s=200, marker='*', color='blue', label='Hedef')
plt.xlim(-0.5, grid_map.BOYUT - 0.5)
plt.ylim(-0.5, grid_map.BOYUT - 0.5)
plt.gca().invert_yaxis()
plt.legend()
# plt.show()
# plt.savefig("QACO.png", dpi=300)
plt.savefig(f"/home/seda/Documents/QuantumInspiredMethods/outputs/output_QACO.png", dpi=300, bbox_inches="tight")