Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
bb6bc0ffa2 | |||
8fcf4fad2b | |||
fb4cd44b61 |
150
astar.py
@ -1,150 +0,0 @@
|
||||
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')
|
||||
|
||||
|
||||
class Node:
|
||||
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
|
||||
|
||||
def __lt__(self, other):
|
||||
# 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
|
||||
return self.f < other.f
|
||||
# Jesli pare wezlow ma taie same f, to tilebreaker ustawia
|
||||
# prorytety akcje right i down maja wyzszy priorytet
|
||||
|
||||
|
||||
def manhattan(pos0, pos1):
|
||||
# Heurystyka odległości Manhattan
|
||||
d1 = abs(pos1[0] - pos0[0])
|
||||
d2 = abs(pos1[1] - pos0[1])
|
||||
return d1 + d2
|
||||
|
||||
|
||||
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
|
||||
|
||||
def main():
|
||||
run = True
|
||||
clock = pygame.time.Clock()
|
||||
board = Board()
|
||||
board.load_images()
|
||||
|
||||
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)
|
||||
|
||||
while run:
|
||||
clock.tick(fps)
|
||||
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
run = False
|
||||
|
||||
if not path:
|
||||
run = False
|
||||
continue
|
||||
|
||||
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]
|
||||
|
||||
if action == "right":
|
||||
tractor.direction = "right"
|
||||
elif action == "left":
|
||||
tractor.direction = "left"
|
||||
elif action == "down":
|
||||
tractor.direction = "down"
|
||||
elif action == "up":
|
||||
tractor.direction = "up"
|
||||
|
||||
# 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)
|
||||
|
||||
board.draw_cubes(WIN)
|
||||
tractor.draw(WIN)
|
||||
pygame.display.update()
|
||||
|
||||
print(f"Całkowity koszt trasy: {total_cost}")
|
||||
|
||||
while True:
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
pygame.quit()
|
||||
return
|
||||
|
||||
|
||||
main()
|
134
board.py
@ -1,95 +1,117 @@
|
||||
import pygame
|
||||
from constant import size, rows, cols
|
||||
import random
|
||||
from tractor import Tractor
|
||||
|
||||
class Board:
|
||||
def __init__(self):
|
||||
self.board = []
|
||||
self.vegetables = []
|
||||
self.soil_features = None
|
||||
self.load_images()
|
||||
self.generate_board()
|
||||
self.load_costs()
|
||||
|
||||
|
||||
def load_images(self):
|
||||
self.grass = pygame.image.load("board/grass.png")
|
||||
self.dirt = pygame.image.load("board/dirt.png")
|
||||
self.rock= pygame.image.load("board/rock.png")
|
||||
self.weeds = pygame.image.load("board/weeds.png")
|
||||
self.rock = pygame.image.load("board/rock.png")
|
||||
self.weeds = pygame.image.load("board/weeds.png")
|
||||
self.soil = pygame.image.load("board/zyzna.png")
|
||||
self.carrot = pygame.image.load("board/carrot.png")
|
||||
|
||||
self.warzywa_images = {
|
||||
"pomidor": pygame.image.load("warzywa/pomidor.png"),
|
||||
"salata": pygame.image.load("warzywa/salata.png"),
|
||||
"cebula": pygame.image.load("warzywa/cebula.png"),
|
||||
"marchewka": pygame.image.load("warzywa/marchewka.png"),
|
||||
"ziemniak": pygame.image.load("warzywa/ziemniak.png"),
|
||||
"papryka": pygame.image.load("warzywa/papryka.png"),
|
||||
"burak": pygame.image.load("warzywa/burak.png"),
|
||||
"brukselka": pygame.image.load("warzywa/brukselka.png"),
|
||||
"szpinak": pygame.image.load("warzywa/szpinak.png"),
|
||||
"rzepak": pygame.image.load("warzywa/rzepak.png")
|
||||
}
|
||||
self.vegetable_types = {
|
||||
"marchewka": 1,
|
||||
"ziemniak": 2,
|
||||
"pomidor": 3,
|
||||
"salata": 4,
|
||||
"cebula": 5,
|
||||
"papryka": 6,
|
||||
"burak": 7,
|
||||
"brukselka": 8,
|
||||
"szpinak": 9,
|
||||
"rzepak": 10
|
||||
}
|
||||
|
||||
def generate_board(self):
|
||||
self.board = [[random.choice([0,1,2,3,4,5,6,7,8,9]) for _ in range(rows)] for _ in range(cols)]
|
||||
self.board = [[random.choice([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) for _ in range(cols)] for _ in range(rows)]
|
||||
self.vegetables = [
|
||||
[random.choice(list(self.warzywa_images.keys())) if self.board[row][col] in (6, 7, 8, 9) else None for col in range(cols)]
|
||||
for row in range(rows)
|
||||
]
|
||||
self.soil_features = self.generate_soil_features()
|
||||
|
||||
|
||||
print(f"Shared soil features: {self.soil_features}")
|
||||
|
||||
def generate_soil_features(self):
|
||||
return {
|
||||
"wilgotnosc_gleby": random.randint(30, 70),
|
||||
"temperatura_gleby": random.randint(13, 26),
|
||||
"opady_deszczu": random.randint(0, 11),
|
||||
"wiek_rosliny": random.randint(1, 9),
|
||||
"proc_ekspo_na_swiatlo": random.randint(10, 90),
|
||||
"pora_dnia": random.randint(8, 20),
|
||||
"pora_roku": random.randint(1, 4)
|
||||
}
|
||||
|
||||
def draw_cubes(self, win):
|
||||
|
||||
for row in range(rows):
|
||||
for col in range(cols):
|
||||
cube_rect = pygame.Rect(row * size, col * size, size, size)
|
||||
cube=self.board[row][col]
|
||||
|
||||
if row==4 and col==4:
|
||||
cube_rect = pygame.Rect(col * size, row * size, size, size)
|
||||
cube = self.board[row][col]
|
||||
if row == 4 and col == 4:
|
||||
win.blit(self.grass, cube_rect)
|
||||
elif cube == 0:
|
||||
rock_scale = pygame.transform.scale(self.rock, (size, size))
|
||||
win.blit(self.dirt, cube_rect)
|
||||
win.blit(rock_scale, cube_rect)
|
||||
win.blit(rock_scale, cube_rect)
|
||||
elif cube == 1:
|
||||
weed_scale = pygame.transform.scale(self.weeds, (size,size))
|
||||
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 in (2, 3, 4, 5):
|
||||
win.blit(self.grass, cube_rect)
|
||||
elif cube == 10:
|
||||
win.blit(self.soil, cube_rect)
|
||||
if self.vegetables[row][col]:
|
||||
vegetable_image = pygame.transform.scale(self.warzywa_images[self.vegetables[row][col]], (size, size))
|
||||
win.blit(vegetable_image, cube_rect)
|
||||
elif cube == 11:
|
||||
carrot_scale = pygame.transform.scale(self.carrot, (size,size))
|
||||
win.blit(self.carrot, cube_rect)
|
||||
win.blit(carrot_scale, cube_rect)
|
||||
|
||||
win.blit(self.dirt, cube_rect)
|
||||
if self.vegetables[row][col]:
|
||||
vegetable_image = pygame.transform.scale(self.warzywa_images[self.vegetables[row][col]],
|
||||
(size, size))
|
||||
win.blit(vegetable_image, cube_rect)
|
||||
else:
|
||||
win.blit(self.dirt, cube_rect)
|
||||
|
||||
|
||||
def load_costs(self):
|
||||
self.costs = {
|
||||
0: 100, #kamien
|
||||
1:2, #chwast
|
||||
2: 1, #po trawie
|
||||
3: 1, #po trawie
|
||||
4: 1, #po trawie
|
||||
5: 1, #po trawie
|
||||
6: 3, #ziemia
|
||||
7: 3, #ziemia
|
||||
8: 3, #ziemia
|
||||
9: 3, #ziemia
|
||||
10: 4, #zyzna
|
||||
11: 10 #marchewka
|
||||
}
|
||||
win.blit(self.dirt, cube_rect)
|
||||
if self.vegetables[row][col]:
|
||||
vegetable_image = pygame.transform.scale(self.warzywa_images[self.vegetables[row][col]], (size, size))
|
||||
win.blit(vegetable_image, cube_rect)
|
||||
|
||||
def get_cost(self, row, col):
|
||||
tile_type = self.board[row][col]
|
||||
return self.costs.get(tile_type, 1)
|
||||
|
||||
def is_rock(self, row, col):
|
||||
tractor = Tractor(row, col)
|
||||
return self.board[row][col] == 0 and not (row == tractor.row and col == tractor.col)
|
||||
return self.board[row][col] == 0
|
||||
|
||||
def is_weed(self,row,col):
|
||||
def is_weed(self, row, col):
|
||||
return self.board[row][col] == 1
|
||||
|
||||
def set_grass(self,row,col):
|
||||
self.board[row][col]=2
|
||||
def set_grass(self, row, col):
|
||||
self.board[row][col] = 2
|
||||
|
||||
def is_dirt(self, row, col):
|
||||
return self.board[row][col] in (6, 7, 8, 9)
|
||||
|
||||
def is_dirt(self,row,col):
|
||||
return self.board[row][col] in (6,7,8,9)
|
||||
|
||||
def is_soil(self, row, col):
|
||||
return self.board[row][col] == 10
|
||||
|
||||
def set_soil(self, row, col):
|
||||
self.board[row][col] = 10
|
||||
|
||||
def set_carrot(self, row, col):
|
||||
self.board[row][col] = 11
|
||||
self.vegetables[row][col] = self.vegetables[row][col]
|
||||
def set_fakedirt(self,row,col):
|
||||
self.board[row][col] = 11
|
||||
|
BIN
board/carrot.png
Before Width: | Height: | Size: 232 KiB |
@ -1,6 +1,6 @@
|
||||
import pygame
|
||||
width, height = 640, 640
|
||||
rows, cols = 10, 10
|
||||
rows, cols = 8, 8
|
||||
size = width//cols
|
||||
yellow = (216,178,0)
|
||||
green= (103,178,0)
|
||||
|
@ -1,37 +1,23 @@
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.tree import DecisionTreeClassifier, plot_tree
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
from sklearn.tree import DecisionTreeClassifier
|
||||
import pandas as pd
|
||||
|
||||
|
||||
data = pd.read_csv("dane.csv")
|
||||
print(data)
|
||||
|
||||
# Wczytanie danych
|
||||
X = data.drop(columns=["podlac"])
|
||||
X = pd.get_dummies(X)
|
||||
y = data["podlac"]
|
||||
|
||||
# Podział danych na zbiór treningowy i testowy
|
||||
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
|
||||
|
||||
# Inicjalizacja i dopasowanie modelu drzewa decyzyjnego
|
||||
model = DecisionTreeClassifier(max_depth=4)
|
||||
model.fit(X_train, y_train)
|
||||
|
||||
# Wyliczenie poprawności algorytmu
|
||||
accuracy = model.score(X_test, y_test)
|
||||
print("Accuracy:", accuracy)
|
||||
def train_decision_tree(data):
|
||||
X = data.drop(columns=["podlac"])
|
||||
y = data["podlac"]
|
||||
model = DecisionTreeClassifier()
|
||||
model.fit(X, y)
|
||||
return model, X.columns
|
||||
|
||||
|
||||
def predict(model, feature_columns, sample):
|
||||
sample_df = pd.DataFrame([sample])
|
||||
sample_df = pd.get_dummies(sample_df)
|
||||
sample_df = sample_df.reindex(columns=feature_columns, fill_value=0)
|
||||
return model.predict(sample_df)
|
||||
|
||||
|
||||
|
||||
# Wyświetlenie drzewa decyzyjnego
|
||||
plt.figure(figsize=(20, 20))
|
||||
plot_tree(model, feature_names=X.columns, class_names=sorted(y.unique()), filled=True)
|
||||
plt.show()
|
||||
|
||||
#Marchew = 1
|
||||
#zmiemniaki = 2
|
||||
#pomidor = 3
|
||||
|
83
kolejka.py
@ -1,80 +1,39 @@
|
||||
from constant import width, height, rows, cols
|
||||
from constant import rows, cols
|
||||
|
||||
class Stan:
|
||||
def __init__(self, row, col, dir):
|
||||
def __init__(self, row, col, direction):
|
||||
self.p = []
|
||||
self.a = ""
|
||||
self.row = row
|
||||
self.col = col
|
||||
self.direction = dir
|
||||
self.direction = direction
|
||||
def __str__(self):
|
||||
return f"row: {self.row}, col: {self.col}, direction: {self.direction}"
|
||||
|
||||
def parrent(self, stan, action):
|
||||
if(len(self.p) == 0):
|
||||
if len(self.p) == 0:
|
||||
self.p.append(stan)
|
||||
self.a = action
|
||||
|
||||
def succ(self, action, board):
|
||||
if(action == "up"):
|
||||
if(self.direction == "left"):
|
||||
if not board.is_rock(max(self.col - 1, 0), self.row):
|
||||
return Stan( self.row , max(self.col - 1, 0), self.direction)
|
||||
return Stan(self.row, self.col, self.direction)
|
||||
if(self.direction == "up"):
|
||||
if not board.is_rock(self.col, max(self.row - 1, 0)):
|
||||
return Stan(max(self.row - 1,0) , self.col, self.direction)
|
||||
return Stan(self.row, self.col, self.direction)
|
||||
if(self.direction == "right"):
|
||||
if not board.is_rock(min(self.col + 1, cols - 1), self.row):
|
||||
return Stan(self.row, min(self.col + 1, cols - 1) , self.direction)
|
||||
return Stan(self.row, self.col, self.direction)
|
||||
if(self.direction == "down"):
|
||||
if not board.is_rock(self.col, min(self.row + 1,rows - 1 )):
|
||||
return Stan(min(self.row + 1,rows - 1 ) , self.col, self.direction)
|
||||
return Stan(self.row, self.col, self.direction)
|
||||
if(action == "left"):
|
||||
if(self.direction == "left"):
|
||||
return Stan(self.row , self.col, "down")
|
||||
if(self.direction == "up"):
|
||||
return Stan(self.row, self.col, "left")
|
||||
if(self.direction == "right"):
|
||||
return Stan(self.row, self.col, "up")
|
||||
if(self.direction == "down"):
|
||||
return Stan(self.row, self.col , "right")
|
||||
if(action == "right"):
|
||||
if(self.direction == "left"):
|
||||
return Stan(self.row, self.col, "up")
|
||||
if(self.direction == "up"):
|
||||
return Stan(self.row, self.col, "right")
|
||||
if(self.direction == "right"):
|
||||
return Stan(self.row, self.col, "down")
|
||||
if(self.direction == "down"):
|
||||
return Stan(self.row, self.col,"left")
|
||||
move_offsets = {
|
||||
"up": {"up": (-1, 0), "left": (0, -1), "down": (1, 0), "right": (0, 1)},
|
||||
"left": {"up": "left", "left": "down", "down": "right", "right": "up"},
|
||||
"right": {"up": "right", "right": "down", "down": "left", "left": "up"}
|
||||
}
|
||||
|
||||
class Kolejka:
|
||||
def __init__(self):
|
||||
self.id = 0
|
||||
self.len = 0
|
||||
self.stany = []
|
||||
if action == "up":
|
||||
row_offset, col_offset = move_offsets[action][self.direction]
|
||||
new_row = self.row + row_offset
|
||||
new_col = self.col + col_offset
|
||||
if 0 <= new_row < rows and 0 <= new_col < cols and not board.is_rock(new_row, new_col):
|
||||
return Stan(new_row, new_col, self.direction)
|
||||
|
||||
def dodaj_stan(self, stan):
|
||||
self.stany.append(stan)
|
||||
self.len += 1
|
||||
def usun_stan(self):
|
||||
if not self.czy_pusta():
|
||||
self.id += 1
|
||||
return self.stany[self.id - 1]
|
||||
else:
|
||||
raise IndexError("Kolejka stanów jest pusta")
|
||||
elif action in ["left", "right"]:
|
||||
new_direction = move_offsets[action][self.direction]
|
||||
return Stan(self.row, self.col, new_direction)
|
||||
|
||||
def czy_pusta(self):
|
||||
return self.len <= self.id
|
||||
|
||||
def check(self, stan):
|
||||
indeks = 0
|
||||
for i in self.stany:
|
||||
if(stan.direction == i.direction and stan.col == i.col and stan.row == i.row):
|
||||
return True
|
||||
return False
|
||||
return None
|
||||
|
||||
class Odwiedzone:
|
||||
def __init__(self):
|
||||
|
180
main.py
@ -2,135 +2,113 @@ import pygame
|
||||
from board import Board
|
||||
from constant import width, height, rows, cols
|
||||
from tractor import Tractor
|
||||
from kolejka import Stan, Kolejka, Odwiedzone
|
||||
from kolejka import Stan, Odwiedzone
|
||||
from queue import Queue
|
||||
import pandas as pd
|
||||
|
||||
|
||||
data = pd.read_csv('dane.csv')
|
||||
|
||||
|
||||
from decisiontree import train_decision_tree
|
||||
|
||||
|
||||
model, feature_columns = train_decision_tree(data)
|
||||
|
||||
fps = 5
|
||||
WIN = pygame.display.set_mode((width, height))
|
||||
pygame.display.set_caption('Inteligentny Traktor')
|
||||
|
||||
pygame.display.set_caption('Inteligenty Traktor')
|
||||
def goal_test(elem, board):
|
||||
return board.is_dirt(elem.row, elem.col)
|
||||
|
||||
def goal_test(elem, goaltest, board):
|
||||
if board.is_rock(goaltest.col, goaltest.row):
|
||||
return True
|
||||
if((elem.row == goaltest.row) and (elem.col == goaltest.col)):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
def actions(elem, istate):
|
||||
akcje = []
|
||||
while((elem.row != istate.row) or (elem.col != istate.col) or (elem.direction != istate.direction)):
|
||||
while elem.row != istate.row or elem.col != istate.col or elem.direction != istate.direction:
|
||||
akcje.append(elem.a)
|
||||
elem = elem.p[0]
|
||||
return akcje
|
||||
return akcje[::-1]
|
||||
|
||||
def graphsearch(istate, goaltest, board):
|
||||
def graphsearch(istate, board):
|
||||
explored = Odwiedzone()
|
||||
fringe = Kolejka()
|
||||
fringe.dodaj_stan(istate)
|
||||
fringe = Queue()
|
||||
fringe.put(istate)
|
||||
moves = ["up", "left", "right"]
|
||||
while not fringe.czy_pusta():
|
||||
elem = fringe.usun_stan()
|
||||
if goal_test(elem, goaltest, board):
|
||||
return actions(elem, istate)
|
||||
while not fringe.empty():
|
||||
elem = fringe.get()
|
||||
if goal_test(elem, board):
|
||||
return actions(elem, istate), elem
|
||||
explored.dodaj_stan(elem)
|
||||
for action in moves:
|
||||
stan = elem.succ(action, board)
|
||||
if((not fringe.check(stan)) and (not explored.check(stan))):
|
||||
stan.parrent(elem, action)
|
||||
fringe.dodaj_stan(stan)
|
||||
return "Brak sciezki"
|
||||
if stan is not None:
|
||||
if not fringe_check(fringe, stan) and not explored.check(stan):
|
||||
stan.parrent(elem, action)
|
||||
fringe.put(stan)
|
||||
return [], None
|
||||
|
||||
def fringe_check(fringe, stan):
|
||||
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
|
||||
|
||||
def main():
|
||||
rotation = ["left", "up", "right", "down"]
|
||||
istate = Stan(4,4, "down")
|
||||
goaltest = Stan(2,3, "up")
|
||||
initial_state = Stan(4, 4, "down")
|
||||
run = True
|
||||
clock = pygame.time.Clock()
|
||||
board = Board()
|
||||
board.load_images()
|
||||
actions = graphsearch(istate, goaltest, board)
|
||||
print("akcje: >",actions )
|
||||
tractor = Tractor(2, 3)
|
||||
tractor = Tractor(4, 4, model, feature_columns)
|
||||
|
||||
while run:
|
||||
clock.tick(fps)
|
||||
|
||||
if all(not board.is_dirt(row, col) for row in range(rows) for col in range(cols)):
|
||||
print("Traktor odwiedził wszystkie pola.")
|
||||
break
|
||||
|
||||
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")
|
||||
tractor = Tractor(4, 4, model, feature_columns)
|
||||
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":
|
||||
tractor.turn_left()
|
||||
elif akcja == "right":
|
||||
tractor.turn_right()
|
||||
elif akcja == "up":
|
||||
tractor.move_forward(board)
|
||||
|
||||
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:
|
||||
run = False
|
||||
pygame.quit()
|
||||
return
|
||||
|
||||
keys = pygame.key.get_pressed()
|
||||
|
||||
if keys[pygame.K_UP]:
|
||||
if keys[pygame.K_UP]:
|
||||
if(tractor.direction == "up" and tractor.row > 0 ):
|
||||
if board.is_weed(tractor.col, tractor.row - 1):
|
||||
board.set_grass(tractor.col, tractor.row - 1)
|
||||
tractor.row -= 1
|
||||
elif board.is_dirt(tractor.col, tractor.row - 1):
|
||||
board.set_soil(tractor.col, tractor.row - 1)
|
||||
tractor.row -= 1
|
||||
elif board.is_soil(tractor.col, tractor.row - 1):
|
||||
board.set_carrot(tractor.col, tractor.row - 1)
|
||||
tractor.row -= 1
|
||||
elif not board.is_rock(tractor.col, tractor.row - 1):
|
||||
tractor.row -= 1
|
||||
|
||||
if(tractor.direction == "left" and tractor.col > 0):
|
||||
if board.is_weed(tractor.col - 1, tractor.row):
|
||||
board.set_grass(tractor.col - 1, tractor.row)
|
||||
tractor.col -= 1
|
||||
elif board.is_dirt(tractor.col - 1, tractor.row):
|
||||
board.set_soil(tractor.col - 1, tractor.row)
|
||||
tractor.col -= 1
|
||||
elif board.is_soil(tractor.col - 1, tractor.row):
|
||||
board.set_carrot(tractor.col - 1, tractor.row)
|
||||
tractor.col -= 1
|
||||
elif not board.is_rock(tractor.col - 1, tractor.row):
|
||||
tractor.col -= 1
|
||||
if(tractor.direction == "down" and tractor.row < rows - 1):
|
||||
if board.is_weed(tractor.col, tractor.row + 1):
|
||||
board.set_grass(tractor.col, tractor.row + 1)
|
||||
tractor.row += 1
|
||||
elif board.is_dirt(tractor.col, tractor.row + 1):
|
||||
board.set_soil(tractor.col, tractor.row + 1)
|
||||
tractor.row += 1
|
||||
elif board.is_soil(tractor.col, tractor.row + 1):
|
||||
board.set_carrot(tractor.col, tractor.row + 1)
|
||||
tractor.row += 1
|
||||
elif not board.is_rock(tractor.col, tractor.row + 1):
|
||||
tractor.row += 1
|
||||
if(tractor.direction == "right" and tractor.col < cols - 1):
|
||||
if board.is_weed(tractor.col + 1, tractor.row):
|
||||
board.set_grass(tractor.col + 1, tractor.row)
|
||||
tractor.col += 1
|
||||
elif board.is_dirt(tractor.col + 1, tractor.row):
|
||||
board.set_soil(tractor.col + 1, tractor.row)
|
||||
tractor.col += 1
|
||||
elif board.is_soil(tractor.col + 1, tractor.row ):
|
||||
board.set_carrot(tractor.col + 1, tractor.row )
|
||||
tractor.col += 1
|
||||
elif not board.is_rock(tractor.col + 1, tractor.row):
|
||||
tractor.col += 1
|
||||
if keys[pygame.K_LEFT]:
|
||||
for i in range(0, 4):
|
||||
if(tractor.direction == rotation[i]):
|
||||
if(i == 0):
|
||||
i = 4
|
||||
tractor.direction = rotation[i-1]
|
||||
break
|
||||
if keys[pygame.K_RIGHT]:
|
||||
for i in range(0, 4):
|
||||
if(tractor.direction == rotation[i]):
|
||||
if(i == 3):
|
||||
i = -1
|
||||
tractor.direction = rotation[i+1]
|
||||
break
|
||||
|
||||
board.draw_cubes(WIN)
|
||||
tractor.draw(WIN)
|
||||
pygame.display.update()
|
||||
pygame.quit()
|
||||
|
||||
main()
|
||||
|
||||
|
66
tractor.py
@ -1,17 +1,75 @@
|
||||
import pygame
|
||||
from constant import size
|
||||
from constant import size, rows, cols
|
||||
from decisiontree import predict
|
||||
|
||||
|
||||
class Tractor:
|
||||
def __init__(self, row, col):
|
||||
def __init__(self, row, col, model, feature_columns):
|
||||
self.row = row
|
||||
self.col = col
|
||||
self.images = {
|
||||
"up": pygame.image.load("tractor/up.png"),
|
||||
"down": pygame.image.load("tractor/down.png"),
|
||||
"left": pygame.image.load("tractor/left.png"),
|
||||
"right":pygame.image.load("tractor/right.png")
|
||||
"right": pygame.image.load("tractor/right.png")
|
||||
}
|
||||
self.direction = "down"
|
||||
self.model = model
|
||||
self.feature_columns = feature_columns
|
||||
|
||||
def draw(self, win):
|
||||
tractor_image = self.images[self.direction]
|
||||
win.blit(tractor_image, (self.col*size, self.row*size))
|
||||
win.blit(tractor_image, (self.col * size, self.row * size))
|
||||
|
||||
def turn_left(self):
|
||||
if self.direction == "up":
|
||||
self.direction = "left"
|
||||
elif self.direction == "left":
|
||||
self.direction = "down"
|
||||
elif self.direction == "down":
|
||||
self.direction = "right"
|
||||
elif self.direction == "right":
|
||||
self.direction = "up"
|
||||
|
||||
def turn_right(self):
|
||||
if self.direction == "up":
|
||||
self.direction = "right"
|
||||
elif self.direction == "right":
|
||||
self.direction = "down"
|
||||
elif self.direction == "down":
|
||||
self.direction = "left"
|
||||
elif self.direction == "left":
|
||||
self.direction = "up"
|
||||
|
||||
def move_forward(self, board):
|
||||
if self.direction == "up" and self.row > 0:
|
||||
next_row, next_col = self.row - 1, self.col
|
||||
elif self.direction == "left" and self.col > 0:
|
||||
next_row, next_col = self.row, self.col - 1
|
||||
elif self.direction == "down" and self.row < rows - 1:
|
||||
next_row, next_col = self.row + 1, self.col
|
||||
elif self.direction == "right" and self.col < cols - 1:
|
||||
next_row, next_col = self.row, self.col + 1
|
||||
else:
|
||||
return # Nie możemy się ruszyć poza planszę
|
||||
|
||||
if board.is_dirt(next_row, next_col) and board.board[next_row][next_col] != 10:
|
||||
soil_features = board.soil_features
|
||||
sample = {
|
||||
"wilgotnosc_gleby": soil_features["wilgotnosc_gleby"],
|
||||
"temperatura_gleby": soil_features["temperatura_gleby"],
|
||||
"opady_deszczu": soil_features["opady_deszczu"],
|
||||
"wiek_rosliny": soil_features["wiek_rosliny"],
|
||||
"proc_ekspo_na_swiatlo": soil_features["proc_ekspo_na_swiatlo"],
|
||||
"pora_dnia": soil_features["pora_dnia"],
|
||||
"pora_roku": soil_features["pora_roku"],
|
||||
"roslina": board.vegetable_types[board.vegetables[next_row][next_col]]
|
||||
}
|
||||
|
||||
should_water = predict(self.model, self.feature_columns, sample)
|
||||
print(f"Predykcja dla pola ({next_row}, {next_col}) z cechami {sample}: {should_water}")
|
||||
if should_water[0].strip() == 'tak':
|
||||
board.set_soil(next_row, next_col)
|
||||
else:
|
||||
board.set_fakedirt(next_row, next_col)
|
||||
self.row, self.col = next_row, next_col # Przemieszczamy traktor niezależnie od predykcji
|
||||
|
BIN
warzywa/brukselka.png
Normal file
After Width: | Height: | Size: 294 KiB |
BIN
warzywa/burak.png
Normal file
After Width: | Height: | Size: 240 KiB |
BIN
warzywa/cebula.png
Normal file
After Width: | Height: | Size: 83 KiB |
BIN
warzywa/marchewka.png
Normal file
After Width: | Height: | Size: 115 KiB |
BIN
warzywa/papryka.png
Normal file
After Width: | Height: | Size: 210 KiB |
BIN
warzywa/pomidor.png
Normal file
After Width: | Height: | Size: 240 KiB |
BIN
warzywa/rzepak.png
Normal file
After Width: | Height: | Size: 76 KiB |
BIN
warzywa/salata.png
Normal file
After Width: | Height: | Size: 230 KiB |
BIN
warzywa/szpinak.png
Normal file
After Width: | Height: | Size: 200 KiB |
BIN
warzywa/ziemniak.png
Normal file
After Width: | Height: | Size: 184 KiB |