2020-04-28 12:18:53 +02:00
|
|
|
import pygame
|
|
|
|
from os import sys
|
2020-05-19 13:59:22 +02:00
|
|
|
from generate import Generate
|
|
|
|
from floor import Floor
|
|
|
|
from wall import Wall
|
|
|
|
from shelf import Shelf
|
2020-04-28 12:18:53 +02:00
|
|
|
from wheel import Wheel
|
2020-05-19 13:59:22 +02:00
|
|
|
from boxOnTheFloor import BoxOnTheFloor
|
2020-04-28 12:18:53 +02:00
|
|
|
from box import Box
|
|
|
|
from unboxOnTheFloor import UnboxOnTheFloor
|
|
|
|
from AStar import AStar
|
|
|
|
import numpy
|
|
|
|
import easygui
|
|
|
|
from neurons import Neurons
|
|
|
|
from whereDecision import WhereDecision
|
|
|
|
from Evencik import Evencik
|
2020-05-08 22:35:15 +02:00
|
|
|
import pathlib
|
2020-05-19 13:59:22 +02:00
|
|
|
from Data import Data
|
|
|
|
from genetyczne import *
|
2020-04-28 12:18:53 +02:00
|
|
|
|
|
|
|
class MainWindow:
|
|
|
|
def __init__(self, szerokosc, wysokosc, kruche, latwopalne, radioaktywne, niebezpieczne):
|
|
|
|
#config
|
|
|
|
self.cell = 50
|
|
|
|
#init
|
|
|
|
pygame.init()
|
|
|
|
pygame.display.set_caption('Inteligentny wózek widłowy')
|
|
|
|
self.clock = pygame.time.Clock()
|
|
|
|
self.ticks = 0
|
2020-05-17 15:23:06 +02:00
|
|
|
self.data = Data()
|
2020-04-28 12:18:53 +02:00
|
|
|
self.moves = []
|
|
|
|
self.regals = []
|
|
|
|
self.map = Generate.generate(szerokosc+2, wysokosc+2, kruche, latwopalne, radioaktywne, niebezpieczne)
|
|
|
|
self.mapForAStar = Generate.generate(szerokosc+2, wysokosc+2, kruche, latwopalne, radioaktywne, niebezpieczne)
|
|
|
|
self.screen = pygame.display.set_mode((((szerokosc+2)*self.cell), ((wysokosc+2)*self.cell)))
|
|
|
|
self.neurons = Neurons()
|
|
|
|
self.whereDecision = WhereDecision()
|
|
|
|
#create
|
|
|
|
self.wheel = Wheel(self.screen, self.cell);
|
|
|
|
for i in range(len(self.map)):
|
|
|
|
for j in range(len(self.map[i])):
|
|
|
|
if (self.map[i][j]==1):
|
|
|
|
self.map[i][j] = Wall(self.screen, self.cell, i, j)
|
|
|
|
self.mapForAStar[i][j] = 1
|
|
|
|
elif (self.map[i][j]==2):
|
|
|
|
self.map[i][j] = Floor(self.screen, self.cell, i, j)
|
|
|
|
self.mapForAStar[i][j] = 0
|
|
|
|
elif (self.map[i][j]==23):
|
|
|
|
self.map[i][j] = UnboxOnTheFloor(self.screen, self.cell, i, j)
|
2020-05-17 15:23:06 +02:00
|
|
|
self.mapForAStar[i][j] = 0
|
2020-05-12 20:39:36 +02:00
|
|
|
|
2020-04-28 12:18:53 +02:00
|
|
|
else:
|
2020-05-12 20:39:36 +02:00
|
|
|
#regals (kordy i,j oraz rodzaj regału)
|
2020-04-28 12:18:53 +02:00
|
|
|
self.regals.append((i, j, (self.map[i][j]-3)//4))
|
|
|
|
self.map[i][j] = Shelf(self.screen, self.cell, i, j, (self.map[i][j]-3)%4, (self.map[i][j]-3)//4)
|
|
|
|
self.mapForAStar[i][j] = 1
|
2020-05-12 20:39:36 +02:00
|
|
|
|
2020-05-19 19:26:12 +02:00
|
|
|
|
|
|
|
|
2020-05-19 13:59:22 +02:00
|
|
|
|
2020-05-17 15:23:06 +02:00
|
|
|
|
|
|
|
|
2020-05-15 13:20:04 +02:00
|
|
|
#################################################
|
2020-05-12 20:39:36 +02:00
|
|
|
#loop
|
2020-04-28 12:18:53 +02:00
|
|
|
while True:
|
|
|
|
self.events()
|
|
|
|
self.draw()
|
|
|
|
self.clock.tick(40)
|
|
|
|
self.ticks+=1
|
|
|
|
if self.ticks>10:
|
|
|
|
self.ticks=0
|
|
|
|
if len(self.moves)>0:
|
|
|
|
if(self.moves[0]==1):
|
|
|
|
self.wheel.move(Evencik(pygame.K_UP), self.map)
|
|
|
|
elif (self.moves[0]==2):
|
|
|
|
self.wheel.move(Evencik(pygame.K_DOWN), self.map)
|
|
|
|
elif (self.moves[0]==3):
|
|
|
|
self.wheel.move(Evencik(pygame.K_LEFT), self.map)
|
|
|
|
elif (self.moves[0]==4):
|
|
|
|
self.wheel.move(Evencik(pygame.K_RIGHT), self.map)
|
2020-05-18 18:55:18 +02:00
|
|
|
|
2020-04-28 12:18:53 +02:00
|
|
|
self.moves.pop(0)
|
2020-05-12 20:39:36 +02:00
|
|
|
|
2020-04-28 12:18:53 +02:00
|
|
|
def events(self):
|
|
|
|
for event in pygame.event.get():
|
|
|
|
if(event.type==pygame.QUIT):
|
|
|
|
sys.exit()
|
2020-05-18 12:53:51 +02:00
|
|
|
|
2020-05-18 18:55:18 +02:00
|
|
|
elif(event.type==pygame.KEYDOWN):
|
|
|
|
if event.key == pygame.K_g:
|
2020-06-08 21:22:27 +02:00
|
|
|
updateMap(self.data, self.map, self.mapForAStar, self.regals)
|
2020-06-08 22:41:10 +02:00
|
|
|
|
2020-06-08 21:22:27 +02:00
|
|
|
dane = okno()
|
|
|
|
if(dane == 0):
|
|
|
|
continue
|
2020-06-08 22:41:10 +02:00
|
|
|
|
2020-06-08 21:22:27 +02:00
|
|
|
start(self.data,self.wheel, dane)
|
2020-05-18 18:55:18 +02:00
|
|
|
for gen in self.data.best[0]:
|
2020-05-19 13:01:21 +02:00
|
|
|
if(gen.unboxWczesniejszegoGenu == None):
|
|
|
|
kordStartowy = (self.wheel.ns, self.wheel.we)
|
|
|
|
else:
|
|
|
|
kordStartowy = self.data.unbox[gen.unboxWczesniejszegoGenu]
|
|
|
|
|
|
|
|
zbierzBox(gen,self.data, self.moves, kordStartowy)
|
2020-06-09 17:57:00 +02:00
|
|
|
self.data.__init__()
|
2020-05-19 19:26:12 +02:00
|
|
|
elif(event.key== pygame.K_r):
|
2020-06-09 17:57:00 +02:00
|
|
|
self.map = randomBox(self.map, self.regals, 2)
|
2020-05-19 19:26:12 +02:00
|
|
|
updateMap(self.data, self.map, self.mapForAStar, self.regals)
|
2020-05-18 18:55:18 +02:00
|
|
|
elif len(self.moves)==0:
|
2020-04-28 12:18:53 +02:00
|
|
|
self.wheel.move(event, self.map)
|
2020-05-18 18:55:18 +02:00
|
|
|
|
2020-04-28 12:18:53 +02:00
|
|
|
elif(event.type==pygame.MOUSEBUTTONDOWN):
|
|
|
|
if (type(self.map[0][2]) == Floor):
|
2020-05-08 22:35:15 +02:00
|
|
|
meh = easygui.fileopenbox("Wybierz zdjęcie paczki", "Wybierz zdjęcie paczki", filetypes = [["*.jpg", "*.jpeg", "*.png", "Pliki graficzne"]])
|
|
|
|
if meh is None:
|
|
|
|
return
|
|
|
|
while pathlib.Path(meh).suffix!=".jpg" and pathlib.Path(meh).suffix!=".jpeg" and pathlib.Path(meh).suffix!=".png":
|
|
|
|
meh = easygui.fileopenbox("Wybierz zdjęcie paczki", "Wybierz zdjęcie paczki", filetypes = [["*.jpg", "*.jpeg", "*.png", "Pliki graficzne"]])
|
|
|
|
if meh is None:
|
|
|
|
return
|
|
|
|
whatIsIt = self.neurons.whatIsIt(meh)
|
2020-04-28 12:18:53 +02:00
|
|
|
where = self.whereDecision.recognize(whatIsIt, self.regalsik())
|
|
|
|
self.map[0][2] = BoxOnTheFloor(self.screen, self.cell, 0, 2, Box())
|
2020-05-12 20:39:36 +02:00
|
|
|
|
2020-04-28 12:18:53 +02:00
|
|
|
star = AStar()
|
2020-05-17 15:23:06 +02:00
|
|
|
path = star.search([self.wheel.ns, self.wheel.we], [0, 2], self.mapForAStar, 1, 1)
|
2020-04-28 12:18:53 +02:00
|
|
|
cns = self.wheel.ns
|
|
|
|
cwe = self.wheel.we
|
|
|
|
value = path[cns][cwe]
|
|
|
|
while True:
|
|
|
|
if cns>0 and path[cns-1][cwe]==(value+1):
|
|
|
|
cns=cns-1
|
|
|
|
self.moves.append(1)
|
|
|
|
value=value+1
|
|
|
|
continue
|
|
|
|
if cns<(len(self.mapForAStar)-1) and path[cns+1][cwe]==(value+1):
|
|
|
|
cns=cns+1
|
|
|
|
self.moves.append(2)
|
|
|
|
value=value+1
|
|
|
|
continue
|
|
|
|
if cwe>0 and path[cns][cwe-1]==(value+1):
|
|
|
|
cwe=cwe-1
|
|
|
|
self.moves.append(3)
|
|
|
|
value=value+1
|
|
|
|
continue
|
|
|
|
if cwe<(len(self.mapForAStar[0])-1) and path[cns][cwe+1]==(value+1):
|
|
|
|
cns=cns+1
|
|
|
|
self.moves.append(4)
|
|
|
|
value=value+1
|
|
|
|
continue
|
|
|
|
break
|
|
|
|
self.mapForAStar[where[0]][where[1]] = 0
|
2020-05-12 20:39:36 +02:00
|
|
|
# wyszukiwanie ścieżki z miejsca podjęcia paczki do regału
|
|
|
|
# zmienna path posiada macierz oraz kroki podjęte przez wózek
|
2020-05-17 15:23:06 +02:00
|
|
|
path = star.search([0, 2], where, self.mapForAStar, 1, 1)
|
2020-04-28 12:18:53 +02:00
|
|
|
self.mapForAStar[where[0]][where[1]] = 1
|
2020-05-07 22:23:46 +02:00
|
|
|
value = path[cns][cwe]
|
2020-04-28 12:18:53 +02:00
|
|
|
while True:
|
|
|
|
if cns>0 and path[cns-1][cwe]==(value+1):
|
|
|
|
cns=cns-1
|
|
|
|
self.moves.append(1)
|
|
|
|
value=value+1
|
|
|
|
continue
|
|
|
|
if cns<(len(self.mapForAStar)-1) and path[cns+1][cwe]==(value+1):
|
|
|
|
cns=cns+1
|
|
|
|
self.moves.append(2)
|
|
|
|
value=value+1
|
|
|
|
continue
|
|
|
|
if cwe>0 and path[cns][cwe-1]==(value+1):
|
|
|
|
cwe=cwe-1
|
|
|
|
self.moves.append(3)
|
|
|
|
value=value+1
|
|
|
|
continue
|
|
|
|
if cwe<(len(self.mapForAStar[0])-1) and path[cns][cwe+1]==(value+1):
|
|
|
|
cwe=cwe+1
|
|
|
|
self.moves.append(4)
|
|
|
|
value=value+1
|
|
|
|
continue
|
|
|
|
break
|
2020-05-17 15:23:06 +02:00
|
|
|
self.data.zajeteRegaly = znajdzBox(self.map, self.regals)
|
2020-05-19 13:59:22 +02:00
|
|
|
|
2020-04-28 12:18:53 +02:00
|
|
|
def draw(self):
|
|
|
|
self.screen.fill((33,69,108))
|
|
|
|
for i in range(len(self.map)):
|
|
|
|
for j in range(len(self.map[i])):
|
|
|
|
self.map[i][j].draw()
|
|
|
|
self.wheel.draw()
|
|
|
|
pygame.display.flip()
|
|
|
|
def regalsik(self):
|
|
|
|
tmp = []
|
|
|
|
for regal in self.regals:
|
|
|
|
if self.map[regal[0]][regal[1]].isOccupied()==False:
|
|
|
|
tmp.append(regal)
|
2020-05-08 22:35:15 +02:00
|
|
|
return tmp
|