Traktor/main.py

117 lines
3.3 KiB
Python
Raw Normal View History

2024-03-10 02:46:14 +01:00
import pygame
from board import Board
from constant import width, height, rows, cols
from tractor import Tractor
2024-05-18 17:10:56 +02:00
from kolejka import Stan, Odwiedzone
from queue import Queue
2024-05-18 22:51:09 +02:00
import pandas as pd
2024-05-23 01:57:24 +02:00
from neuralnetwork import load_model
2024-03-10 02:46:14 +01:00
2024-05-18 22:51:09 +02:00
data = pd.read_csv('dane.csv')
from decisiontree import train_decision_tree
2024-05-23 01:57:24 +02:00
model_path = 'model.pth'
neuralnetwork_model = load_model(model_path)
2024-05-18 22:51:09 +02:00
model, feature_columns = train_decision_tree(data)
2024-03-10 02:46:14 +01:00
fps = 5
WIN = pygame.display.set_mode((width, height))
2024-05-18 22:51:09 +02:00
pygame.display.set_caption('Inteligentny Traktor')
2024-03-10 02:46:14 +01:00
2024-05-18 22:51:09 +02:00
def goal_test(elem, board):
return board.is_dirt(elem.row, elem.col)
2024-03-10 02:46:14 +01:00
2024-04-14 14:37:32 +02:00
def actions(elem, istate):
akcje = []
2024-05-18 22:51:09 +02:00
while elem.row != istate.row or elem.col != istate.col or elem.direction != istate.direction:
2024-04-14 14:37:32 +02:00
akcje.append(elem.a)
elem = elem.p[0]
2024-05-18 22:51:09 +02:00
return akcje[::-1]
2024-05-18 17:10:56 +02:00
2024-05-18 22:51:09 +02:00
def graphsearch(istate, board):
2024-04-14 14:37:32 +02:00
explored = Odwiedzone()
2024-05-18 17:10:56 +02:00
fringe = Queue()
fringe.put(istate)
2024-04-14 14:37:32 +02:00
moves = ["up", "left", "right"]
2024-05-18 17:10:56 +02:00
while not fringe.empty():
elem = fringe.get()
2024-05-18 22:51:09 +02:00
if goal_test(elem, board):
return actions(elem, istate), elem
2024-04-14 14:37:32 +02:00
explored.dodaj_stan(elem)
for action in moves:
stan = elem.succ(action, board)
2024-05-18 17:10:56 +02:00
if stan is not None:
2024-05-18 22:51:09 +02:00
if not fringe_check(fringe, stan) and not explored.check(stan):
2024-05-18 17:10:56 +02:00
stan.parrent(elem, action)
fringe.put(stan)
2024-05-18 22:51:09 +02:00
return [], None
2024-04-14 14:37:32 +02:00
2024-05-18 22:51:09 +02:00
def fringe_check(fringe, stan):
2024-05-18 17:10:56 +02:00
with fringe.mutex:
for item in fringe.queue:
if stan.direction == item.direction and stan.col == item.col and stan.row == item.row:
return True
return False
2024-05-18 22:51:09 +02:00
2024-03-10 02:46:14 +01:00
def main():
2024-05-18 22:51:09 +02:00
initial_state = Stan(4, 4, "down")
2024-03-10 02:46:14 +01:00
run = True
clock = pygame.time.Clock()
board = Board()
board.load_images()
2024-05-23 01:57:24 +02:00
tractor = Tractor(4, 4, model, feature_columns, neuralnetwork_model)
2024-05-18 22:51:09 +02:00
2024-03-10 02:46:14 +01:00
while run:
clock.tick(fps)
2024-05-18 22:51:09 +02:00
if all(not board.is_dirt(row, col) for row in range(rows) for col in range(cols)):
print("Traktor odwiedził wszystkie pola.")
break
2024-03-10 02:46:14 +01:00
2024-05-18 22:51:09 +02:00
akcje, nowy_stan = graphsearch(initial_state, board)
if not akcje:
print("Nie znaleziono ścieżki do najbliższego pola dirt.")
board.generate_board()
initial_state = Stan(4, 4, "down")
2024-05-23 01:57:24 +02:00
tractor = Tractor(4, 4, model, feature_columns, neuralnetwork_model)
2024-05-18 22:51:09 +02:00
while board.is_rock(initial_state.row, initial_state.col):
board.generate_board()
continue
print("akcje: >", akcje)
while akcje:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
akcja = akcje.pop(0)
if akcja == "left":
2024-05-18 17:10:56 +02:00
tractor.turn_left()
2024-05-18 22:51:09 +02:00
elif akcja == "right":
2024-05-18 17:10:56 +02:00
tractor.turn_right()
2024-05-18 22:51:09 +02:00
elif akcja == "up":
2024-05-18 17:10:56 +02:00
tractor.move_forward(board)
2024-03-24 19:49:08 +01:00
2024-05-18 22:51:09 +02:00
board.draw_cubes(WIN)
tractor.draw(WIN)
pygame.display.update()
pygame.time.delay(500)
initial_state = nowy_stan
initial_state.direction = tractor.direction
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return
2024-03-10 02:46:14 +01:00
pygame.quit()
2024-04-14 14:37:32 +02:00
main()