2024-04-27 00:34:03 +02:00
|
|
|
import pygame
|
|
|
|
from board import Board
|
|
|
|
from constant import width, height, rows, cols
|
|
|
|
from tractor import Tractor
|
|
|
|
import heapq
|
|
|
|
|
|
|
|
fps = 2
|
|
|
|
WIN = pygame.display.set_mode((width, height))
|
|
|
|
pygame.display.set_caption('Inteligenty Traktor')
|
|
|
|
|
2024-06-08 12:41:46 +02:00
|
|
|
|
2024-04-27 00:34:03 +02:00
|
|
|
class Node:
|
2024-06-08 12:41:46 +02:00
|
|
|
def __init__(self, state, parent=None, action=None, cost=0):
|
|
|
|
self.state = state # Stan reprezentowany przez węzeł
|
|
|
|
self.parent = parent # Węzeł rodzica
|
|
|
|
self.action = action # Akcja prowadząca do tego stanu
|
|
|
|
self.cost = cost # Koszt przejścia do tego stanu
|
|
|
|
self.f = 0 # Wartość funkcji priorytetowej
|
|
|
|
self.tie_breaker = 0 # Wartość używana do rozwiązywania konfliktów priorytetów
|
2024-04-27 00:34:03 +02:00
|
|
|
|
|
|
|
def __lt__(self, other):
|
2024-06-08 12:41:46 +02:00
|
|
|
# Porównanie węzłów w celu ustalenia kolejności w kolejce priorytetowej
|
|
|
|
if self.f == other.f:
|
|
|
|
return self.tie_breaker > other.tie_breaker # Większy tie_breaker ma wyższy priorytet
|
2024-04-27 00:34:03 +02:00
|
|
|
return self.f < other.f
|
2024-06-08 12:41:46 +02:00
|
|
|
# Jesli pare wezlow ma taie same f, to tilebreaker ustawia
|
|
|
|
# prorytety akcje right i down maja wyzszy priorytet
|
2024-04-27 00:34:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
def manhattan(pos0, pos1):
|
2024-06-08 12:41:46 +02:00
|
|
|
# Heurystyka odległości Manhattan
|
2024-04-27 00:34:03 +02:00
|
|
|
d1 = abs(pos1[0] - pos0[0])
|
|
|
|
d2 = abs(pos1[1] - pos0[1])
|
|
|
|
return d1 + d2
|
|
|
|
|
|
|
|
|
2024-06-08 12:41:46 +02:00
|
|
|
def nastepnik(state, board):
|
|
|
|
# Funkcja generująca możliwe następne stany (akcje)
|
|
|
|
x, y = state
|
|
|
|
successors = []
|
|
|
|
actions = [('right', (x+1, y), 1), ('down', (x, y+1), 1), ('up', (x, y-1), 0), ('left', (x-1, y), 0)]
|
|
|
|
for action, next_state, tie_breaker in actions:
|
|
|
|
if 0 <= next_state[0] < cols and 0 <= next_state[1] < rows:
|
|
|
|
cost = board.get_cost(next_state[0], next_state[1])
|
|
|
|
successors.append((action, next_state, cost, tie_breaker))
|
|
|
|
return successors
|
|
|
|
|
|
|
|
|
|
|
|
def goal_test(state, goal):
|
|
|
|
# Czy dany stan jest stanem docelowym
|
|
|
|
return state == goal
|
|
|
|
|
|
|
|
|
|
|
|
def graphsearch(istate, goal, board, heuristic=manhattan):
|
|
|
|
# Algorytm przeszukiwania grafu
|
|
|
|
fringe = [] # Kolejka priorytetowa przechowująca węzły do odwiedzenia
|
|
|
|
explored = set() # Zbiór odwiedzonych stanów
|
|
|
|
start_node = Node(istate)
|
|
|
|
start_node.f = heuristic(istate, goal) # Obliczenie wartości heurystycznej dla stanu początkowego
|
|
|
|
start_node.tie_breaker = 0 # Ustawienie tie_breaker dla węzła startowego,
|
|
|
|
heapq.heappush(fringe, start_node)
|
|
|
|
|
|
|
|
while fringe:
|
|
|
|
elem = heapq.heappop(fringe)
|
|
|
|
if goal_test(elem.state, goal):
|
|
|
|
path = []
|
|
|
|
total_cost = elem.cost # Zapisanie całkowitego kosztu
|
|
|
|
while elem:
|
|
|
|
path.append((elem.state, elem.action))
|
|
|
|
elem = elem.parent
|
|
|
|
return path[::-1], total_cost # Zwrócenie ścieżki i kosztu
|
|
|
|
|
|
|
|
explored.add(elem.state)
|
|
|
|
for action_index, (action, state, cost, tie_breaker) in enumerate(nastepnik(elem.state, board)):
|
|
|
|
x = Node(state, parent=elem, action=action, cost=elem.cost + cost)
|
|
|
|
x.f = x.cost + heuristic(state, goal) # Obliczenie wartości funkcji priorytetowej
|
|
|
|
x.tie_breaker = elem.tie_breaker * 4 + action_index # Obliczanie tie_breaker na podstawie akcji
|
|
|
|
|
|
|
|
if state not in explored and not any(node.state == state for node in fringe):
|
|
|
|
heapq.heappush(fringe, x)
|
|
|
|
else:
|
|
|
|
for i, node in enumerate(fringe):
|
|
|
|
if node.state == state and (node.f > x.f or (node.f == x.f and node.tie_breaker < x.tie_breaker)):
|
|
|
|
fringe[i] = x
|
|
|
|
heapq.heapify(fringe)
|
|
|
|
break
|
|
|
|
|
|
|
|
print("Nie znaleziono ścieżki.")
|
|
|
|
return None, 0 # Zwrócenie ścieżki jako None i kosztu jako 0 w przypadku braku ścieżki
|
|
|
|
|
2024-04-27 00:34:03 +02:00
|
|
|
def main():
|
|
|
|
run = True
|
|
|
|
clock = pygame.time.Clock()
|
|
|
|
board = Board()
|
|
|
|
board.load_images()
|
2024-06-08 12:41:46 +02:00
|
|
|
|
|
|
|
start_state = (9, 9) # Stan początkowy
|
|
|
|
goal_state = (0, 0) # Stan docelowy
|
|
|
|
tractor = Tractor(start_state[1], start_state[0])
|
|
|
|
board.set_grass(start_state[0], start_state[1]) # Ustawienie startowego pola jako trawę
|
|
|
|
board.set_grass(goal_state[0], goal_state[1]) # Ustawienie docelowego pola jako trawę
|
|
|
|
|
|
|
|
path, total_cost = graphsearch(start_state, goal_state, board)
|
2024-04-27 00:34:03 +02:00
|
|
|
|
|
|
|
while run:
|
|
|
|
clock.tick(fps)
|
2024-06-08 12:41:46 +02:00
|
|
|
|
2024-04-27 00:34:03 +02:00
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.QUIT:
|
|
|
|
run = False
|
|
|
|
|
|
|
|
if not path:
|
|
|
|
run = False
|
|
|
|
continue
|
|
|
|
|
2024-06-08 12:41:46 +02:00
|
|
|
next_state, action = path.pop(0) if path else (start_state, None)
|
|
|
|
print(next_state) # Wypisanie następnego stanu
|
|
|
|
tractor.row, tractor.col = next_state[1], next_state[0]
|
2024-04-27 00:34:03 +02:00
|
|
|
|
2024-06-08 12:41:46 +02:00
|
|
|
if action == "right":
|
2024-04-28 21:17:39 +02:00
|
|
|
tractor.direction = "right"
|
2024-06-08 12:41:46 +02:00
|
|
|
elif action == "left":
|
2024-04-28 21:17:39 +02:00
|
|
|
tractor.direction = "left"
|
2024-06-08 12:41:46 +02:00
|
|
|
elif action == "down":
|
2024-04-28 21:17:39 +02:00
|
|
|
tractor.direction = "down"
|
2024-06-08 12:41:46 +02:00
|
|
|
elif action == "up":
|
2024-04-28 21:17:39 +02:00
|
|
|
tractor.direction = "up"
|
2024-06-08 12:41:46 +02:00
|
|
|
|
|
|
|
# Aktualizacja planszy na podstawie położenia traktora
|
|
|
|
if board.is_weed(tractor.col, tractor.row):
|
|
|
|
board.set_grass(tractor.col, tractor.row)
|
|
|
|
elif board.is_dirt(tractor.col, tractor.row):
|
|
|
|
board.set_soil(tractor.col, tractor.row)
|
|
|
|
elif board.is_soil(tractor.col, tractor.row):
|
|
|
|
board.set_carrot(tractor.col, tractor.row)
|
2024-04-27 00:34:03 +02:00
|
|
|
|
|
|
|
board.draw_cubes(WIN)
|
|
|
|
tractor.draw(WIN)
|
|
|
|
pygame.display.update()
|
|
|
|
|
2024-06-08 12:41:46 +02:00
|
|
|
print(f"Całkowity koszt trasy: {total_cost}")
|
|
|
|
|
2024-04-27 00:34:03 +02:00
|
|
|
while True:
|
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.QUIT:
|
|
|
|
pygame.quit()
|
|
|
|
return
|
|
|
|
|
2024-06-08 12:41:46 +02:00
|
|
|
|
2024-04-27 00:34:03 +02:00
|
|
|
main()
|