AI/program.py

156 lines
6.8 KiB
Python
Raw Normal View History

2020-04-28 12:18:53 +02:00
import pygame
from os import sys
from Mapa.generate import Generate
from Mapa.floor import Floor
from Mapa.wall import Wall
from Mapa.shelf import Shelf
2020-04-28 12:18:53 +02:00
from wheel import Wheel
from Mapa.boxOnTheFloor import BoxOnTheFloor
from Mapa.box import Box
from Mapa.unboxOnTheFloor import UnboxOnTheFloor
2020-04-28 12:18:53 +02:00
from AStar import AStar
import easygui
from neurons import Neurons
from whereDecision import WhereDecision
from Evencik import Evencik
from genetyczny import metody
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
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);
#przekladanie wartosci liczbowych z tablicy na mapę obiektow
2020-04-28 12:18:53 +02:00
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)
self.mapForAStar[i][j] = 1
2020-04-28 12:18:53 +02:00
else:
#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
#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)
self.moves.pop(0)
2020-04-28 12:18:53 +02:00
def events(self):
for event in pygame.event.get():
if(event.type==pygame.QUIT):
sys.exit()
elif(event.type==pygame.KEYDOWN):
if len(self.moves)==0:
self.wheel.move(event, self.map)
elif(event.type==pygame.MOUSEBUTTONDOWN):
if (type(self.map[0][2]) == Floor):
whatIsIt = self.neurons.whatIsIt(easygui.fileopenbox("Wybierz zdjęcie paczki", "Wybierz zdjęcie paczki", filetypes = [["*.jpg", "*.jpeg", "*.png", "Pliki graficzne"]]))
where = self.whereDecision.recognize(whatIsIt, self.regalsik()) #kordy regalu
2020-04-28 12:18:53 +02:00
self.map[0][2] = BoxOnTheFloor(self.screen, self.cell, 0, 2, Box())
2020-04-28 12:18:53 +02:00
star = AStar()
path = star.search([self.wheel.ns, self.wheel.we], [0, 2], self.mapForAStar, 1)
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
# wyszukiwanie ścieżki z miejsca podjęcia paczki do regału
# zmienna path posiada macierz oraz kroki podjęte przez wózek
2020-04-28 12:18:53 +02:00
path = star.search([0, 2], where, self.mapForAStar, 1)
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
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)
return tmp