Generic Algo

This commit is contained in:
s464923 2024-06-08 19:46:06 +02:00
parent 7b358760e9
commit 7c5d9fa852
4 changed files with 47 additions and 56 deletions

View File

@ -1,15 +1,18 @@
import pygame import pygame
from constant import size, rows, cols from constant import size, rows, cols
import random import random
import numpy as np
class Board: class Board:
def __init__(self): def __init__(self,load_from_file=False, filename='generated_board.npy'):
self.board = [] self.board = []
self.vegetables = [] self.vegetables = []
self.soil_features = None self.soil_features = None
self.load_images()
self.generate_board()
self.vegetable_names = [] self.vegetable_names = []
self.load_images()
if load_from_file:
self.load_board_from_file(filename)
def load_images(self): def load_images(self):
self.grass = pygame.image.load("board/grass.png") self.grass = pygame.image.load("board/grass.png")
@ -19,11 +22,11 @@ class Board:
self.soil = pygame.image.load("board/zyzna.png") self.soil = pygame.image.load("board/zyzna.png")
self.warzywa_images = { self.warzywa_images = {
"marchewka": [pygame.image.load(f"warzywa/Carrot/{i}.jpg") for i in range(1, 10)],
"ziemniak": [pygame.image.load(f"warzywa/Potato/{i}.jpg") for i in range(1, 10)],
"pomidor": [pygame.image.load(f"warzywa/tomato/{i}.jpg") for i in range(1, 10)], "pomidor": [pygame.image.load(f"warzywa/tomato/{i}.jpg") for i in range(1, 10)],
"fasola": [pygame.image.load(f"warzywa/Bean/{i}.jpg") for i in range(1, 10)], "fasola": [pygame.image.load(f"warzywa/Bean/{i}.jpg") for i in range(1, 10)],
"dynia": [pygame.image.load(f"warzywa/Pumpkin/{i}.jpg") for i in range(1, 10)], "dynia": [pygame.image.load(f"warzywa/Pumpkin/{i}.jpg") for i in range(1, 10)],
"marchewka": [pygame.image.load(f"warzywa/Carrot/{i}.jpg") for i in range(1, 10)],
"ziemniak": [pygame.image.load(f"warzywa/Potato/{i}.jpg") for i in range(1, 10)],
"rzodkiewka": [pygame.image.load(f"warzywa/Radish/{i}.jpg") for i in range(1, 10)], "rzodkiewka": [pygame.image.load(f"warzywa/Radish/{i}.jpg") for i in range(1, 10)],
"ogorek": [pygame.image.load(f"warzywa/Cucumber/{i}.jpg") for i in range(1, 10)], "ogorek": [pygame.image.load(f"warzywa/Cucumber/{i}.jpg") for i in range(1, 10)],
"kalafior": [pygame.image.load(f"warzywa/Cauliflower/{i}.jpg") for i in range(1, 10)], "kalafior": [pygame.image.load(f"warzywa/Cauliflower/{i}.jpg") for i in range(1, 10)],
@ -31,30 +34,26 @@ class Board:
"brokul": [pygame.image.load(f"warzywa/Broccoli/{i}.jpg") for i in range(1, 10)] "brokul": [pygame.image.load(f"warzywa/Broccoli/{i}.jpg") for i in range(1, 10)]
} }
self.vegetable_types = { self.vegetable_types = {
"marchewka": 1, "marchewka": 2,
"ziemniak": 2, "ziemniak": 3,
"pomidor": 3, "pomidor": 4,
"fasola": 4, "fasola": 5,
"dynia": 5, "dynia": 6,
"rzodkiewka": 6, "rzodkiewka": 7,
"ogorek": 7, "ogorek": 8,
"kalafior": 8, "kalafior": 9,
"kapusta": 9, "kapusta": 10,
"brokul": 10 "brokul": 11
} }
def generate_board(self): def load_board_from_file(self, filename):
self.board = [[random.choice([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) for _ in range(cols)] for _ in range(rows)] self.board = np.load(filename)
self.vegetables = [ self.vegetables = [[None for _ in range(cols)] for _ in range(rows)]
[None for _ in range(cols)] for _ in range(rows) self.vegetable_names = [[None for _ in range(cols)] for _ in range(rows)]
]
self.vegetable_names = [
[None for _ in range(cols)] for _ in range(rows)
]
for row in range(rows): for row in range(rows):
for col in range(cols): for col in range(cols):
if self.board[row][col] in (6, 7, 8, 9): if self.board[row][col] in self.vegetable_types.values():
vegetable_type = random.choice(list(self.warzywa_images.keys())) vegetable_type = list(self.warzywa_images.keys())[self.board[row][col] - 2]
vegetable_image = random.choice(self.warzywa_images[vegetable_type]) vegetable_image = random.choice(self.warzywa_images[vegetable_type])
self.vegetables[row][col] = vegetable_image self.vegetables[row][col] = vegetable_image
self.vegetable_names[row][col] = vegetable_type self.vegetable_names[row][col] = vegetable_type
@ -77,24 +76,18 @@ class Board:
for col in range(cols): for col in range(cols):
cube_rect = pygame.Rect(col * size, row * size, size, size) cube_rect = pygame.Rect(col * size, row * size, size, size)
cube = self.board[row][col] cube = self.board[row][col]
if row == 4 and col == 4: if cube == 0:
win.blit(self.grass, cube_rect) win.blit(self.grass, cube_rect)
elif cube == 0: elif cube == 1:
rock_scale = pygame.transform.scale(self.rock, (size, size)) rock_scale = pygame.transform.scale(self.rock, (size, size))
win.blit(self.dirt, cube_rect) win.blit(self.dirt, cube_rect)
win.blit(rock_scale, cube_rect) win.blit(rock_scale, cube_rect)
elif cube == 1: elif cube == 12:
weed_scale = pygame.transform.scale(self.weeds, (size, size))
win.blit(self.grass, cube_rect)
win.blit(weed_scale, cube_rect)
elif cube in (2, 3, 4, 5):
win.blit(self.grass, cube_rect)
elif cube == 10:
win.blit(self.soil, cube_rect) win.blit(self.soil, cube_rect)
if self.vegetables[row][col]: if self.vegetables[row][col]:
vegetable_image = pygame.transform.scale(self.vegetables[row][col], (size, size)) vegetable_image = pygame.transform.scale(self.vegetables[row][col], (size, size))
win.blit(vegetable_image, cube_rect) win.blit(vegetable_image, cube_rect)
elif cube == 11: elif cube == 13:
win.blit(self.dirt, cube_rect) win.blit(self.dirt, cube_rect)
if self.vegetables[row][col]: if self.vegetables[row][col]:
vegetable_image = pygame.transform.scale(self.vegetables[row][col], (size, size)) vegetable_image = pygame.transform.scale(self.vegetables[row][col], (size, size))
@ -106,21 +99,16 @@ class Board:
win.blit(vegetable_image, cube_rect) win.blit(vegetable_image, cube_rect)
def is_rock(self, row, col): def is_rock(self, row, col):
return self.board[row][col] == 0
def is_weed(self, row, col):
return self.board[row][col] == 1 return self.board[row][col] == 1
def set_grass(self, row, col):
self.board[row][col] = 2
def is_dirt(self, row, col): def is_dirt(self, row, col):
return self.board[row][col] in (6, 7, 8, 9) return self.board[row][col] in (2,3,4,5,6,7,8,9,10,11)
def set_soil(self, row, col): def set_soil(self, row, col):
self.board[row][col] = 10 self.board[row][col] = 12
self.vegetables[row][col] = self.vegetables[row][col] self.vegetables[row][col] = self.vegetables[row][col]
def set_fakedirt(self, row, col): def set_fakedirt(self, row, col):
self.board[row][col] = 11 self.board[row][col] = 13

View File

@ -1,6 +1,6 @@
import pygame import pygame
width, height = 640, 640 width, height = 640, 640
rows, cols = 8, 8 rows, cols = 10, 10
size = width//cols size = width//cols
yellow = (216,178,0) yellow = (216,178,0)
green= (103,178,0) green= (103,178,0)

25
main.py
View File

@ -1,4 +1,5 @@
import pygame import pygame
import numpy as np
from board import Board from board import Board
from constant import width, height, rows, cols from constant import width, height, rows, cols
from tractor import Tractor from tractor import Tractor
@ -6,18 +7,19 @@ from kolejka import Stan, Odwiedzone
from queue import Queue from queue import Queue
import pandas as pd import pandas as pd
from neuralnetwork import load_model from neuralnetwork import load_model
from decisiontree import train_decision_tree
data = pd.read_csv('dane.csv') data = pd.read_csv('dane.csv')
from decisiontree import train_decision_tree
model_path = 'model.pth' model_path = 'model.pth'
neuralnetwork_model = load_model(model_path) neuralnetwork_model = load_model(model_path)
model, feature_columns = train_decision_tree(data) model, feature_columns = train_decision_tree(data)
fps = 5 fps = 15
WIN = pygame.display.set_mode((width, height)) WIN = pygame.display.set_mode((width, height))
pygame.display.set_caption('Inteligentny Traktor') pygame.display.set_caption('Inteligentny Traktor')
@ -56,16 +58,17 @@ def fringe_check(fringe, stan):
return True return True
return False return False
def main(): def main():
initial_state = Stan(4, 4, "down") initial_state = Stan(9, 1, "down")
run = True run = True
clock = pygame.time.Clock() clock = pygame.time.Clock()
board = Board() board = Board(load_from_file=True, filename='generated_board.npy')
board.load_images()
tractor = Tractor(4, 4, model, feature_columns, neuralnetwork_model) tractor = Tractor(9, 1, model, feature_columns, neuralnetwork_model)
while run: while run:
clock.tick(fps) clock.tick(fps)
@ -78,11 +81,11 @@ def main():
if not akcje: if not akcje:
print("Nie znaleziono ścieżki do najbliższego pola dirt.") print("Nie znaleziono ścieżki do najbliższego pola dirt.")
board.generate_board() board = Board(load_from_file=True, filename='generated_board.npy')
initial_state = Stan(4, 4, "down") initial_state = Stan(0, 1, "down")
tractor = Tractor(4, 4, model, feature_columns, neuralnetwork_model) tractor = Tractor(0, 1, model, feature_columns, neuralnetwork_model)
while board.is_rock(initial_state.row, initial_state.col): while board.is_rock(initial_state.row, initial_state.col):
board.generate_board() board = Board(load_from_file=True, filename='generated_board.npy')
continue continue
print("akcje: >", akcje) print("akcje: >", akcje)
@ -103,7 +106,7 @@ def main():
board.draw_cubes(WIN) board.draw_cubes(WIN)
tractor.draw(WIN) tractor.draw(WIN)
pygame.display.update() pygame.display.update()
pygame.time.delay(500)
initial_state = nowy_stan initial_state = nowy_stan
initial_state.direction = tractor.direction initial_state.direction = tractor.direction

View File

@ -76,7 +76,7 @@ class Tractor:
vegetable_type_number = board.vegetable_types[vegetable_name] vegetable_type_number = board.vegetable_types[vegetable_name]
else: else:
vegetable_type_number = -1 vegetable_type_number = -1
if board.is_dirt(next_row, next_col) and board.board[next_row][next_col] != 10: if board.is_dirt(next_row, next_col) and board.board[next_row][next_col] != 12:
soil_features = board.soil_features soil_features = board.soil_features
sample = { sample = {
"wilgotnosc_gleby": soil_features["wilgotnosc_gleby"], "wilgotnosc_gleby": soil_features["wilgotnosc_gleby"],