Sztuczna_Inteligencja_2020/main.py

273 lines
9.5 KiB
Python

import secrets
import sys
from random import randrange
from src.decisionTree import *
from src.plate import *
from src.SubprojectMaksymilianKierski.PlateRecognition import use_model_to_predict, text_speech
# Marcin Dobrowolski
from src.SubprojectMarcinDobrowolski.suggestionDecisionTree import *
from src.SubprojectMarcinDobrowolski.utility import generateTestData, generateTestExample
from src.guest import *
if __name__ == "__main__":
# SETUP
pygame.init()
clock = pygame.time.Clock()
graphics = Graphics()
waiter = Waiter(graphics)
tree = DecisionTree()
# Init functions
graphics.drawBackground(waiter.matrix)
graphics.update(waiter)
# AStar
goal = None
path = ''
# Dominik
queue = []
fromBar = 0
barNode = (3, 12)
node = ()
goalList = [0, 0]
# Marcin Dobrowolski
suggestionTreeRoot = SuggestionTree.buildTree(trainingData)
newGuests = []
actions = []
# Maksymilian
go = 0
randomPlate = None
plate = 0
changePlateVar = 0
waitPos = [[1, 2], [1, 5], [1, 8], [5, 4], [5, 8],
[8, 2], [8, 5], [8, 8], [12, 3], [12, 7]]
tabPos = [[1, 2], [1, 5], [1, 8], [5, 4], [5, 8],
[8, 2], [8, 5], [8, 8], [12, 3], [12, 7]]
pltPos = [[2, 3], [2, 6], [2, 9], [4, 4], [4, 8],
[9, 3], [9, 6], [9, 9], [11, 4], [11, 8]]
plateArr = []
def changePlate():
if plateArr and len(plateArr) > 0:
if randrange(30) == 17:
plate = random.choice(plateArr)
if not plate.empty:
randomPlate = randrange(4) + 11
plate.changePlate('plate-empty.png', 'test-{}'.format(randomPlate))
def addGuest():
if randrange(10) == 5 and path == '':
newGuests.append(Guest(graphics))
guest = newGuests[len(newGuests) - 1]
goal = [0, 0]
goal[0] = guest.cord[0]
goal[1] = guest.cord[1]
tree.TasksList('order', goal, [waiter.X, waiter.Y])
tree.print()
print(actions)
return tree.ReturnQueueList()
while True:
changePlate()
# queue = addGuest()
for event in pygame.event.get():
# rabbit.check(waiter.matrix, waiter.X, waiter.Y)
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
break
if event.key == pygame.K_s:
waitPosition = [waiter.X, waiter.Y]
tree.TasksList('check', [2, 3], waitPosition)
tree.TasksList('eat', [9, 6], waitPosition)
tree.TasksList('order', [4, 8], waitPosition)
tree.TasksList('goToBar', [11, 4], waitPosition)
tree.TasksList('check', [2, 9], waitPosition)
tree.TasksList('eat', [1, 1], waitPosition)
queue = tree.ReturnQueueList()
tree.print()
# Marcin Dobrowolski
if event.key == pygame.K_0:
newGuests.append(Guest(graphics))
# Marcin Dobrowolski
if event.key == pygame.K_1:
guest = newGuests[0]
goal = [0, 0]
if guest.cord[0] == 2 or guest.cord[1] == 9:
goal[0] = guest.cord[0] - 1
goal[1] = guest.cord[1]
else:
goal[0] = guest.cord[0] + 1
goal[1] = guest.cord[1]
actions.append(('takeOrder', goal))
print(actions)
# AStar
if event.key == pygame.K_r:
temp = False
while not temp:
x = secrets.randbelow(graphics.width)
y = secrets.randbelow(graphics.height)
print(x, y)
if waiter.matrix.matrix[x][y].walk_through == 1:
temp = True
goal = (x, y)
path = waiter.findPath(goal)
path = waiter.translatePath(path)
if event.key == pygame.K_a:
if path == '':
newGuests.append(Guest(graphics))
guest = newGuests[len(newGuests) - 1]
goalList[0] = guest.cord[0]
goalList[1] = guest.cord[1]
tree.TasksList('order', goalList, [waiter.X, waiter.Y])
tree.TasksList('goToBar', goalList, [waiter.X, waiter.Y])
queue = tree.ReturnQueueList()
if plateArr and event.key == pygame.K_m:
randomPlate = random.choice(plateArr)
if (not [waiter.X, waiter.Y] == randomPlate.table) or go == 0:
print(waiter.X, waiter.Y)
print(randomPlate.table)
print(go)
print([waiter.X, waiter.Y] in randomPlate.table)
if [waiter.X, waiter.Y] in waitPos:
model = 'waiter_' + waiter.direction
for x in range(-1, 2):
waiterX = waiter.X + (x * 0.1)
graphics.clear(waiterX, waiter.Y - 1)
print(randomPlate)
changePlateVar = 0
# if this plate is exists
goal = (randomPlate.table[0], randomPlate.table[1])
path = waiter.findPath(goal)
path = waiter.translatePath(path)
print('sec-1')
go = 1
plate = 0
else:
if randomPlate.checked:
predict = 'CHECKED'
else:
predict = use_model_to_predict(randomPlate.pictureAI)
if predict == 1:
predict = 'EMPTY'
randomPlate.clearTable()
plateArr.remove(randomPlate)
tree.TasksList('goToBar', barNode, [waiter.X, waiter.Y])
queue = tree.ReturnQueueList()
changePlateVar = 1
else:
predict = 'FOOD'
print('sec-3')
text_speech('arialnarrow.ttf', 25, predict, (255, 255, 255), (0, 128, 0),
(waiter.X * 50 + 25), (waiter.Y * 50 - 25), False, False, screen=graphics.screen)
pygame.display.flip()
go = 0
plate = 0
if plate == 1 and (goalList == [waiter.X - 1, waiter.Y] or goalList == [waiter.X + 1, waiter.Y]):
randTable = goalList
randIndex = pltPos.index(goalList)
tableCord = tabPos[randIndex]
plateCord = pltPos[randIndex]
print('rand{}'.format(randIndex))
pictureAI = 'test-{}'.format(randIndex)
plateArr.append(Plate(graphics, plateCord, tableCord, 'plate-full.png', pictureAI))
tabPos.pop(randIndex)
pltPos.pop(randIndex)
print(plateArr, pltPos)
goal = (plateArr[len(plateArr) - 1].table[0], plateArr[len(plateArr) - 1].table[1])
randGo = len(plateArr) - 1
path = waiter.findPath(goal)
path = waiter.translatePath(path)
print('sec-2')
go = 1
plate = 0
# Dominik
if queue and path == '' and fromBar == 0:
print(queue)
task = queue.pop(0)
print('tasks.{}'.format(task))
tree.RemoveTask()
node = (task[2][0], task[2][1])
if task[0] == "goToBar":
path = waiter.findPath(barNode)
fromBar = 1
else:
changePlateVar = 0
path = waiter.findPath(node)
path = waiter.translatePath(path)
if path == '' and fromBar == 1 and changePlateVar == 0:
path = waiter.findPath(node)
path = waiter.translatePath(path)
fromBar = 0
plate = 1
if path == '' and fromBar == 1:
path = waiter.findPath(node)
path = waiter.translatePath(path)
fromBar = 0
print(plate)
# AStar
if path == '' and actions:
print('Goal: {}'.format(actions[0][1]))
path = waiter.findPath(actions[0][1])
print('Path: {}'.format(path))
path = waiter.translatePath(path)
print('Translated path: {}'.format(path))
if path != '':
nextStep = path[0]
path = path[1:]
waiter.travel(nextStep, graphics)
if path == '':
print('')
# action = actions.pop(0)
# if action[0] == 'takeOrder':
# guest = newGuests.pop(0)
# waiter.takeOrder(suggestionTreeRoot, guest)
# graphics.clearGuest(guest)
# tables.append(guest.cord)
pygame.display.flip()
clock.tick(graphics.fps)