2021-06-23 00:09:58 +02:00
|
|
|
|
|
|
|
from keras.models import Sequential, load_model
|
|
|
|
from keras.layers import Dense, Dropout, Flatten
|
|
|
|
from keras.layers import Conv2D, MaxPooling2D
|
|
|
|
from keras.layers.normalization import BatchNormalization
|
|
|
|
from PIL import Image
|
|
|
|
from random import shuffle, choice
|
|
|
|
import numpy as np
|
|
|
|
import os
|
|
|
|
|
2021-06-23 11:09:17 +02:00
|
|
|
import tree
|
2021-06-13 15:14:55 +02:00
|
|
|
import pygame
|
2021-06-15 19:48:34 +02:00
|
|
|
from time import sleep
|
2021-06-23 11:09:17 +02:00
|
|
|
from os import path
|
2021-06-13 15:14:55 +02:00
|
|
|
from colors import gray
|
|
|
|
from house import create_houses
|
|
|
|
from truck import Truck
|
|
|
|
from trash import Trash
|
2021-06-16 02:04:55 +02:00
|
|
|
from TSP import tsp, tspmove
|
2021-06-23 11:09:17 +02:00
|
|
|
from bfs import bfs, distance
|
2021-06-23 11:40:06 +02:00
|
|
|
from pool import create_pools
|
|
|
|
from granny import create_granny
|
2021-06-23 00:09:58 +02:00
|
|
|
|
|
|
|
model = load_model("model.h5")
|
|
|
|
|
2021-06-16 02:04:55 +02:00
|
|
|
multi_trash = []
|
2021-06-13 15:14:55 +02:00
|
|
|
pygame.init()
|
|
|
|
|
|
|
|
|
2021-06-15 20:24:06 +02:00
|
|
|
def game_keys(truck, multi_trash, houses, auto=False):
|
2021-06-15 19:48:34 +02:00
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.KEYDOWN:
|
|
|
|
if auto:
|
2021-06-23 00:09:58 +02:00
|
|
|
sleep(.01)
|
2021-06-15 19:48:34 +02:00
|
|
|
if (event.key == pygame.K_w or event.key == pygame.K_UP):
|
|
|
|
if truck.test_crash(houses):
|
|
|
|
break
|
|
|
|
truck.move()
|
|
|
|
print('↑')
|
2021-06-15 20:24:06 +02:00
|
|
|
for tindex, trash in enumerate(multi_trash):
|
|
|
|
if truck.pos == trash.pos:
|
2021-06-23 11:09:17 +02:00
|
|
|
truck.mass += trash.mass
|
|
|
|
truck.space += trash.space
|
|
|
|
print(truck.mass, truck.space)
|
2021-06-23 00:09:58 +02:00
|
|
|
prediction = model.predict(multi_trash[tindex].content)
|
|
|
|
for i in range (3):
|
|
|
|
if multi_trash[tindex].names[i][:3] == 'cat':
|
|
|
|
truck.allCats = truck.allCats + 1
|
|
|
|
else:
|
|
|
|
truck.allTrash = truck.allTrash + 1
|
|
|
|
if prediction[i][0] > prediction[i][1]:
|
|
|
|
truck.container.append(multi_trash[tindex].names[i])
|
|
|
|
if multi_trash[tindex].names[i][:3] == 'cat':
|
|
|
|
truck.cats = truck.cats + 1
|
|
|
|
else:
|
|
|
|
truck.trash = truck.trash + 1
|
|
|
|
print(f'cats in truck: {truck.cats} cats encountered: {truck.allCats}')
|
|
|
|
print(f'trash in truck: {truck.trash} trash encountered: {truck.allTrash}')
|
|
|
|
print(truck.container)
|
2021-06-15 20:24:06 +02:00
|
|
|
multi_trash.pop(tindex)
|
2021-06-15 19:48:34 +02:00
|
|
|
break
|
|
|
|
elif (event.key == pygame.K_a or event.key == pygame.K_LEFT):
|
|
|
|
print('←')
|
|
|
|
truck.rotate(-1)
|
|
|
|
truck.rotate_image(90)
|
|
|
|
break
|
|
|
|
elif (event.key == pygame.K_d or event.key == pygame.K_RIGHT):
|
|
|
|
print('→')
|
|
|
|
truck.rotate(1)
|
|
|
|
truck.rotate_image(-90)
|
|
|
|
break
|
|
|
|
|
|
|
|
|
2021-06-23 11:40:06 +02:00
|
|
|
def update_images(gameDisplay, truck, multi_trash, houses,pools,grannies):
|
2021-06-13 15:14:55 +02:00
|
|
|
gameDisplay.fill(gray)
|
|
|
|
for house in houses:
|
|
|
|
gameDisplay.blit(pygame.image.load('./img/house.png'), house.pos)
|
2021-06-23 11:40:06 +02:00
|
|
|
for pool in pools:
|
|
|
|
gameDisplay.blit(pygame.image.load('./img/wet.png'), pool.pos)
|
|
|
|
for granny in grannies:
|
|
|
|
gameDisplay.blit(pygame.image.load('./img/granny.png'), granny.pos)
|
2021-06-15 20:24:06 +02:00
|
|
|
for trash in multi_trash:
|
|
|
|
gameDisplay.blit(pygame.image.load('./img/trash.png'), trash.pos)
|
2021-06-13 15:14:55 +02:00
|
|
|
gameDisplay.blit(truck.image, truck.pos)
|
|
|
|
pygame.display.update()
|
|
|
|
|
|
|
|
|
|
|
|
def game_loop():
|
|
|
|
game_w = 1280 # 32
|
|
|
|
game_h = 640 # 16
|
|
|
|
grid_size = 40
|
|
|
|
|
|
|
|
gameDisplay = pygame.display.set_mode((game_w, game_h))
|
|
|
|
pygame.display.set_caption('Garbage truck')
|
|
|
|
clock = pygame.time.Clock()
|
|
|
|
|
|
|
|
gameExit = False
|
|
|
|
|
|
|
|
truck = Truck(game_w, game_h, grid_size)
|
|
|
|
houses = create_houses(grid_size)
|
2021-06-23 11:40:06 +02:00
|
|
|
pools = create_pools(grid_size)
|
|
|
|
grannies = create_granny(grid_size)
|
2021-06-13 15:14:55 +02:00
|
|
|
|
2021-06-15 20:24:06 +02:00
|
|
|
for _ in range(7):
|
|
|
|
trash = Trash(game_w, game_h, grid_size)
|
|
|
|
trash.new_pos(truck.pos, houses, multi_trash)
|
|
|
|
multi_trash.append(trash)
|
2021-06-23 00:09:58 +02:00
|
|
|
|
|
|
|
model.predict(multi_trash[0].content)
|
2021-06-13 15:14:55 +02:00
|
|
|
while not gameExit:
|
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.QUIT:
|
|
|
|
pygame.quit()
|
|
|
|
quit()
|
|
|
|
if event.type == pygame.KEYDOWN:
|
|
|
|
if event.key == pygame.K_ESCAPE:
|
|
|
|
pygame.quit()
|
|
|
|
quit()
|
2021-06-23 11:09:17 +02:00
|
|
|
if (event.key == pygame.K_l):
|
|
|
|
if path.isfile('./tree_model') and not os.stat(
|
|
|
|
'./tree_model').st_size == 0:
|
|
|
|
decision_tree = tree.load_tree_from_structure('./tree_model')
|
|
|
|
print("Tree model loaded!")
|
|
|
|
|
|
|
|
if (event.key == pygame.K_k):
|
|
|
|
print(":>")
|
|
|
|
trash = multi_trash[0]
|
2021-06-23 11:40:06 +02:00
|
|
|
dis = [120,120]
|
|
|
|
print(truck.pos)
|
|
|
|
dis_dump = distance(dis,truck.pos)
|
2021-06-23 11:09:17 +02:00
|
|
|
dis_trash = distance(truck.pos, trash.pos)
|
|
|
|
print(dis_dump, dis_trash, truck.mass, truck.space, trash.mass, trash.space)
|
|
|
|
decision = tree.making_decision(decision_tree,
|
|
|
|
dis_dump // 12 + 1,
|
|
|
|
dis_trash // 12 + 1,
|
|
|
|
truck.mass // 20 + 1, truck.space // 20 + 1,
|
|
|
|
trash.mass // 20 + 1,
|
|
|
|
trash.space // 20 + 1)
|
|
|
|
|
|
|
|
print(decision)
|
|
|
|
if(decision[0]==0):
|
|
|
|
actions = bfs(truck.pos, truck.dir_control,
|
|
|
|
trash.pos, houses)
|
|
|
|
print(actions)
|
|
|
|
if not actions:
|
|
|
|
print('Path couldn\'t be found')
|
|
|
|
break
|
|
|
|
|
|
|
|
print('##################################################')
|
|
|
|
while actions:
|
|
|
|
action = actions.pop()
|
|
|
|
pygame.event.post(pygame.event.Event(
|
|
|
|
pygame.KEYDOWN, {'key': action}))
|
|
|
|
|
|
|
|
game_keys(truck, multi_trash, houses, True)
|
2021-06-23 11:40:06 +02:00
|
|
|
update_images(gameDisplay, truck, multi_trash, houses,pools,grannies)
|
2021-06-23 11:09:17 +02:00
|
|
|
else:
|
|
|
|
truck.space=0
|
|
|
|
truck.mass=0
|
|
|
|
|
2021-06-13 15:14:55 +02:00
|
|
|
if (event.key == pygame.K_b):
|
2021-06-15 20:24:06 +02:00
|
|
|
trash = multi_trash[0]
|
2021-06-15 19:48:34 +02:00
|
|
|
actions = bfs(truck.pos, truck.dir_control,
|
|
|
|
trash.pos, houses)
|
2021-06-23 00:09:58 +02:00
|
|
|
print(actions)
|
2021-06-15 19:48:34 +02:00
|
|
|
if not actions:
|
|
|
|
print('Path couldn\'t be found')
|
2021-06-13 15:14:55 +02:00
|
|
|
break
|
|
|
|
|
2021-06-15 19:48:34 +02:00
|
|
|
print('##################################################')
|
|
|
|
while actions:
|
|
|
|
action = actions.pop()
|
|
|
|
pygame.event.post(pygame.event.Event(
|
|
|
|
pygame.KEYDOWN, {'key': action}))
|
2021-06-15 19:59:23 +02:00
|
|
|
|
2021-06-15 20:24:06 +02:00
|
|
|
game_keys(truck, multi_trash, houses, True)
|
2021-06-23 11:40:06 +02:00
|
|
|
update_images(gameDisplay, truck, multi_trash, houses,pools,grannies)
|
2021-06-16 02:04:55 +02:00
|
|
|
if (event.key == pygame.K_t):
|
|
|
|
solution = tsp(multi_trash, truck)
|
|
|
|
actions = tspmove(solution, truck, multi_trash)
|
|
|
|
if not actions:
|
|
|
|
print('Path couldn\'t be found')
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
|
|
for i in actions:
|
|
|
|
arr=i
|
|
|
|
#print(arr)
|
|
|
|
print('##################################################')
|
|
|
|
while arr:
|
|
|
|
action = arr.pop()
|
|
|
|
pygame.event.post(pygame.event.Event(
|
|
|
|
pygame.KEYDOWN, {'key': action}))
|
|
|
|
|
|
|
|
game_keys(truck, multi_trash, houses, True)
|
2021-06-23 11:40:06 +02:00
|
|
|
update_images(gameDisplay, truck, multi_trash, houses,pools,grannies)
|
2021-06-16 11:53:54 +02:00
|
|
|
truck.direction = [1, 0]
|
|
|
|
truck.dir_control = 0
|
|
|
|
truck.image = pygame.image.load('./img/truck.png')
|
|
|
|
if not multi_trash:
|
|
|
|
for _ in range(7):
|
|
|
|
trash = Trash(game_w, game_h, grid_size)
|
|
|
|
trash.new_pos(truck.pos, houses, multi_trash)
|
|
|
|
multi_trash.append(trash)
|
2021-06-15 19:48:34 +02:00
|
|
|
else:
|
|
|
|
pygame.event.post(event)
|
2021-06-15 20:24:06 +02:00
|
|
|
game_keys(truck, multi_trash, houses)
|
2021-06-23 11:40:06 +02:00
|
|
|
update_images(gameDisplay, truck, multi_trash, houses,pools,grannies)
|
2021-06-13 15:14:55 +02:00
|
|
|
clock.tick(60)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
game_loop()
|
|
|
|
pygame.quit()
|