-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask1.py
141 lines (110 loc) · 3.74 KB
/
task1.py
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from queue import PriorityQueue
import matplotlib.pyplot as plt
import numpy as np
from datetime import datetime
import tkinter as tk
def a_star(grid, start, goal):
gScore = {start: 0}
fScore = {start: heuristic(start, goal)}
closedSet = set()
openSet = PriorityQueue()
openSet.put((fScore[start], start))
cameFrom = {}
while not openSet.empty():
current = openSet.get()[1]
if current == goal:
return cameFrom, gScore[current]
closedSet.add(current)
for neighbor in get_neighbors(grid, current):
if neighbor in closedSet:
continue
tentative_gScore = gScore[current] + dist_between(current, neighbor)
if neighbor not in [i[1] for i in openSet.queue]:
openSet.put((fScore.get(neighbor, float('inf')), neighbor))
elif tentative_gScore >= gScore.get(neighbor, float('inf')):
continue
cameFrom[neighbor] = current
gScore[neighbor] = tentative_gScore
fScore[neighbor] = gScore[neighbor] + heuristic(neighbor, goal)
def heuristic(a,b):
(x1,y1) = a
(x2,y2) = b
return abs(x1-x2) + abs(y1-y2)
def dist_between(a,b):
return 1
def get_neighbors(grid,current):
neighbors=[]
x,y=current
if x > 0 and grid[x-1][y]!='3':
neighbors.append((x-1,y))
if y > 0 and grid[x][y-1]!='3':
neighbors.append((x,y-1))
if x < len(grid)-1 and grid[x+1][y]!='3':
neighbors.append((x+1,y))
if y < len(grid[0])-1 and grid[x][y+1]!='3':
neighbors.append((x,y+1))
return neighbors
def reconstruct_path(cameFrom,current,start):
path=[current]
while current!=start:
current=cameFrom[current]
path.insert(0,current)
return path
mode = '1'
grid=[]
with open('grid1.txt','r') as file:
for line in file:
grid.append(list(line.strip()))
def degistir():
global grid
global mode
if mode == '1':
grid = []
with open('grid2.txt','r') as file:
for line in file:
grid.append(list(line.strip()))
mode = '2'
else:
grid = []
with open('grid1.txt','r') as file:
for line in file:
grid.append(list(line.strip()))
mode = '1'
def run():
global grid
grid=np.array(grid)
start=(0,np.argmin(grid[0]=='3'))
goal = (len(grid) - 1, len(grid[0]) - 1)
a = datetime.now()
cameFrom,cost=a_star(grid,start=start,goal=goal)
b = datetime.now()
path=reconstruct_path(cameFrom=cameFrom,current=goal,start=start)
for i in range(len(path)):
x,y=path[i]
grid[x][y]='X'
print(f'Cost: {cost}')
print(f'Path: {path}')
print('Şu kadar zamanda bulundu: ',(b - a).microseconds / 1000,'ms')
if mode == '1':
plt.xticks(np.arange(0, 21, 1))
plt.yticks(np.arange(0, 21, 1))
plt.xlabel('')
plt.ylabel('')
elif mode == '2':
plt.xticks(np.arange(0, 11, 1))
plt.yticks(np.arange(0, 11, 1))
plt.xlabel('')
plt.ylabel('')
plt.imshow(np.where(grid=='X',10,np.where(grid=='3',5,np.where(grid=='2',4,np.where(grid=='0',2,np.where(grid==' ',np.nan,np.nan))))))
plt.show()
if __name__ == '__main__':
root = tk.Tk()
ana_cerceve = tk.Frame(root)
ana_cerceve.pack()
axe_butonu = tk.Button(ana_cerceve, text="Url Değiştir", command=degistir)
axe_butonu.pack(side="left")
calistir_butonu = tk.Button(ana_cerceve, text="Çalıştır", command=run)
calistir_butonu.pack(side="left")
root.title("App")
root.geometry("100x100")
root.mainloop()