Spaces:
Sleeping
Sleeping
Add application file
Browse files- A_Star.py +39 -0
- Bellman_Ford.py +44 -0
- Dijkstra.py +49 -0
- Dockerfile +21 -0
- app.py +284 -0
- docker-compose.yml +11 -0
- main.py +87 -0
- requirements.txt +4 -0
- run_local.sh +30 -0
A_Star.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from heapq import *
|
| 2 |
+
|
| 3 |
+
# A* algoritmasında kullanılacak olan heuristik fonksiyon
|
| 4 |
+
def heuristic(a, b):
|
| 5 |
+
return abs(a[0] - b[0]) + abs(a[1] - b[1]) # A ile B arasındaki Manhattan mesafesi
|
| 6 |
+
|
| 7 |
+
# Yolu geri döndürmek için kullanılan fonksiyon
|
| 8 |
+
def reconstruct_path(came_from, current):
|
| 9 |
+
total_path = [current]
|
| 10 |
+
while current in came_from and came_from[current] is not None:
|
| 11 |
+
current = came_from[current]
|
| 12 |
+
total_path.append(current) # Yolun parçalarını sırayla ekle
|
| 13 |
+
return total_path[::-1] # Yolu ters çevir, böylece başlangıç -> hedef sırası olur
|
| 14 |
+
|
| 15 |
+
def a_star(graph, start, goal):
|
| 16 |
+
queue = []
|
| 17 |
+
heappush(queue, (0, start)) # Başlangıç düğümünü maliyet 0 ile kuyruğa ekle
|
| 18 |
+
g_score = {start: 0} # Başlangıç düğümünün maliyeti 0
|
| 19 |
+
came_from = {start: None} # Her düğümün nereden geldiğini takip etmek için
|
| 20 |
+
|
| 21 |
+
while queue:
|
| 22 |
+
_, current = heappop(queue) # En düşük maliyetli düğümü al
|
| 23 |
+
|
| 24 |
+
if current == goal:
|
| 25 |
+
return reconstruct_path(came_from, goal) # Hedefe ulaşıldıysa yolu geri döndür
|
| 26 |
+
|
| 27 |
+
# Mevcut düğümün komşularını dolaş
|
| 28 |
+
for neighbor_cost, neighbor in graph[current]:
|
| 29 |
+
tentative_g_score = g_score[current] + neighbor_cost # Komşuya ulaşmanın maliyetini hesapla
|
| 30 |
+
|
| 31 |
+
# Daha düşük maliyet bulunursa güncelle
|
| 32 |
+
if neighbor not in g_score or tentative_g_score < g_score[neighbor]:
|
| 33 |
+
g_score[neighbor] = tentative_g_score # En iyi g_score güncelle
|
| 34 |
+
f_score = tentative_g_score + heuristic(neighbor, goal) # Toplam maliyeti hesapla (g + h)
|
| 35 |
+
heappush(queue, (f_score, neighbor)) # Komşuyu öncelik sırasına ekle
|
| 36 |
+
came_from[neighbor] = current # Bu komşuya, mevcut düğümden gelindi
|
| 37 |
+
|
| 38 |
+
return None # Eğer hedefe ulaşılamazsa None döndür
|
| 39 |
+
|
Bellman_Ford.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# kaynak: https://www.youtube.com/watch?v=24HziTZ8_xo
|
| 2 |
+
|
| 3 |
+
def bellman_ford(graph,başlangıç,hedef):
|
| 4 |
+
# Mesafeleri sonsuz olarak başlat
|
| 5 |
+
mesafeler = {node: float('infinity') for node in graph}
|
| 6 |
+
önceki = {node: None for node in graph} # Her düğümün öncesini kaydet
|
| 7 |
+
|
| 8 |
+
# Başlangıç düğümünün mesafesini sıfır olarak ayarla
|
| 9 |
+
mesafeler[başlangıç] = 0
|
| 10 |
+
|
| 11 |
+
# Döngü graftaki düğümlerin sayısının 1 eksiği kadar devam etmeli.
|
| 12 |
+
for _ in range(len(graph) - 1): # Düğüm sayısı - 1 kadar tekrar et
|
| 13 |
+
for i in graph: # Her düğüm üzerinde dolaş
|
| 14 |
+
for j in graph[i]: # Düğümün komşularına bak
|
| 15 |
+
hedef_dugum = j[1] # Hedef düğümü al
|
| 16 |
+
maliyet = j[0] # Kenar maliyetini al
|
| 17 |
+
|
| 18 |
+
# Eğer mevcut düğümün mesafesi sonsuz değilse ve komşuya olan mesafe daha düşükse güncelle
|
| 19 |
+
if mesafeler[i] != float('infinity') and (mesafeler[i] + maliyet < mesafeler[hedef_dugum]):
|
| 20 |
+
mesafeler[hedef_dugum] = mesafeler[i] + maliyet
|
| 21 |
+
önceki[hedef_dugum] = i # Önceki düğümü güncelle
|
| 22 |
+
|
| 23 |
+
# Negatif ağırlıklı döngü kontrolü
|
| 24 |
+
for i in graph:
|
| 25 |
+
for j in graph[i]:
|
| 26 |
+
hedef_dugum = j[1]
|
| 27 |
+
maliyet = j[0]
|
| 28 |
+
|
| 29 |
+
# Eğer hala bir iyileştirme yapılabiliyorsa negatif döngü vardır
|
| 30 |
+
if mesafeler[i] != float('infinity') and (mesafeler[i] + maliyet < mesafeler[hedef_dugum]):
|
| 31 |
+
return None # Negatif döngü bulunduktan sonra işlevi sonlandırın.
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# En kısa yolu bulmak için hedeften başlangıca doğru git
|
| 35 |
+
yol = []
|
| 36 |
+
geçerli_dugum = hedef
|
| 37 |
+
|
| 38 |
+
while geçerli_dugum is not None:
|
| 39 |
+
yol.append(geçerli_dugum)
|
| 40 |
+
geçerli_dugum = önceki[geçerli_dugum]
|
| 41 |
+
|
| 42 |
+
# Yolu ters çevir ve döndür.
|
| 43 |
+
yol.reverse()
|
| 44 |
+
return yol
|
Dijkstra.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def get_shortest_path(previous_nodes, start, target):
|
| 2 |
+
path = []
|
| 3 |
+
current_node = target
|
| 4 |
+
|
| 5 |
+
while current_node is not None:
|
| 6 |
+
path.insert(0, current_node) # Düğümü başa ekle
|
| 7 |
+
current_node = previous_nodes[current_node] # Bir önceki düğüme git
|
| 8 |
+
|
| 9 |
+
if path[0] == start:
|
| 10 |
+
return path
|
| 11 |
+
else:
|
| 12 |
+
return [] # Eğer yol yoksa boş liste döner
|
| 13 |
+
|
| 14 |
+
def dijkstra(graph, start,target):
|
| 15 |
+
# 1. Mesafe tablosu: Başlangıç düğümünden diğer düğümlere olan mesafeleri tutar
|
| 16 |
+
mesafeler = {node: float('infinity') for node in graph}
|
| 17 |
+
mesafeler[start] = 0
|
| 18 |
+
|
| 19 |
+
# 2. Ziyaret edilen düğümleri tutan liste
|
| 20 |
+
ziyaret_edilen = []
|
| 21 |
+
|
| 22 |
+
# 3. Önceki düğümleri tutan tablo: En kısa yolu oluşturmak için
|
| 23 |
+
önceki_düğümler = {node: None for node in graph}
|
| 24 |
+
|
| 25 |
+
while len(ziyaret_edilen) < len(graph):
|
| 26 |
+
# 4. Ziyaret edilmemiş düğümler arasında en kısa mesafeye sahip olanı seç
|
| 27 |
+
en_kısa_yol_düğümü = None
|
| 28 |
+
for node in mesafeler:
|
| 29 |
+
if node not in ziyaret_edilen:
|
| 30 |
+
if en_kısa_yol_düğümü is None:
|
| 31 |
+
en_kısa_yol_düğümü = node
|
| 32 |
+
elif mesafeler[node] < mesafeler[en_kısa_yol_düğümü]:
|
| 33 |
+
en_kısa_yol_düğümü = node
|
| 34 |
+
|
| 35 |
+
if en_kısa_yol_düğümü is None:
|
| 36 |
+
break # Tüm düğümler ziyaret edilmişse ya da ulaşılamayan düğümler kalmışsa döngüyü sonlandır
|
| 37 |
+
|
| 38 |
+
# 5. Seçilen düğümün komşularının mesafelerini güncelle
|
| 39 |
+
for ağırlık,komşu in graph[en_kısa_yol_düğümü]:
|
| 40 |
+
|
| 41 |
+
yeni_mesafe = mesafeler[en_kısa_yol_düğümü] + ağırlık
|
| 42 |
+
if yeni_mesafe < mesafeler[komşu]:
|
| 43 |
+
mesafeler[komşu] = yeni_mesafe
|
| 44 |
+
önceki_düğümler[komşu] = en_kısa_yol_düğümü
|
| 45 |
+
|
| 46 |
+
# 6. Seçilen düğümü ziyaret edilmiş olarak işaretle
|
| 47 |
+
ziyaret_edilen.append(en_kısa_yol_düğümü)
|
| 48 |
+
|
| 49 |
+
return get_shortest_path(önceki_düğümler, start, target)
|
Dockerfile
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
# Sistem bağımlılıklarını kur
|
| 6 |
+
RUN apt-get update && apt-get install -y \
|
| 7 |
+
build-essential \
|
| 8 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 9 |
+
|
| 10 |
+
# Python paketlerini kur
|
| 11 |
+
COPY requirements.txt .
|
| 12 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 13 |
+
|
| 14 |
+
# Uygulama dosyalarını kopyala
|
| 15 |
+
COPY app.py .
|
| 16 |
+
|
| 17 |
+
# Port
|
| 18 |
+
EXPOSE 7860
|
| 19 |
+
|
| 20 |
+
# Uygulamayı başlat
|
| 21 |
+
CMD ["python", "app.py"]
|
app.py
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
from matplotlib.patches import Rectangle
|
| 5 |
+
from heapq import heappush, heappop
|
| 6 |
+
import io
|
| 7 |
+
from PIL import Image
|
| 8 |
+
|
| 9 |
+
# Grid tanımı
|
| 10 |
+
GRID = [[4, 4, 4, 4, 4, 4, 4, 2, 3, 2, 4, 2],
|
| 11 |
+
[4, 4, 4, 4, 9, 9, 3, 2, 2, 4, 4, 4],
|
| 12 |
+
[4, 4, 2, 4, 2, 2, 2, 1, 1, 9, 9, 4],
|
| 13 |
+
[4, 2, 2, 4, 2, 2, 1, 1, 1, 4, 4, 2],
|
| 14 |
+
[1, 1, 2, 1, 1, 1, 1, 9, 1, 1, 1, 1],
|
| 15 |
+
[4, 1, 2, 1, 9, 2, 1, 1, 1, 9, 2, 2],
|
| 16 |
+
[4, 1, 4, 1, 2, 4, 4, 1, 1, 4, 4, 2],
|
| 17 |
+
[1, 1, 1, 1, 2, 4, 2, 2, 1, 2, 3, 2]]
|
| 18 |
+
|
| 19 |
+
COLS, ROWS = 12, 8
|
| 20 |
+
|
| 21 |
+
# Graph oluştur
|
| 22 |
+
def create_graph(grid):
|
| 23 |
+
graph = {}
|
| 24 |
+
for y in range(len(grid)):
|
| 25 |
+
for x in range(len(grid[0])):
|
| 26 |
+
neighbors = []
|
| 27 |
+
for dx, dy in [(-1, 0), (0, -1), (1, 0), (0, 1)]:
|
| 28 |
+
nx, ny = x + dx, y + dy
|
| 29 |
+
if 0 <= nx < len(grid[0]) and 0 <= ny < len(grid):
|
| 30 |
+
neighbors.append((grid[ny][nx], (nx, ny)))
|
| 31 |
+
graph[(x, y)] = neighbors
|
| 32 |
+
return graph
|
| 33 |
+
|
| 34 |
+
GRAPH = create_graph(GRID)
|
| 35 |
+
|
| 36 |
+
# A* Algoritması
|
| 37 |
+
def heuristic(a, b):
|
| 38 |
+
return abs(a[0] - b[0]) + abs(a[1] - b[1])
|
| 39 |
+
|
| 40 |
+
def a_star(graph, start, goal):
|
| 41 |
+
queue = []
|
| 42 |
+
heappush(queue, (0, start))
|
| 43 |
+
g_score = {start: 0}
|
| 44 |
+
came_from = {start: None}
|
| 45 |
+
|
| 46 |
+
while queue:
|
| 47 |
+
_, current = heappop(queue)
|
| 48 |
+
|
| 49 |
+
if current == goal:
|
| 50 |
+
path = []
|
| 51 |
+
while current:
|
| 52 |
+
path.append(current)
|
| 53 |
+
current = came_from[current]
|
| 54 |
+
return path[::-1]
|
| 55 |
+
|
| 56 |
+
for neighbor_cost, neighbor in graph[current]:
|
| 57 |
+
tentative_g_score = g_score[current] + neighbor_cost
|
| 58 |
+
|
| 59 |
+
if neighbor not in g_score or tentative_g_score < g_score[neighbor]:
|
| 60 |
+
g_score[neighbor] = tentative_g_score
|
| 61 |
+
f_score = tentative_g_score + heuristic(neighbor, goal)
|
| 62 |
+
heappush(queue, (f_score, neighbor))
|
| 63 |
+
came_from[neighbor] = current
|
| 64 |
+
|
| 65 |
+
return None
|
| 66 |
+
|
| 67 |
+
# Dijkstra Algoritması
|
| 68 |
+
def dijkstra(graph, start, goal):
|
| 69 |
+
distances = {node: float('infinity') for node in graph}
|
| 70 |
+
distances[start] = 0
|
| 71 |
+
visited = []
|
| 72 |
+
previous = {node: None for node in graph}
|
| 73 |
+
|
| 74 |
+
while len(visited) < len(graph):
|
| 75 |
+
current = None
|
| 76 |
+
for node in distances:
|
| 77 |
+
if node not in visited:
|
| 78 |
+
if current is None or distances[node] < distances[current]:
|
| 79 |
+
current = node
|
| 80 |
+
|
| 81 |
+
if current is None:
|
| 82 |
+
break
|
| 83 |
+
|
| 84 |
+
for weight, neighbor in graph[current]:
|
| 85 |
+
new_distance = distances[current] + weight
|
| 86 |
+
if new_distance < distances[neighbor]:
|
| 87 |
+
distances[neighbor] = new_distance
|
| 88 |
+
previous[neighbor] = current
|
| 89 |
+
|
| 90 |
+
visited.append(current)
|
| 91 |
+
|
| 92 |
+
path = []
|
| 93 |
+
current = goal
|
| 94 |
+
while current is not None:
|
| 95 |
+
path.insert(0, current)
|
| 96 |
+
current = previous[current]
|
| 97 |
+
|
| 98 |
+
return path if path[0] == start else None
|
| 99 |
+
|
| 100 |
+
# Bellman-Ford Algoritması
|
| 101 |
+
def bellman_ford(graph, start, goal):
|
| 102 |
+
distances = {node: float('infinity') for node in graph}
|
| 103 |
+
previous = {node: None for node in graph}
|
| 104 |
+
distances[start] = 0
|
| 105 |
+
|
| 106 |
+
for _ in range(len(graph) - 1):
|
| 107 |
+
for node in graph:
|
| 108 |
+
for cost, neighbor in graph[node]:
|
| 109 |
+
if distances[node] != float('infinity') and distances[node] + cost < distances[neighbor]:
|
| 110 |
+
distances[neighbor] = distances[node] + cost
|
| 111 |
+
previous[neighbor] = node
|
| 112 |
+
|
| 113 |
+
path = []
|
| 114 |
+
current = goal
|
| 115 |
+
while current is not None:
|
| 116 |
+
path.append(current)
|
| 117 |
+
current = previous[current]
|
| 118 |
+
|
| 119 |
+
path.reverse()
|
| 120 |
+
return path if path and path[0] == start else None
|
| 121 |
+
|
| 122 |
+
# Görselleştirme
|
| 123 |
+
def visualize_path(start_x, start_y, goal_x, goal_y, algorithm):
|
| 124 |
+
start = (start_x, start_y)
|
| 125 |
+
goal = (goal_x, goal_y)
|
| 126 |
+
|
| 127 |
+
# Algoritma seçimi
|
| 128 |
+
if algorithm == "A*":
|
| 129 |
+
path = a_star(GRAPH, start, goal)
|
| 130 |
+
color = '#2196F3'
|
| 131 |
+
title = "A* Algorithm"
|
| 132 |
+
elif algorithm == "Dijkstra":
|
| 133 |
+
path = dijkstra(GRAPH, start, goal)
|
| 134 |
+
color = '#FF9800'
|
| 135 |
+
title = "Dijkstra Algorithm"
|
| 136 |
+
else: # Bellman-Ford
|
| 137 |
+
path = bellman_ford(GRAPH, start, goal)
|
| 138 |
+
color = '#F44336'
|
| 139 |
+
title = "Bellman-Ford Algorithm"
|
| 140 |
+
|
| 141 |
+
# Matplotlib ile görselleştirme
|
| 142 |
+
fig, ax = plt.subplots(figsize=(14, 8))
|
| 143 |
+
|
| 144 |
+
# Grid çiz
|
| 145 |
+
for y in range(ROWS):
|
| 146 |
+
for x in range(COLS):
|
| 147 |
+
cost = GRID[y][x]
|
| 148 |
+
# Maliyet değerine göre renk
|
| 149 |
+
if cost == 1:
|
| 150 |
+
cell_color = '#E8F5E9'
|
| 151 |
+
elif cost <= 3:
|
| 152 |
+
cell_color = '#FFF9C4'
|
| 153 |
+
elif cost <= 6:
|
| 154 |
+
cell_color = '#FFE0B2'
|
| 155 |
+
else:
|
| 156 |
+
cell_color = '#FFCDD2'
|
| 157 |
+
|
| 158 |
+
rect = Rectangle((x, ROWS - y - 1), 1, 1,
|
| 159 |
+
facecolor=cell_color,
|
| 160 |
+
edgecolor='gray',
|
| 161 |
+
linewidth=0.5)
|
| 162 |
+
ax.add_patch(rect)
|
| 163 |
+
|
| 164 |
+
# Maliyet değerini yaz
|
| 165 |
+
ax.text(x + 0.5, ROWS - y - 0.5, str(cost),
|
| 166 |
+
ha='center', va='center',
|
| 167 |
+
fontsize=10, color='#424242', weight='bold')
|
| 168 |
+
|
| 169 |
+
# Yolu çiz
|
| 170 |
+
if path:
|
| 171 |
+
for i, (x, y) in enumerate(path):
|
| 172 |
+
if (x, y) == start:
|
| 173 |
+
circle_color = '#4CAF50'
|
| 174 |
+
label = 'Start'
|
| 175 |
+
elif (x, y) == goal:
|
| 176 |
+
circle_color = '#F44336'
|
| 177 |
+
label = 'Goal'
|
| 178 |
+
else:
|
| 179 |
+
circle_color = color
|
| 180 |
+
label = 'Path'
|
| 181 |
+
|
| 182 |
+
circle = plt.Circle((x + 0.5, ROWS - y - 0.5), 0.3,
|
| 183 |
+
color=circle_color, zorder=10)
|
| 184 |
+
ax.add_patch(circle)
|
| 185 |
+
|
| 186 |
+
path_length = sum(GRID[y][x] for x, y in path[1:])
|
| 187 |
+
info_text = f"Path Length: {len(path)} nodes\nTotal Cost: {path_length}"
|
| 188 |
+
else:
|
| 189 |
+
info_text = "No path found!"
|
| 190 |
+
|
| 191 |
+
ax.set_xlim(0, COLS)
|
| 192 |
+
ax.set_ylim(0, ROWS)
|
| 193 |
+
ax.set_aspect('equal')
|
| 194 |
+
ax.axis('off')
|
| 195 |
+
ax.set_title(f'{title}\n{info_text}', fontsize=16, weight='bold', pad=20)
|
| 196 |
+
|
| 197 |
+
# Legend
|
| 198 |
+
from matplotlib.patches import Patch
|
| 199 |
+
legend_elements = [
|
| 200 |
+
Patch(facecolor='#4CAF50', label='Start'),
|
| 201 |
+
Patch(facecolor='#F44336', label='Goal'),
|
| 202 |
+
Patch(facecolor=color, label='Path'),
|
| 203 |
+
Patch(facecolor='#E8F5E9', label='Low Cost (1)'),
|
| 204 |
+
Patch(facecolor='#FFF9C4', label='Medium Cost (2-3)'),
|
| 205 |
+
Patch(facecolor='#FFE0B2', label='High Cost (4-6)'),
|
| 206 |
+
Patch(facecolor='#FFCDD2', label='Very High Cost (7-9)')
|
| 207 |
+
]
|
| 208 |
+
ax.legend(handles=legend_elements, loc='upper left', bbox_to_anchor=(1, 1))
|
| 209 |
+
|
| 210 |
+
plt.tight_layout()
|
| 211 |
+
|
| 212 |
+
# PIL Image'e çevir
|
| 213 |
+
buf = io.BytesIO()
|
| 214 |
+
plt.savefig(buf, format='png', dpi=100, bbox_inches='tight')
|
| 215 |
+
buf.seek(0)
|
| 216 |
+
img = Image.open(buf)
|
| 217 |
+
plt.close()
|
| 218 |
+
|
| 219 |
+
return img
|
| 220 |
+
|
| 221 |
+
# Gradio Arayüzü
|
| 222 |
+
with gr.Blocks(title="Path Finding Algorithms", theme=gr.themes.Soft()) as demo:
|
| 223 |
+
gr.Markdown("""
|
| 224 |
+
# 🗺️ Path Finding Algorithms Visualizer
|
| 225 |
+
|
| 226 |
+
Select start and goal positions, choose an algorithm, and visualize the shortest path!
|
| 227 |
+
|
| 228 |
+
### 📊 Algorithms:
|
| 229 |
+
- **A\***: Heuristic-based, fastest with known goal
|
| 230 |
+
- **Dijkstra**: Classic shortest path algorithm
|
| 231 |
+
- **Bellman-Ford**: Handles negative weights, detects negative cycles
|
| 232 |
+
""")
|
| 233 |
+
|
| 234 |
+
with gr.Row():
|
| 235 |
+
with gr.Column(scale=1):
|
| 236 |
+
gr.Markdown("### ⚙️ Settings")
|
| 237 |
+
|
| 238 |
+
algorithm = gr.Radio(
|
| 239 |
+
choices=["A*", "Dijkstra", "Bellman-Ford"],
|
| 240 |
+
value="A*",
|
| 241 |
+
label="Algorithm"
|
| 242 |
+
)
|
| 243 |
+
|
| 244 |
+
with gr.Row():
|
| 245 |
+
start_x = gr.Slider(0, 11, value=0, step=1, label="Start X")
|
| 246 |
+
start_y = gr.Slider(0, 7, value=7, step=1, label="Start Y")
|
| 247 |
+
|
| 248 |
+
with gr.Row():
|
| 249 |
+
goal_x = gr.Slider(0, 11, value=11, step=1, label="Goal X")
|
| 250 |
+
goal_y = gr.Slider(0, 7, value=0, step=1, label="Goal Y")
|
| 251 |
+
|
| 252 |
+
find_btn = gr.Button("🔍 Find Path", variant="primary", size="lg")
|
| 253 |
+
|
| 254 |
+
gr.Markdown("""
|
| 255 |
+
### 📝 Info:
|
| 256 |
+
- **Grid Size**: 12x8
|
| 257 |
+
- **Cost Range**: 1-9
|
| 258 |
+
- **Green**: Start point
|
| 259 |
+
- **Red**: Goal point
|
| 260 |
+
- **Blue/Orange/Red**: Path (depends on algorithm)
|
| 261 |
+
""")
|
| 262 |
+
|
| 263 |
+
with gr.Column(scale=2):
|
| 264 |
+
output_image = gr.Image(label="Visualization", type="pil")
|
| 265 |
+
|
| 266 |
+
find_btn.click(
|
| 267 |
+
fn=visualize_path,
|
| 268 |
+
inputs=[start_x, start_y, goal_x, goal_y, algorithm],
|
| 269 |
+
outputs=output_image
|
| 270 |
+
)
|
| 271 |
+
|
| 272 |
+
# İlk yükleme
|
| 273 |
+
demo.load(
|
| 274 |
+
fn=visualize_path,
|
| 275 |
+
inputs=[gr.Number(value=0, visible=False),
|
| 276 |
+
gr.Number(value=7, visible=False),
|
| 277 |
+
gr.Number(value=11, visible=False),
|
| 278 |
+
gr.Number(value=0, visible=False),
|
| 279 |
+
gr.Textbox(value="A*", visible=False)],
|
| 280 |
+
outputs=output_image
|
| 281 |
+
)
|
| 282 |
+
|
| 283 |
+
if __name__ == "__main__":
|
| 284 |
+
demo.launch()
|
docker-compose.yml
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version: '3.8'
|
| 2 |
+
|
| 3 |
+
services:
|
| 4 |
+
pathfinding:
|
| 5 |
+
build: .
|
| 6 |
+
ports:
|
| 7 |
+
- "7860:7860"
|
| 8 |
+
environment:
|
| 9 |
+
- GRADIO_SERVER_NAME=0.0.0.0
|
| 10 |
+
- GRADIO_SERVER_PORT=7860
|
| 11 |
+
restart: unless-stopped
|
main.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pygame as pg
|
| 2 |
+
from heapq import *
|
| 3 |
+
from A_Star import a_star
|
| 4 |
+
from Dijkstra import dijkstra
|
| 5 |
+
from Bellman_Ford import bellman_ford
|
| 6 |
+
|
| 7 |
+
# Konumun merkezine yerleştirilen çemberin pozisyonunu döndürüyor
|
| 8 |
+
def get_circle(x, y):
|
| 9 |
+
return (x * TILE + TILE // 2, y * TILE + TILE // 2), TILE // 7 # Çemberin merkezini ve yarıçapını döndür
|
| 10 |
+
|
| 11 |
+
# Komşu hücreleri kontrol ediyor
|
| 12 |
+
def neighbor_control(x, y):
|
| 13 |
+
# Komşuların hangi yönlerde olduğunu belirleyen liste (sol, yukarı, sağ, aşağı)
|
| 14 |
+
ways = [-1, 0], [0, -1], [1, 0], [0, 1]
|
| 15 |
+
neighbor = []
|
| 16 |
+
|
| 17 |
+
for dx, dy in ways:
|
| 18 |
+
new_dx, new_dy = x + dx, y + dy
|
| 19 |
+
# Komşu hücrelerin oyun alanı sınırları içinde olup olmadığını kontrol et
|
| 20 |
+
if cols > new_dx >= 0 and rows > new_dy >= 0:
|
| 21 |
+
neighbor.append((grid[new_dy][new_dx], (new_dx, new_dy))) # Geçerli komşuyu listeye ekle
|
| 22 |
+
return neighbor
|
| 23 |
+
|
| 24 |
+
# Fare ile tıklanan hücreyi seçiyor.
|
| 25 |
+
def get_click_mouse_pos():
|
| 26 |
+
x, y = pg.mouse.get_pos() # Fare imlecinin konumunu al
|
| 27 |
+
grid_x, grid_y = x // TILE, y // TILE # Imlecin bulunduğu grid hücresini bul
|
| 28 |
+
pg.draw.circle(sc, pg.Color('black'), * ((grid_x * TILE + TILE // 2, grid_y * TILE + TILE // 2), TILE // 7)) # Hücrede kırmızı çember çiz
|
| 29 |
+
click = pg.mouse.get_pressed() # Fare tıklaması kontrolü
|
| 30 |
+
return (grid_x, grid_y) if click[0] else False # Sol tıklama yapılmışsa pozisyonu döndür, aksi halde False döndür
|
| 31 |
+
|
| 32 |
+
# Oyun alanının boyutları (kolonlar, satırlar ve hücre boyutu)
|
| 33 |
+
cols, rows = 12, 8
|
| 34 |
+
TILE = 70 # Her bir hücrenin piksel boyutu
|
| 35 |
+
|
| 36 |
+
# Pygame başlatma ve ekran ayarlama
|
| 37 |
+
pg.init()
|
| 38 |
+
sc = pg.display.set_mode([cols * TILE, rows * TILE])
|
| 39 |
+
clock = pg.time.Clock()
|
| 40 |
+
|
| 41 |
+
# Oyun alanı için grid oluşturma (her hücrede maliyetler)
|
| 42 |
+
grid = [[4, 4, 4, 4, 4, 4, 4, 2, 3, 2, 4, 2],
|
| 43 |
+
[4, 4, 4, 4, 9, 9, 3, 2, 2, 4, 4, 4],
|
| 44 |
+
[4, 4, 2, 4, 2, 2, 2, 1, 1, 9, 9, 4],
|
| 45 |
+
[4, 2, 2, 4, 2, 2, 1, 1, 1, 4, 4, 2],
|
| 46 |
+
[1, 1, 2, 1, 1, 1, 1, 9, 1, 1, 1, 1],
|
| 47 |
+
[4, 1, 2, 1, 9, 2, 1, 1, 1, 9, 2, 2],
|
| 48 |
+
[4, 1, 4, 1, 2, 4, 4, 1, 1, 4, 4, 2],
|
| 49 |
+
[1, 1, 1, 1, 2, 4, 2, 2, 1, 2, 3, 2]]
|
| 50 |
+
|
| 51 |
+
# Oyun alanı için graph oluşturma (komşuluk ilişkileri ile)
|
| 52 |
+
graph = {}
|
| 53 |
+
for y, row in enumerate(grid):
|
| 54 |
+
for x, col in enumerate(row):
|
| 55 |
+
graph[(x, y)] = graph.get((x, y), []) + neighbor_control(x, y)
|
| 56 |
+
|
| 57 |
+
# Başlangıç ve hedef konum
|
| 58 |
+
start = (0, 7)
|
| 59 |
+
goal = start
|
| 60 |
+
visited = []
|
| 61 |
+
|
| 62 |
+
# Arka plan resmi yükleme ve ölçeklendirme
|
| 63 |
+
bg = pg.image.load('./resim/map.jpg').convert()
|
| 64 |
+
bg = pg.transform.scale(bg, (cols * TILE, rows * TILE))
|
| 65 |
+
|
| 66 |
+
# Oyun döngüsü
|
| 67 |
+
while True:
|
| 68 |
+
|
| 69 |
+
sc.blit(bg, (0, 0))
|
| 70 |
+
mouse_pos = get_click_mouse_pos()
|
| 71 |
+
if mouse_pos:
|
| 72 |
+
#visited = a_star(graph, start, mouse_pos) # A* algoritmasını çalıştır ve yolu bul
|
| 73 |
+
#visited = dijkstra(graph, start, mouse_pos) # dijkstra algoritmasını çalıştır ve yolu bul
|
| 74 |
+
visited = bellman_ford(graph, start, mouse_pos) # bellman_ford algoritmasını çalıştır ve yolu bul
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
goal = mouse_pos # Hedefi fare ile tıklanan pozisyona ayarla
|
| 78 |
+
|
| 79 |
+
if visited:
|
| 80 |
+
for path_segment in visited:
|
| 81 |
+
pg.draw.circle(sc, pg.Color('blue'), *get_circle(*path_segment)) # Bulunan yol üzerindeki hücreleri maviyle işaretle
|
| 82 |
+
pg.draw.circle(sc, pg.Color('green'), *get_circle(*start)) # Başlangıç hücresini yeşille işaretle
|
| 83 |
+
pg.draw.circle(sc, pg.Color('red'), *get_circle(*goal)) # Hedef hücresini kırmızı işaretle
|
| 84 |
+
|
| 85 |
+
[exit() for event in pg.event.get() if event.type == pg.QUIT]
|
| 86 |
+
pg.display.flip()
|
| 87 |
+
clock.tick(30)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==4.16.0
|
| 2 |
+
numpy==1.24.3
|
| 3 |
+
matplotlib==3.7.1
|
| 4 |
+
Pillow==10.2.0
|
run_local.sh
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
|
| 3 |
+
echo "🚀 Starting Path Finding Algorithms Visualizer..."
|
| 4 |
+
echo ""
|
| 5 |
+
|
| 6 |
+
# Check if Docker is installed
|
| 7 |
+
if ! command -v docker &> /dev/null; then
|
| 8 |
+
echo "❌ Docker is not installed. Please install Docker first."
|
| 9 |
+
echo "Visit: https://docs.docker.com/get-docker/"
|
| 10 |
+
exit 1
|
| 11 |
+
fi
|
| 12 |
+
|
| 13 |
+
# Build Docker image
|
| 14 |
+
echo "📦 Building Docker image..."
|
| 15 |
+
docker build -t pathfinding-app .
|
| 16 |
+
|
| 17 |
+
if [ $? -ne 0 ]; then
|
| 18 |
+
echo "❌ Docker build failed!"
|
| 19 |
+
exit 1
|
| 20 |
+
fi
|
| 21 |
+
|
| 22 |
+
echo "✅ Docker image built successfully!"
|
| 23 |
+
echo ""
|
| 24 |
+
|
| 25 |
+
# Run container
|
| 26 |
+
echo "🏃 Running container..."
|
| 27 |
+
docker run -p 7860:7860 pathfinding-app
|
| 28 |
+
|
| 29 |
+
# Or use docker-compose
|
| 30 |
+
# docker-compose up
|