Poczyszczenie kodu A_star i powiązanych z nim funkcji

This commit is contained in:
tonywesoly 2022-05-08 16:43:43 +02:00
parent 676b9c49c5
commit 85d981a177
3 changed files with 11 additions and 44 deletions

View File

@ -1,4 +1,3 @@
from ast import walk
import math
import pygame
@ -51,8 +50,8 @@ class Node:
if self.walkable:
return self.g_cost + self.h_cost
else:
return 0
# return math.inf
# return 0
return math.inf
class Pathfinding:
@ -108,8 +107,8 @@ class Pathfinding:
self.path = path
for neighbour in self.succ(current_node):
# if not neighbour.walkable or neighbour in explored:
if neighbour in explored:
if not neighbour.walkable or neighbour in explored:
# if neighbour in explored:
continue
new_movement_cost_to_neighbour = current_node.g_cost + self.get_distance(current_node,neighbour)
if new_movement_cost_to_neighbour < neighbour.g_cost or not neighbour in fringe:
@ -150,39 +149,3 @@ class Pathfinding:
pygame.draw.rect(window,
color,
block)
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

View File

@ -10,7 +10,7 @@ from Truck import Truck
from Global_variables import Global_variables as G_var
from pygame.constants import *
from astar import Pathfinding, State
from Astar import Pathfinding, State
class Environment:
@ -34,9 +34,13 @@ class Environment:
for field in row:
field.draw()
self.grid.draw_grid()
self.use_astar() # w przyszlosci trzeba przeniesc funkcje w jakies logiczniejsze miejsce np funkcje update()
self.astar.draw_path(self.window)
pygame.display.flip()
def update_all_elements(self,event):
self.use_astar()
self.update_truck(event)
def use_astar(self):
start_state = State(1,self.truck.x,self.truck.y)

View File

@ -17,5 +17,5 @@ class Program:
for event in pygame.event.get(): # integrating with keyboard
if event.type == QUIT:
running = False
self.environment.update_truck(event)
self.environment.update_all_elements(event)
self.environment.draw_all_elements()