This commit is contained in:
Anna Śmigiel 2022-04-28 17:44:04 +02:00
parent fb6ebb1d7d
commit 2494d7ef66

72
astar.py Normal file
View File

@ -0,0 +1,72 @@
class State:
def __init__(self, direction, x, y):
self.direction = direction # kierunek w ktorym "patrzy wozek"
self.x = x
self.y = y
def get_direction(self):
return self.direction
def get_x(self):
return self.x
def get_y(self):
return self.y
def goal_test(self, goal): # sprawdza czy osiagnelismy cel
if self.x == goal[0] and self.y == goal[1]:
return True
else:
return False
class Node:
def __init__(self, action, state, parent):
self.state = state
self.action = action # akcja jaką ma wykonać (jedz prawo, lewo, przod)
self.direction = state.direction
self.parent = parent # ojciec wierzchołka
def get_action(self):
return self.action
def get_direction(self):
return self.direction
def get_parent(self):
return self.parent
def cost(node): # funkcja kosztu : ile kosztuje przejechanie przez dane pole
cost = 0
while node.parent is not None: # FIX!!!!!!!!!!
cost = cost + 1 + 1
node = node.parent
return cost
def f(goal, node): # funkcja zwracająca sumę funkcji kosztu oraz heurestyki
return cost(node) + heuristic(goal, node)
def heuristic(goal, node): # funkcja heurestyki : oszacowuje koszt osiągnięcia stanu końcowego (droga)
return abs(node.x - goal[0]) + abs(node.y - goal[1])
def print_moves(elem): # zwraca listę ruchów jakie należy wykonać by dotrzeć do punktu docelowego
moves_list = []
while elem.parent is not None:
moves_list.append(elem.action)
elem = elem.parent
moves_list.reverse()
return moves_list
def succ(elem): # funkcja następnika, przypisuje jakie akcje są możliwe do wykonania na danym polu oraz jaki będzie stan (kierunek, położenie) po wykonaniu tej akcji
pass
def graphsearch(explored, fringe, goaltest, istate): # przeszukiwanie grafu wszerz
pass