Compare commits

...

3 Commits

Author SHA1 Message Date
Adam Mikolajczak
30878adedd feat: After arriving at selected by user tile, Tractor makes decision by using decision tree.
The decision is printed in terminal.
2024-06-09 11:26:14 +02:00
Adam Mikolajczak
a6f51420e1 feat: tractor goes on the tile clicked by user. 2024-06-09 11:11:29 +02:00
Adam Mikolajczak
b0ac67a105 fix: Slightly changed tractor.py - also checking if branch works 2024-06-09 10:39:48 +02:00
2 changed files with 53 additions and 39 deletions

View File

@ -1,7 +1,7 @@
import pygame
from field import Field
import os
from config import TILE_SIZE, TICK_RATE
from config import TILE_SIZE, TICK_RATE, FINAL_X, FINAL_Y
if __name__ == "__main__":
pygame.init()
@ -13,10 +13,19 @@ if __name__ == "__main__":
field = Field()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos()
print(f"Mouse clicked at: ({x}, {y})")
grid_x = x // TILE_SIZE
grid_y = y // TILE_SIZE
field.tractor.set_new_goal((grid_x, grid_y))
field.tractor.update()
screen.fill(WHITE)

View File

@ -18,30 +18,33 @@ class Tractor(pygame.sprite.Sprite):
def __init__(self, field):
super().__init__
self.field = field
self.water = 50
self.image = pygame.image.load('images/tractor/east.png').convert_alpha()
self.image = pygame.transform.scale(self.image, (TILE_SIZE, TILE_SIZE))
self.rect = self.image.get_rect()
self.direction = STARTING_DIRECTION
# TODO: enable tractor to start on other tile than (0,0)
self.start = (START_X, START_Y)
self.final = (FINAL_X, FINAL_Y)
self.direction = 'east'
self.start = (0, 0)
self.final = (0, 0)
print('destination @', self.final[0], self.final[1])
self.rect.topleft = (self.start[0] * TILE_SIZE, self.start[1] * TILE_SIZE)
self.water = 50
# A-STAR
# came_from, total_cost = self.a_star()
# path = self.reconstruct_path(came_from)
# self.actions = self.recreate_actions(path)
# self.action_index = 0
self.rect.topleft = (self.start[0] * TILE_SIZE, self.start[1] * TILE_SIZE)
self.actions = []
self.action_index = 0
# DECISION TREE:
self.label_encoders = {}
self.load_decision_tree_model()
def set_new_goal(self, goal):
self.start = self.get_coordinates()
self.final = goal
came_from, total_cost = self.a_star()
path = self.reconstruct_path(came_from)
self.actions = self.recreate_actions(path)
self.action_index = 0
print(f"New goal set to: {self.final}")
def load_decision_tree_model(self):
data = pd.read_csv('tree.csv')
@ -94,13 +97,11 @@ class Tractor(pygame.sprite.Sprite):
def draw(self, surface):
surface.blit(self.image, self.rect)
def get_coordinates(self):
x = self.rect.x // TILE_SIZE
y = self.rect.y // TILE_SIZE
return (x,y)
def move(self):
if self.direction == "north" and self.rect.y > 0:
self.rect.y -= TILE_SIZE
@ -162,29 +163,16 @@ class Tractor(pygame.sprite.Sprite):
self.move()
else:
self.move()
def update(self):
# A STAR:
# if self.action_index == len(self.actions):
# return
# action = self.actions[self.action_index]
# match (action):
# case ('move'):
# self.move()
# case ('left'):
# self.rotate('left')
# case ('right'):
# self.rotate('right')
# DECISION TREE:
def decision_tree(self):
action = self.make_decision()
if (self.get_current_tile().type != 'grass' or self.get_current_tile().type == 'water'): action = 'move'
if (self.get_current_tile().type != 'grass' or self.get_current_tile().type == 'water'): action = 'nothing'
self.prev_action = action
print("Decyzja podjęta przez drzewo decyzyjne: ", action)
match (action):
case ('move'):
case ('nothing'):
pass
#self.move_rotating()
case ('harvest'):
@ -241,8 +229,22 @@ class Tractor(pygame.sprite.Sprite):
self.get_current_tile().set_type('szpinak')
case ('plant(ziemniak)'):
self.get_current_tile().set_type('ziemniak')
self.move_2()
#self.action_index += 1
def update(self):
# A STAR:
if self.action_index == len(self.actions):
return
action = self.actions[self.action_index]
match (action):
case ('move'):
self.move()
case ('left'):
self.rotate('left')
case ('right'):
self.rotate('right')
self.action_index += 1
if self.get_current_tile().type == "grass":
print("Co jest faktycznie: trawa")
@ -252,9 +254,13 @@ class Tractor(pygame.sprite.Sprite):
print("Rozpoznano: ", self.get_current_tile().prediction)
print("Co jest faktycznie: ", self.get_current_tile().type)
print("\n")
if self.get_coordinates() == self.final:
self.decision_tree()
return
def log_info(self):
# print on what tile type the tractor is on
x = self.rect.x // TILE_SIZE
@ -367,13 +373,12 @@ class Tractor(pygame.sprite.Sprite):
if current == self.final:
break
# next_node: tuple[int, int]
for next_node in self.neighboring_nodes(coordinates=current):
enter_cost = self.cost_of_entering_node(coordinates=next_node)
new_cost: int = cost_so_far[current] + enter_cost
new_cost = cost_so_far[current] + enter_cost
if next_node not in cost_so_far or new_cost < cost_so_far[next_node]:
cost_so_far[next_node] = new_cost
priority = new_cost + self.manhattan_cost(current)
priority = new_cost + self.manhattan_cost(next_node)
heapq.heappush(fringe, (priority, next_node))
came_from[next_node] = current