Traktor/main.py

123 lines
3.4 KiB
Python
Raw Permalink Normal View History

2024-03-10 02:46:14 +01:00
import pygame
2024-06-08 19:46:06 +02:00
import numpy as np
2024-03-10 02:46:14 +01:00
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-06-08 19:46:06 +02:00
from decisiontree import train_decision_tree
2024-03-10 02:46:14 +01:00
2024-05-18 22:51:09 +02:00
data = pd.read_csv('dane.csv')
2024-06-08 19:46:06 +02:00
2024-05-18 22:51:09 +02:00
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
2024-06-08 19:46:06 +02:00
fps = 15
2024-03-10 02:46:14 +01:00
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-06-08 19:46:06 +02:00
2024-03-10 02:46:14 +01:00
def main():
2024-06-08 19:46:06 +02:00
initial_state = Stan(9, 1, "down")
2024-03-10 02:46:14 +01:00
run = True
clock = pygame.time.Clock()
2024-06-06 23:15:26 +02:00
2024-06-08 19:46:06 +02:00
board = Board(load_from_file=True, filename='generated_board.npy')
2024-06-06 23:15:26 +02:00
2024-06-08 19:46:06 +02:00
tractor = Tractor(9, 1, 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.")
2024-06-08 19:46:06 +02:00
board = Board(load_from_file=True, filename='generated_board.npy')
initial_state = Stan(0, 1, "down")
tractor = Tractor(0, 1, model, feature_columns, neuralnetwork_model)
2024-05-18 22:51:09 +02:00
while board.is_rock(initial_state.row, initial_state.col):
2024-06-08 19:46:06 +02:00
board = Board(load_from_file=True, filename='generated_board.npy')
2024-05-18 22:51:09 +02:00
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()
2024-06-08 19:46:06 +02:00
2024-05-18 22:51:09 +02:00
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()