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 math
import pygame import pygame
@ -51,8 +50,8 @@ class Node:
if self.walkable: if self.walkable:
return self.g_cost + self.h_cost return self.g_cost + self.h_cost
else: else:
return 0 # return 0
# return math.inf return math.inf
class Pathfinding: class Pathfinding:
@ -108,8 +107,8 @@ class Pathfinding:
self.path = path self.path = path
for neighbour in self.succ(current_node): for neighbour in self.succ(current_node):
# if not neighbour.walkable or neighbour in explored: if not neighbour.walkable or neighbour in explored:
if neighbour in explored: # if neighbour in explored:
continue continue
new_movement_cost_to_neighbour = current_node.g_cost + self.get_distance(current_node,neighbour) 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: if new_movement_cost_to_neighbour < neighbour.g_cost or not neighbour in fringe:
@ -150,39 +149,3 @@ class Pathfinding:
pygame.draw.rect(window, pygame.draw.rect(window,
color, color,
block) 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 Global_variables import Global_variables as G_var
from pygame.constants import * from pygame.constants import *
from astar import Pathfinding, State from Astar import Pathfinding, State
class Environment: class Environment:
@ -34,10 +34,14 @@ class Environment:
for field in row: for field in row:
field.draw() field.draw()
self.grid.draw_grid() 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) self.astar.draw_path(self.window)
pygame.display.flip() pygame.display.flip()
def update_all_elements(self,event):
self.use_astar()
self.update_truck(event)
def use_astar(self): def use_astar(self):
start_state = State(1,self.truck.x,self.truck.y) start_state = State(1,self.truck.x,self.truck.y)
package = self.find_packate() package = self.find_packate()

View File

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