wozek-projekt/Program.py

102 lines
3.3 KiB
Python
Raw Normal View History

2023-03-27 13:33:47 +02:00
import pygame
2023-04-16 16:38:04 +02:00
import random
2023-05-05 06:13:03 +02:00
import a_star
2023-04-16 16:38:04 +02:00
2023-03-27 13:33:47 +02:00
from pygame.locals import *
from Forklift import Forklift
2023-05-22 16:08:35 +02:00
import decision_tree
2023-03-27 13:33:47 +02:00
HORIZONTAL = 1250
VERTICAL = 750
TILE_SIZE = 50
2023-05-22 16:08:35 +02:00
c_label = random.randint(0, 3) # 0 - no label, 1 - fragile, 2 - hazardous, 3 - any other
c_size = random.randint(1, 3) # 1 - small, 2 - medium, 3 - large
c_weight = random.randint(1, 3) # -||-
c_urgent = random.randint(0, 1) # yes/no
c_weekend = random.randint(0, 1) # stays for the weekend yes/no
c_payment_method = random.randint(0, 2) # not paid, prepaid, cash on delivery
c_international = random.randint(1, 4) # domestic, european, us, everywhere else
c_delayed = random.randint(0, 1) # yes/no
# sectors:
# 1 - regular,
# 2 - hazardous,
# 3 - international,
# 4 - overdue
tree = decision_tree.treelearn()
decision = decision_tree.make_decision(tree, c_label, c_size, c_weight, c_urgent, c_weekend, c_payment_method, c_international, c_delayed)
def handle_decision():
global row, col
if decision == '1':
row = 10 + 2 * random.randint(0, 2)
col = random.randint(1, 5) + 6 * random.randint(0, 1)
print('regular')
if decision == '2':
row = 10 + 2 * random.randint(0, 2)
col = random.randint(1, 5) + 6 * random.randint(2, 3)
print('hazardous')
if decision == '3':
row = 2 * random.randint(0, 2)
col = random.randint(1, 5) + 6 * random.randint(0, 1)
print('international')
if decision == '4':
row = 2 * random.randint(0, 2)
col = random.randint(1, 5) + 6 * random.randint(2, 3)
print('overdue')
return row, col
2023-05-05 06:13:03 +02:00
2023-04-16 16:38:04 +02:00
def handle_turn(initial_state, turn_count):
if turn_count % 2 == 0:
2023-05-05 06:13:03 +02:00
commands = a_star.a_star(state=initial_state, goal=(7, 23))
2023-04-16 16:38:04 +02:00
else:
2023-05-22 16:08:35 +02:00
# row = random.randint(0, 1) * 10 + 2 * random.randint(0, 2)
# col = random.randint(1, 5) + 6 * random.randint(0, 3)
commands = a_star.a_star(state=initial_state, goal=(handle_decision()))
print('I need to go to row, column: ' + str(handle_decision()))
2023-04-16 16:38:04 +02:00
return commands
2023-03-27 13:33:47 +02:00
class Program:
def __init__(self):
2023-05-22 16:08:35 +02:00
2023-03-27 13:33:47 +02:00
pygame.init()
self.window = pygame.display.set_mode((HORIZONTAL, VERTICAL))
self.caption = pygame.display.set_caption('Autonomiczny wózek widłowy')
self.agent = Forklift(self.window)
self.agent.drawForklift()
running = True
2023-04-16 16:38:04 +02:00
turn_count = 0
2023-05-22 16:08:35 +02:00
initial_state = a_star.State(row=int(self.agent.y / 50), column=int(self.agent.x / 50),
2023-05-05 06:13:03 +02:00
direction=self.agent.direction)
2023-04-16 16:38:04 +02:00
2023-03-27 13:33:47 +02:00
while running:
2023-05-22 16:08:35 +02:00
handle_decision()
2023-04-16 16:38:04 +02:00
commands = handle_turn(initial_state=initial_state, turn_count=turn_count)
while commands:
pygame.event.poll()
step = commands.pop(0)
if step == 'rotate_left':
self.agent.rotate_forklift_left()
elif step == 'rotate_right':
self.agent.rotate_forklift_right()
elif step == 'go':
self.agent.forklift_move()
pygame.time.delay(250)
print(commands)
turn_count += 1
pygame.time.delay(2000)
2023-05-05 06:13:03 +02:00
initial_state.row = int(self.agent.y / 50)
initial_state.column = int(self.agent.x / 50)
initial_state.direction = self.agent.direction