updating for individual projecet

This commit is contained in:
Kamila Bobkowska 2020-05-02 15:23:05 +00:00
parent d98a738d48
commit 3c058cf50e

376
Main.py
View File

@ -1,179 +1,197 @@
import pygame import pygame
import random import random
import numpy as np import numpy as np
import time import time
import collections import collections
import alg import alg
from models.dumpster import trash from models.dumpster import trash
from models.Garbagetruck import GarbageTruck from models.Garbagetruck import GarbageTruck
import numbering
from sklearn.datasets import load_digits
#heuristics for finding the closest dumpster
def closest(GT, D): #heuristics for finding the closest dumpster
point = np.column_stack((np.abs(GT[0]-D[:,0])+np.abs(GT[1]-D[:,1]),D)) def closest(GT, D):
sorted_list = sorted(point, key=lambda x:x[0]) point = np.column_stack((np.abs(GT[0]-D[:,0])+np.abs(GT[1]-D[:,1]),D))
return sorted_list[0][1:] sorted_list = sorted(point, key=lambda x:x[0])
return sorted_list[0][1:]
def display(grid, GT, a):
#find closest dumpster and the path def display(grid, GT, a):
ZZ = closest(list(GT.coor), a) #find closest dumpster and the path
b = alg.aStar(grid, tuple(GT.coor), tuple(ZZ)) ZZ = closest(list(GT.coor), a)
b = alg.aStar(grid, tuple(GT.coor), tuple(ZZ))
# go through the route to the closest dumpster
for i in b: # go through the route to the closest dumpster
for event in pygame.event.get(): for i in b:
if event.type == pygame.QUIT: for event in pygame.event.get():
done = True if event.type == pygame.QUIT:
GT.turn(i) done = True
GT.move() GT.turn(i)
GT.move()
# "Speed" of the Garbage Truck
clock.tick(4) # "Speed" of the Garbage Truck
clock.tick(4)
grid=drawGrid(grid, 20)
screen.blit(GT.picture, ((WIDTH) * GT.coor[0], (HEIGHT) *GT.coor[1])) grid=drawGrid(grid, 20)
pygame.display.flip() screen.blit(GT.picture, ((WIDTH) * GT.coor[0], (HEIGHT) *GT.coor[1]))
return ZZ pygame.display.flip()
return ZZ
#creating dumpsters
def createDumpstersAndDump(size,number): #creating dumpsters
points=[] def createDumpstersAndDump(size,number):
for x in range (0,size): points=[]
for y in range (0,size): for x in range (0,size):
points.append([x,y]) for y in range (0,size):
points.append([x,y])
wol=random.sample(points,number)
return (wol) wol=random.sample(points,number)
return (wol)
def drawGrid(grid, size):
for row in range(size): def drawGrid(grid, size):
for column in range(size): for row in range(size):
#colouring dumpsters for column in range(size):
img=grassImg #colouring dumpsters
if grid[row][column] == 1: img=grassImg
#plastic if grid[row][column] == 1:
img = garbageY #plastic
elif grid[row][column] == 2: img = garbageY
#paper elif grid[row][column] == 2:
img = garbageG #paper
elif grid[row][column] == 3: img = garbageG
#glass elif grid[row][column] == 3:
img = garbageR #glass
elif grid[row][column] == 4: img = garbageR
#organic elif grid[row][column] == 4:
img = garbageBL #organic
elif grid[row][column] == 5: img = garbageBL
#mixede elif grid[row][column] == 5:
img = garbageBLK #mixede
elif row == TheDump[:,0] and column == TheDump[:,1]: img = garbageBLK
grid[row][column] = 0 elif row == TheDump[:,0] and column == TheDump[:,1]:
img = recycImg grid[row][column] = 0
screen.blit(img, ((WIDTH) * row, (HEIGHT) * column)) img = recycImg
screen.blit(img, ((WIDTH) * row, (HEIGHT) * column))
return grid
return grid
# 700:20=35
WIDTH = 35 # 700:20=35
HEIGHT = 35 WIDTH = 35
HEIGHT = 35
# Creation of a two dimensional grid
grid = [] # Creation of a two dimensional grid
for row in range(20): grid = []
grid.append([]) for row in range(20):
for column in range(20): grid.append([])
grid[row].append(10) for column in range(20):
grid[row].append(10)
# number of dumpsters
size=20 # number of dumpsters
size=20
#randomizing coordinates, initializing the Garbage Dump
a = createDumpstersAndDump(20,size+1) #randomizing coordinates, initializing the Garbage Dump
TheDump = np.reshape(a[-1], (1, 2)) a = createDumpstersAndDump(20,size+1)
a = np.reshape(a[:-1], (size, 2)) TheDump = np.reshape(a[-1], (1, 2))
a = np.reshape(a[:-1], (size, 2))
#initiating the coordinates for the garbage truck
GT = GarbageTruck()
coor = GT.start() #Patryk Krawiec subproject part
digitsy=np.load("Ytest.npy")
digitsx=np.load("Xtest.npy")
#Initializing dumpsters images_and_labels=list(zip(digitsx,digitsy))
dumpsters = [] num,xnum,ynum=numbering.num(20,images_and_labels)
for i in range(0,size):
s = trash() paying_customers=num[3:]
s.giveID(i) paying_customers = list(map(int, paying_customers))
s.throwAway()
s.colour() #initiating the coordinates for the garbage truck
s.xy = a[i] GT = GarbageTruck()
dumpsters.append(s) coor = GT.start()
#colouring trashcans
buff = 0 #Initializing dumpsters
for x in a: dumpsters = []
grid[x[0]][x[1]] = dumpsters[buff].color for i in range(0,size):
buff += 1 s = trash()
s.giveID(i)
# Initializing, sizing the window & title s.throwAway()
pygame.init() s.colour()
winsize=700 s.xy = a[i]
WINDOW_SIZE = [winsize, winsize] s.numberx=xnum[i]
screen = pygame.display.set_mode(WINDOW_SIZE) s.numbery=ynum[i]
pygame.display.set_caption("Intelligent Garbage Truck") dumpsters.append(s)
# Loop until user clicks close #colouring trashcans
done = False buff = 0
for x in a:
# Managing refreshing speed grid[x[0]][x[1]] = dumpsters[buff].color
clock = pygame.time.Clock() buff += 1
#resources # Initializing, sizing the window & title
grassImg = pygame.image.load('./images/grass.png') pygame.init()
garbageY = pygame.image.load('./images/garbageyellow.png') winsize=700
garbageR = pygame.image.load('./images/garbagered.png') WINDOW_SIZE = [winsize, winsize]
garbageBL = pygame.image.load('./images/garbageblue.png') screen = pygame.display.set_mode(WINDOW_SIZE)
garbageBLK = pygame.image.load('./images/garbageblack.png') pygame.display.set_caption("Intelligent Garbage Truck")
garbageG = pygame.image.load('./images/garbagegreen.png')
recycImg = pygame.image.load('./images/recycling.png') # Loop until user clicks close
done = False
all = a
while not done: # Managing refreshing speed
for event in pygame.event.get(): clock = pygame.time.Clock()
if event.type == pygame.QUIT:
done = True #resources
grassImg = pygame.image.load('./images/grass.png')
# Draw the grid garbageY = pygame.image.load('./images/garbageyellow.png')
grid=drawGrid(grid, 20) garbageR = pygame.image.load('./images/garbagered.png')
garbageBL = pygame.image.load('./images/garbageblue.png')
#move and display garbageBLK = pygame.image.load('./images/garbageblack.png')
ZZ = display(grid, GT, a) garbageG = pygame.image.load('./images/garbagegreen.png')
recycImg = pygame.image.load('./images/recycling.png')
for x in all: all = a
if(x[0] == GT.coor[0] and x[1] == GT.coor[1]): while not done:
for event in pygame.event.get():
#recognize which dumpster are we visiting if event.type == pygame.QUIT:
Xz=np.where(all==x)[0] done = True
x =[item for item, count in collections.Counter(Xz).items() if count > 1]
# Draw the grid
if GT.collectingTrash(dumpsters, x[0]): grid=drawGrid(grid, 20)
dumpsters[x[0]].empty()
elif GT.amIFull(): #move and display
print("DUMPSTER TRUCK FULL") ZZ = display(grid, GT, a)
else:
print("No more space for this kind of trash")
for x in all:
# remove visited dumpster if(x[0] == GT.coor[0] and x[1] == GT.coor[1]):
a = a[~((a[:,0] ==ZZ[0]) & (a[:,1] ==ZZ[1]))]
print("Dumpsters left: ",len(a)) #recognize which dumpster are we visiting
Xz=np.where(all==x)[0]
# visit Garbage Dump at the end x =[item for item, count in collections.Counter(Xz).items() if count > 1]
if len(a) == 0: predicted=numbering.pred(dumpsters[x[0]].numberx,dumpsters[x[0]].numbery)
print("Going to the Garbage Dump") if(predicted not in paying_customers):
display(grid, GT, TheDump) print("House number is: ",predicted," but it is not our customer")
done = True elif GT.collectingTrash(dumpsters, x[0]):
dumpsters[x[0]].empty()
pygame.quit() print("House number is: ",predicted)
elif GT.amIFull():
print("DUMPSTER TRUCK FULL")
else:
print("No more space for this kind of trash")
# remove visited dumpster
a = a[~((a[:,0] ==ZZ[0]) & (a[:,1] ==ZZ[1]))]
print("Dumpsters left: ",len(a))
# visit Garbage Dump at the end
if len(a) == 0:
print("Going to the Garbage Dump")
display(grid, GT, TheDump)
done = True
pygame.quit()