Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
0c5532ac0d | |||
11d091de6a | |||
|
a56dad13cf | ||
a04f4692c1 | |||
06e13b8f19 | |||
|
9c398488e5 | ||
|
47d1380266 | ||
5094ee732c | |||
9eb5029b38 | |||
8cdf6d8118 |
@ -1,4 +1,7 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
|
<component name="Black">
|
||||||
|
<option name="sdkName" value="Python 3.9 (traktor)" />
|
||||||
|
</component>
|
||||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (traktor)" project-jdk-type="Python SDK" />
|
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (traktor)" project-jdk-type="Python SDK" />
|
||||||
</project>
|
</project>
|
BIN
__pycache__/board.cpython-312.pyc
Normal file
BIN
__pycache__/board.cpython-312.pyc
Normal file
Binary file not shown.
BIN
__pycache__/constant.cpython-312.pyc
Normal file
BIN
__pycache__/constant.cpython-312.pyc
Normal file
Binary file not shown.
BIN
__pycache__/kolejka.cpython-312.pyc
Normal file
BIN
__pycache__/kolejka.cpython-312.pyc
Normal file
Binary file not shown.
BIN
__pycache__/tractor.cpython-312.pyc
Normal file
BIN
__pycache__/tractor.cpython-312.pyc
Normal file
Binary file not shown.
150
astar.py
Normal file
150
astar.py
Normal file
@ -0,0 +1,150 @@
|
|||||||
|
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()
|
43
board.py
43
board.py
@ -1,13 +1,14 @@
|
|||||||
import pygame
|
import pygame
|
||||||
from constant import size, rows, cols
|
from constant import size, rows, cols
|
||||||
import random
|
import random
|
||||||
|
from tractor import Tractor
|
||||||
|
|
||||||
class Board:
|
class Board:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.board = []
|
self.board = []
|
||||||
self.load_images()
|
self.load_images()
|
||||||
self.generate_board()
|
self.generate_board()
|
||||||
|
self.load_costs()
|
||||||
|
|
||||||
|
|
||||||
def load_images(self):
|
def load_images(self):
|
||||||
@ -16,6 +17,7 @@ class Board:
|
|||||||
self.rock= pygame.image.load("board/rock.png")
|
self.rock= pygame.image.load("board/rock.png")
|
||||||
self.weeds = pygame.image.load("board/weeds.png")
|
self.weeds = pygame.image.load("board/weeds.png")
|
||||||
self.soil = pygame.image.load("board/zyzna.png")
|
self.soil = pygame.image.load("board/zyzna.png")
|
||||||
|
self.carrot = pygame.image.load("board/carrot.png")
|
||||||
|
|
||||||
def generate_board(self):
|
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(rows)] for _ in range(cols)]
|
||||||
@ -41,11 +43,38 @@ class Board:
|
|||||||
win.blit(self.grass, cube_rect)
|
win.blit(self.grass, cube_rect)
|
||||||
elif cube == 10:
|
elif cube == 10:
|
||||||
win.blit(self.soil, cube_rect)
|
win.blit(self.soil, 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)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
win.blit(self.dirt, cube_rect)
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
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):
|
def is_rock(self, row, col):
|
||||||
return self.board[row][col] == 0
|
tractor = Tractor(row, col)
|
||||||
|
return self.board[row][col] == 0 and not (row == tractor.row and col == tractor.col)
|
||||||
|
|
||||||
def is_weed(self,row,col):
|
def is_weed(self,row,col):
|
||||||
return self.board[row][col] == 1
|
return self.board[row][col] == 1
|
||||||
@ -55,6 +84,12 @@ class Board:
|
|||||||
|
|
||||||
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 (6,7,8,9)
|
||||||
|
|
||||||
|
def is_soil(self, row, col):
|
||||||
|
return self.board[row][col] == 10
|
||||||
|
|
||||||
def set_soil(self, row, col):
|
def set_soil(self, row, col):
|
||||||
self.board[row][col] = 10
|
self.board[row][col] = 10
|
||||||
|
|
||||||
|
def set_carrot(self, row, col):
|
||||||
|
self.board[row][col] = 11
|
BIN
board/carrot.png
Normal file
BIN
board/carrot.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 232 KiB |
@ -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)
|
||||||
|
206
dane.csv
Normal file
206
dane.csv
Normal file
@ -0,0 +1,206 @@
|
|||||||
|
wilgotnosc_gleby,temperatura_gleby,opady_deszczu,wiek_rosliny,proc_ekspo_na_swiatlo,pora_dnia,pora_roku,roslina,podlac
|
||||||
|
40, 20, 5, 3, 70, 10, 2, 1, nie
|
||||||
|
60, 25, 0, 2, 50, 15, 3, 2, tak
|
||||||
|
30, 18, 10, 1, 80, 12, 4, 3, nie
|
||||||
|
50, 22, 3, 4, 60, 18, 1, 4, tak
|
||||||
|
45, 23, 2, 5, 75, 8, 2, 5, tak
|
||||||
|
55, 26, 8, 3, 70, 14, 3, 6, nie
|
||||||
|
35, 21, 1, 2, 55, 16, 4, 7, tak
|
||||||
|
65, 24, 6, 4, 80, 11, 1, 8, nie
|
||||||
|
42, 20, 4, 3, 65, 13, 2, 9, tak
|
||||||
|
48, 22, 7, 5, 75, 9, 3, 10, tak
|
||||||
|
30, 25, 0, 1, 70, 14, 2, 1, tak
|
||||||
|
60, 18, 10, 2, 60, 8, 3, 2, nie
|
||||||
|
35, 20, 2, 3, 50, 12, 4, 3, tak
|
||||||
|
50, 23, 1, 4, 65, 17, 1, 4, tak
|
||||||
|
45, 24, 5, 5, 80, 10, 2, 5, tak
|
||||||
|
55, 22, 3, 3, 75, 13, 3, 6, nie
|
||||||
|
40, 26, 8, 6, 70, 16, 1, 7, nie
|
||||||
|
62, 21, 1, 4, 85, 9, 2, 8, tak
|
||||||
|
47, 25, 6, 5, 60, 11, 3, 9, nie
|
||||||
|
58, 20, 4, 3, 75, 14, 4, 10, tak
|
||||||
|
38, 24, 7, 3, 65, 11, 1, 1, tak
|
||||||
|
59, 20, 3, 2, 55, 16, 2, 2, nie
|
||||||
|
33, 22, 5, 1, 70, 9, 3, 3, nie
|
||||||
|
52, 25, 2, 4, 80, 14, 4, 4, tak
|
||||||
|
46, 21, 1, 5, 75, 10, 1, 5, tak
|
||||||
|
56, 26, 8, 3, 70, 15, 2, 6, nie
|
||||||
|
34, 23, 0, 2, 60, 8, 3, 7, nie
|
||||||
|
63, 18, 4, 4, 85, 12, 4, 8, nie
|
||||||
|
41, 20, 1, 3, 65, 13, 1, 9, tak
|
||||||
|
49, 22, 9, 5, 75, 17, 2, 10, tak
|
||||||
|
29, 25, 0, 2, 70, 14, 3, 1, tak
|
||||||
|
61, 18, 11, 3, 60, 8, 1, 2, nie
|
||||||
|
37, 20, 2, 4, 50, 12, 2, 3, tak
|
||||||
|
51, 23, 0, 5, 65, 16, 4, 4, tak
|
||||||
|
44, 24, 4, 6, 80, 10, 3, 5, tak
|
||||||
|
54, 22, 3, 3, 75, 13, 1, 6, nie
|
||||||
|
39, 26, 7, 1, 70, 15, 2, 7, nie
|
||||||
|
64, 21, 0, 4, 85, 9, 3, 8, tak
|
||||||
|
48, 25, 5, 5, 60, 11, 4, 9, nie
|
||||||
|
57, 20, 2, 3, 75, 14, 1, 10, tak
|
||||||
|
32, 24, 8, 3, 65, 10, 2, 1, tak
|
||||||
|
62, 19, 2, 2, 55, 15, 3, 2, nie
|
||||||
|
36, 21, 1, 1, 70, 12, 4, 3, nie
|
||||||
|
53, 22, 4, 4, 80, 16, 1, 4, tak
|
||||||
|
42, 23, 2, 5, 75, 8, 2, 5, tak
|
||||||
|
58, 26, 6, 3, 70, 14, 3, 6, nie
|
||||||
|
35, 21, 0, 2, 60, 17, 4, 7, nie
|
||||||
|
65, 18, 3, 4, 85, 11, 1, 8, nie
|
||||||
|
45, 20, 1, 3, 65, 13, 2, 9, tak
|
||||||
|
50, 22, 7, 5, 75, 9, 3, 10, tak
|
||||||
|
31, 25, 0, 2, 70, 14, 1, 1, tak
|
||||||
|
60, 18, 9, 3, 60, 8, 2, 2, nie
|
||||||
|
34, 20, 3, 4, 50, 12, 3, 3, tak
|
||||||
|
52, 23, 1, 5, 65, 16, 4, 4, tak
|
||||||
|
47, 24, 6, 6, 80, 10, 1, 5, tak
|
||||||
|
55, 22, 2, 3, 75, 13, 2, 6, nie
|
||||||
|
38, 26, 8, 1, 70, 15, 3, 7, nie
|
||||||
|
64, 21, 0, 4, 85, 9, 4, 8, tak
|
||||||
|
41, 20, 2, 3, 65, 13, 1, 9, tak
|
||||||
|
49, 22, 8, 5, 75, 17, 2, 10, tak
|
||||||
|
30, 24, 1, 2, 70, 14, 3, 1, tak
|
||||||
|
61, 18, 10, 3, 60, 8, 1, 2, nie
|
||||||
|
37, 20, 4, 4, 50, 12, 2, 3, tak
|
||||||
|
51, 23, 2, 5, 65, 16, 4, 4, tak
|
||||||
|
44, 24, 5, 6, 80, 10, 3, 5, tak
|
||||||
|
53, 22, 3, 3, 75, 13, 1, 6, nie
|
||||||
|
39, 26, 7, 1, 70, 15, 2, 7, nie
|
||||||
|
65, 21, 0, 4, 85, 9, 3, 8, tak
|
||||||
|
48, 25, 6, 5, 60, 11, 4, 9, nie
|
||||||
|
57, 20, 3, 3, 75, 14, 1, 10, tak
|
||||||
|
32, 24, 9, 2, 65, 10, 2, 1, tak
|
||||||
|
62, 19, 1, 2, 55, 15, 3, 2, nie
|
||||||
|
36, 21, 2, 1, 70, 12, 4, 3, nie
|
||||||
|
55, 23, 3, 4, 80, 16, 1, 4, tak
|
||||||
|
42, 23, 1, 5, 75, 8, 2, 5, tak
|
||||||
|
58, 26, 5, 3, 70, 14, 3, 6, nie
|
||||||
|
35, 21, 0, 2, 60, 17, 4, 7, nie
|
||||||
|
46, 20, 2, 3, 65, 13, 2, 9, tak
|
||||||
|
50, 22, 6, 5, 75, 9, 3, 10, tak
|
||||||
|
31, 25, 1, 2, 70, 14, 1, 1, tak
|
||||||
|
60, 18, 8, 3, 60, 8, 2, 2, nie
|
||||||
|
33, 20, 4, 4, 50, 12, 3, 3, tak
|
||||||
|
52, 23, 1, 5, 65, 16, 4, 4, tak
|
||||||
|
47, 24, 7, 6, 80, 10, 1, 5, tak
|
||||||
|
38, 25, 2, 3, 65, 10, 1, 1, tak
|
||||||
|
59, 21, 1, 2, 55, 15, 2, 2, nie
|
||||||
|
33, 23, 3, 1, 70, 9, 3, 3, nie
|
||||||
|
47, 22, 7, 4, 75, 11, 2, 1, tak
|
||||||
|
58, 20, 4, 3, 60, 16, 1, 2, nie
|
||||||
|
32, 24, 6, 2, 70, 8, 3, 3, nie
|
||||||
|
53, 23, 3, 5, 80, 15, 4, 4, tak
|
||||||
|
45, 21, 8, 6, 75, 12, 1, 5, tak
|
||||||
|
55, 26, 11, 4, 70, 17, 2, 6, nie
|
||||||
|
36, 24, 5, 3, 60, 9, 3, 7, nie
|
||||||
|
67, 19, 10, 4, 85, 13, 4, 8, nie
|
||||||
|
43, 22, 1, 3, 65, 14, 1, 9, tak
|
||||||
|
51, 24, 9, 5, 75, 10, 2, 10, tak
|
||||||
|
31, 27, 2, 2, 70, 15, 3, 1, tak
|
||||||
|
62, 18, 12, 3, 60, 8, 1, 2, nie
|
||||||
|
38, 21, 6, 4, 50, 11, 2, 3, tak
|
||||||
|
49, 24, 4, 5, 65, 16, 4, 4, tak
|
||||||
|
42, 25, 7, 3, 80, 9, 3, 5, tak
|
||||||
|
57, 23, 4, 3, 75, 12, 1, 6, nie
|
||||||
|
35, 28, 9, 1, 70, 16, 2, 7, nie
|
||||||
|
76, 20, 11, 4, 85, 10, 3, 8, nie
|
||||||
|
46, 22, 0, 3, 65, 13, 1, 9, tak
|
||||||
|
10, 25, 10, 5, 95, 9, 2, 10, tak
|
||||||
|
38, 19, 3, 6, 80, 11, 2, 1, tak
|
||||||
|
57, 24, 2, 7, 90, 15, 1, 2, tak
|
||||||
|
81, 18, 9, 8, 70, 12, 4, 3, nie
|
||||||
|
49, 22, 2, 9, 85, 18, 3, 4, tak
|
||||||
|
44, 23, 1, 3, 60, 8, 2, 5, nie
|
||||||
|
24, 26, 5, 2, 75, 14, 3, 6, tak
|
||||||
|
76, 13, 0, 4, 25, 19, 4, 7, nie
|
||||||
|
67, 15, 5, 2, 80, 11, 1, 8, nie
|
||||||
|
43, 20, 1, 1, 65, 13, 2, 9, tak
|
||||||
|
50, 22, 8, 1, 20, 19, 3, 10, nie
|
||||||
|
62, 25, 0, 2, 75, 14, 2, 1, tak
|
||||||
|
58, 18, 11, 3, 30, 8, 3, 2, nie
|
||||||
|
37, 20, 2, 2, 50, 12, 4, 3, tak
|
||||||
|
21, 23, 0, 1, 65, 20, 1, 4, tak
|
||||||
|
46, 24, 2, 2, 75, 12, 2, 5, tak
|
||||||
|
76, 22, 6, 2, 70, 13, 3, 6, nie
|
||||||
|
39, 26, 9, 2, 15, 16, 1, 7, nie
|
||||||
|
53, 21, 2, 3, 80, 12, 2, 8, tak
|
||||||
|
48, 25, 7, 4, 10, 11, 3, 9, nie
|
||||||
|
88, 19, 3, 6, 30, 11, 2, 1, nie
|
||||||
|
57, 24, 4, 7, 10, 15, 1, 2, nie
|
||||||
|
11, 18, 9, 8, 90, 12, 4, 3, tak
|
||||||
|
49, 22, 2, 9, 85, 18, 3, 4, tak
|
||||||
|
44, 23, 1, 1, 60, 8, 2, 5, tak
|
||||||
|
54, 26, 7, 2, 75, 14, 3, 6, nie
|
||||||
|
36, 21, 0, 2, 55, 16, 4, 7, tak
|
||||||
|
27, 24, 2, 3, 80, 11, 1, 8, tak
|
||||||
|
13, 20, 6, 4, 95, 10, 2, 9, tak
|
||||||
|
50, 22, 8, 5, 20, 17, 3, 10, nie
|
||||||
|
32, 25, 1, 6, 95, 14, 2, 1, tak
|
||||||
|
58, 18, 11, 3, 80, 8, 3, 2, nie
|
||||||
|
37, 20, 2, 1, 50, 12, 4, 3, tak
|
||||||
|
51, 23, 5, 2, 25, 22, 1, 4, nie
|
||||||
|
46, 24, 0, 2, 75, 10, 2, 5, tak
|
||||||
|
56, 22, 1, 3, 70, 13, 3, 6, tak
|
||||||
|
39, 26, 9, 4, 35, 20, 1, 7, nie
|
||||||
|
63, 21, 2, 3, 90, 9, 2, 8, tak
|
||||||
|
48, 25, 7, 1, 70, 11, 3, 9, nie
|
||||||
|
42, 24, 1, 5, 75, 14, 2, 1, tak
|
||||||
|
55, 20, 1, 8, 65, 13, 1, 2, tak
|
||||||
|
58, 22, 6, 4, 10, 12, 3, 3, nie
|
||||||
|
47, 21, 2, 2, 70, 9, 4, 4, tak
|
||||||
|
50, 25, 1, 1, 85, 15, 1, 5, nie
|
||||||
|
60, 23, 7, 7, 70, 11, 2, 6, nie
|
||||||
|
83, 19, 4, 3, 60, 13, 3, 7, nie
|
||||||
|
68, 26, 8, 5, 90, 16, 4, 8, nie
|
||||||
|
41, 18, 9, 1, 75, 14, 2, 9, nie
|
||||||
|
48, 20, 0, 8, 70, 8, 1, 10, tak
|
||||||
|
89, 23, 2, 6, 55, 12, 3, 1, nie
|
||||||
|
59, 21, 2, 7, 80, 10, 4, 2, tak
|
||||||
|
97, 24, 3, 5, 65, 9, 1, 3, nie
|
||||||
|
49, 22, 2, 9, 75, 15, 2, 4, tak
|
||||||
|
92, 26, 1, 1, 85, 11, 3, 5, nie
|
||||||
|
21, 18, 4, 8, 70, 13, 4, 6, tak
|
||||||
|
34, 25, 9, 4, 60, 16, 1, 7, nie
|
||||||
|
70, 19, 8, 5, 90, 14, 2, 8, nie
|
||||||
|
45, 20, 2, 2, 80, 12, 3, 9, tak
|
||||||
|
53, 24, 7, 7, 75, 10, 1, 10, nie
|
||||||
|
43, 21, 3, 6, 70, 11, 2, 1, tak
|
||||||
|
56, 22, 8, 8, 80, 13, 3, 2, nie
|
||||||
|
39, 19, 5, 7, 65, 9, 4, 3, nie
|
||||||
|
48, 23, 1, 9, 75, 14, 1, 4, tak
|
||||||
|
51, 25, 6, 1, 85, 12, 2, 5, nie
|
||||||
|
62, 24, 4, 2, 90, 15, 3, 6, nie
|
||||||
|
35, 20, 2, 2, 60, 10, 4, 7, tak
|
||||||
|
69, 26, 7, 3, 85, 16, 1, 8, nie
|
||||||
|
40, 18, 9, 1, 70, 12, 2, 9, nie
|
||||||
|
49, 22, 0, 5, 75, 11, 3, 10, tak
|
||||||
|
54, 21, 3, 7, 80, 14, 4, 1, tak
|
||||||
|
63, 23, 8, 8, 90, 12, 1, 2, nie
|
||||||
|
36, 20, 2, 9, 65, 10, 2, 3, tak
|
||||||
|
47, 24, 1, 1, 75, 13, 3, 4, tak
|
||||||
|
50, 26, 5, 2, 85, 15, 4, 5, nie
|
||||||
|
34, 25, 2, 2, 90, 9, 1, 6, tak
|
||||||
|
47, 19, 6, 3, 60, 11, 2, 7, nie
|
||||||
|
70, 25, 9, 2, 85, 17, 3, 8, nie
|
||||||
|
41, 21, 7, 1, 70, 13, 4, 9, nie
|
||||||
|
52, 20, 1, 1, 75, 10, 1, 10, tak
|
||||||
|
38, 22, 3, 6, 70, 12, 2, 1, tak
|
||||||
|
87, 23, 9, 7, 80, 14, 3, 2, nie
|
||||||
|
61, 20, 5, 2, 65, 10, 4, 3, nie
|
||||||
|
49, 21, 0, 9, 75, 11, 1, 4, tak
|
||||||
|
44, 25, 6, 1, 80, 15, 2, 5, nie
|
||||||
|
54, 19, 2, 1, 90, 13, 3, 6, tak
|
||||||
|
36, 24, 8, 1, 60, 9, 4, 7, nie
|
||||||
|
67, 20, 3, 2, 85, 16, 1, 8, nie
|
||||||
|
43, 22, 7, 4, 75, 12, 2, 9, nie
|
||||||
|
50, 23, 1, 5, 80, 10, 3, 10, tak
|
||||||
|
55, 21, 4, 7, 70, 13, 4, 1, nie
|
||||||
|
63, 25, 9, 8, 90, 14, 1, 2, nie
|
||||||
|
57, 19, 5, 9, 65, 11, 2, 3, nie
|
||||||
|
49, 24, 0, 1, 75, 16, 3, 4, tak
|
||||||
|
51, 20, 6, 1, 85, 12, 4, 5, nie
|
||||||
|
64, 22, 4, 2, 90, 9, 1, 6, nie
|
||||||
|
39, 23, 8, 3, 60, 14, 2, 7, tak
|
||||||
|
70, 21, 2, 1, 85, 13, 3, 8, nie
|
||||||
|
41, 25, 7, 5, 70, 15, 4, 9, nie
|
||||||
|
52, 19, 1, 2, 75, 10, 1, 10, tak
|
|
44
decisiontree.py
Normal file
44
decisiontree.py
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
from sklearn.model_selection import train_test_split
|
||||||
|
from sklearn.tree import DecisionTreeClassifier, plot_tree
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# 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
|
||||||
|
#salata = 4
|
||||||
|
#cebula = 5
|
||||||
|
#Papryka = 6
|
||||||
|
#Buraki = 7
|
||||||
|
#Bruksela = 8
|
||||||
|
#Rzepak = 9
|
||||||
|
#Szpinak = 10
|
22
main.py
22
main.py
@ -5,8 +5,6 @@ from tractor import Tractor
|
|||||||
from kolejka import Stan, Kolejka, Odwiedzone
|
from kolejka import Stan, Kolejka, Odwiedzone
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
fps = 5
|
fps = 5
|
||||||
WIN = pygame.display.set_mode((width, height))
|
WIN = pygame.display.set_mode((width, height))
|
||||||
|
|
||||||
@ -24,8 +22,8 @@ def actions(elem, istate):
|
|||||||
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)
|
akcje.append(elem.a)
|
||||||
elem = elem.p[0]
|
elem = elem.p[0]
|
||||||
|
|
||||||
return akcje
|
return akcje
|
||||||
|
|
||||||
def graphsearch(istate, goaltest, board):
|
def graphsearch(istate, goaltest, board):
|
||||||
explored = Odwiedzone()
|
explored = Odwiedzone()
|
||||||
fringe = Kolejka()
|
fringe = Kolejka()
|
||||||
@ -46,14 +44,14 @@ def graphsearch(istate, goaltest, board):
|
|||||||
def main():
|
def main():
|
||||||
rotation = ["left", "up", "right", "down"]
|
rotation = ["left", "up", "right", "down"]
|
||||||
istate = Stan(4,4, "down")
|
istate = Stan(4,4, "down")
|
||||||
goaltest = Stan(1,1, "up")
|
goaltest = Stan(2,3, "up")
|
||||||
run = True
|
run = True
|
||||||
clock = pygame.time.Clock()
|
clock = pygame.time.Clock()
|
||||||
board = Board()
|
board = Board()
|
||||||
board.load_images()
|
board.load_images()
|
||||||
actions = graphsearch(istate, goaltest, board)
|
actions = graphsearch(istate, goaltest, board)
|
||||||
print("akcje: >",actions )
|
print("akcje: >",actions )
|
||||||
tractor = Tractor(4, 4)
|
tractor = Tractor(2, 3)
|
||||||
while run:
|
while run:
|
||||||
clock.tick(fps)
|
clock.tick(fps)
|
||||||
|
|
||||||
@ -64,6 +62,7 @@ def main():
|
|||||||
keys = pygame.key.get_pressed()
|
keys = pygame.key.get_pressed()
|
||||||
|
|
||||||
if keys[pygame.K_UP]:
|
if keys[pygame.K_UP]:
|
||||||
|
if keys[pygame.K_UP]:
|
||||||
if(tractor.direction == "up" and tractor.row > 0 ):
|
if(tractor.direction == "up" and tractor.row > 0 ):
|
||||||
if board.is_weed(tractor.col, tractor.row - 1):
|
if board.is_weed(tractor.col, tractor.row - 1):
|
||||||
board.set_grass(tractor.col, tractor.row - 1)
|
board.set_grass(tractor.col, tractor.row - 1)
|
||||||
@ -71,8 +70,12 @@ def main():
|
|||||||
elif board.is_dirt(tractor.col, tractor.row - 1):
|
elif board.is_dirt(tractor.col, tractor.row - 1):
|
||||||
board.set_soil(tractor.col, tractor.row - 1)
|
board.set_soil(tractor.col, tractor.row - 1)
|
||||||
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):
|
elif not board.is_rock(tractor.col, tractor.row - 1):
|
||||||
tractor.row -= 1
|
tractor.row -= 1
|
||||||
|
|
||||||
if(tractor.direction == "left" and tractor.col > 0):
|
if(tractor.direction == "left" and tractor.col > 0):
|
||||||
if board.is_weed(tractor.col - 1, tractor.row):
|
if board.is_weed(tractor.col - 1, tractor.row):
|
||||||
board.set_grass(tractor.col - 1, tractor.row)
|
board.set_grass(tractor.col - 1, tractor.row)
|
||||||
@ -80,6 +83,9 @@ def main():
|
|||||||
elif board.is_dirt(tractor.col - 1, tractor.row):
|
elif board.is_dirt(tractor.col - 1, tractor.row):
|
||||||
board.set_soil(tractor.col - 1, tractor.row)
|
board.set_soil(tractor.col - 1, tractor.row)
|
||||||
tractor.col -= 1
|
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):
|
elif not board.is_rock(tractor.col - 1, tractor.row):
|
||||||
tractor.col -= 1
|
tractor.col -= 1
|
||||||
if(tractor.direction == "down" and tractor.row < rows - 1):
|
if(tractor.direction == "down" and tractor.row < rows - 1):
|
||||||
@ -89,6 +95,9 @@ def main():
|
|||||||
elif board.is_dirt(tractor.col, tractor.row + 1):
|
elif board.is_dirt(tractor.col, tractor.row + 1):
|
||||||
board.set_soil(tractor.col, tractor.row + 1)
|
board.set_soil(tractor.col, tractor.row + 1)
|
||||||
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):
|
elif not board.is_rock(tractor.col, tractor.row + 1):
|
||||||
tractor.row += 1
|
tractor.row += 1
|
||||||
if(tractor.direction == "right" and tractor.col < cols - 1):
|
if(tractor.direction == "right" and tractor.col < cols - 1):
|
||||||
@ -98,6 +107,9 @@ def main():
|
|||||||
elif board.is_dirt(tractor.col + 1, tractor.row):
|
elif board.is_dirt(tractor.col + 1, tractor.row):
|
||||||
board.set_soil(tractor.col + 1, tractor.row)
|
board.set_soil(tractor.col + 1, tractor.row)
|
||||||
tractor.col += 1
|
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):
|
elif not board.is_rock(tractor.col + 1, tractor.row):
|
||||||
tractor.col += 1
|
tractor.col += 1
|
||||||
if keys[pygame.K_LEFT]:
|
if keys[pygame.K_LEFT]:
|
||||||
|
Loading…
Reference in New Issue
Block a user