From 5df46f3f168c6cd6f4eb8de29c11d7261eab4189 Mon Sep 17 00:00:00 2001 From: Kamila Bobkowska Date: Fri, 1 May 2020 10:37:20 +0000 Subject: [PATCH] adding --- Main.py | 179 +++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 8 +++ alg.py | 62 +++++++++++++++++ environment.md | 46 +++++++++++++ 4 files changed, 295 insertions(+) create mode 100644 Main.py create mode 100644 README.md create mode 100644 alg.py create mode 100644 environment.md diff --git a/Main.py b/Main.py new file mode 100644 index 0000000..40870d0 --- /dev/null +++ b/Main.py @@ -0,0 +1,179 @@ +import pygame +import random +import numpy as np +import time +import collections +import alg +from models.dumpster import trash +from models.Garbagetruck import GarbageTruck + + +#heuristics for finding the closest dumpster +def closest(GT, D): + point = np.column_stack((np.abs(GT[0]-D[:,0])+np.abs(GT[1]-D[:,1]),D)) + 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 + 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: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + done = True + GT.turn(i) + GT.move() + + # "Speed" of the Garbage Truck + clock.tick(4) + + grid=drawGrid(grid, 20) + screen.blit(GT.picture, ((WIDTH) * GT.coor[0], (HEIGHT) *GT.coor[1])) + pygame.display.flip() + return ZZ + + + +#creating dumpsters +def createDumpstersAndDump(size,number): + points=[] + for x in range (0,size): + for y in range (0,size): + points.append([x,y]) + + wol=random.sample(points,number) + return (wol) + + +def drawGrid(grid, size): + for row in range(size): + for column in range(size): + #colouring dumpsters + img=grassImg + if grid[row][column] == 1: + #plastic + img = garbageY + elif grid[row][column] == 2: + #paper + img = garbageG + elif grid[row][column] == 3: + #glass + img = garbageR + elif grid[row][column] == 4: + #organic + img = garbageBL + elif grid[row][column] == 5: + #mixede + img = garbageBLK + elif row == TheDump[:,0] and column == TheDump[:,1]: + grid[row][column] = 0 + img = recycImg + screen.blit(img, ((WIDTH) * row, (HEIGHT) * column)) + + return grid + +# 700:20=35 +WIDTH = 35 +HEIGHT = 35 + +# Creation of a two dimensional grid +grid = [] +for row in range(20): + grid.append([]) + for column in range(20): + grid[row].append(10) + + +# number of dumpsters +size=20 + +#randomizing coordinates, initializing the Garbage Dump +a = createDumpstersAndDump(20,size+1) +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() + + +#Initializing dumpsters +dumpsters = [] +for i in range(0,size): + s = trash() + s.giveID(i) + s.throwAway() + s.colour() + s.xy = a[i] + dumpsters.append(s) + +#colouring trashcans +buff = 0 +for x in a: + grid[x[0]][x[1]] = dumpsters[buff].color + buff += 1 + +# Initializing, sizing the window & title +pygame.init() +winsize=700 +WINDOW_SIZE = [winsize, winsize] +screen = pygame.display.set_mode(WINDOW_SIZE) +pygame.display.set_caption("Intelligent Garbage Truck") + +# Loop until user clicks close +done = False + +# Managing refreshing speed +clock = pygame.time.Clock() + +#resources +grassImg = pygame.image.load('./images/grass.png') +garbageY = pygame.image.load('./images/garbageyellow.png') +garbageR = pygame.image.load('./images/garbagered.png') +garbageBL = pygame.image.load('./images/garbageblue.png') +garbageBLK = pygame.image.load('./images/garbageblack.png') +garbageG = pygame.image.load('./images/garbagegreen.png') +recycImg = pygame.image.load('./images/recycling.png') + +all = a +while not done: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + done = True + + # Draw the grid + grid=drawGrid(grid, 20) + + #move and display + ZZ = display(grid, GT, a) + + + for x in all: + if(x[0] == GT.coor[0] and x[1] == GT.coor[1]): + + #recognize which dumpster are we visiting + Xz=np.where(all==x)[0] + x =[item for item, count in collections.Counter(Xz).items() if count > 1] + + if GT.collectingTrash(dumpsters, x[0]): + dumpsters[x[0]].empty() + 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() diff --git a/README.md b/README.md new file mode 100644 index 0000000..85edd6b --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ +# AI2020_Project + +This is a repository for the 2020 Aritificial Intelligence Project. + +The team members are: +* Kamila Bobkowska +* Klaudia Przybylska +* Patryk Krawiec \ No newline at end of file diff --git a/alg.py b/alg.py new file mode 100644 index 0000000..4245a56 --- /dev/null +++ b/alg.py @@ -0,0 +1,62 @@ +import heapq + +class PriorityQueue: + def __init__(self): + self.elements = [] + + def put(self, item, priority): + heapq.heappush(self.elements, (priority, item)) + + def get(self): + return heapq.heappop(self.elements)[1] + +def heuristic(xy1, xy2): + return abs(xy1[0] - xy2[0]) + abs(xy1[1] - xy2[1]) + +def neighbors(point): + x, y = point + list=((x+1,y), (x,y+1), (x,y-1), (x-1,y)) + return list + +#determining the cost of a specific field in the grid +def checkCost(grid, xy): + x, y = xy + cost = grid[x][y] + return cost + +def aStar(grid, start, goal): + openlist = PriorityQueue() + openlist.put(start, 0) + + fScore = {} + origin = {start: None} + fScore[start] = 0 + closedlist=[] + + cost=0 + while openlist!={}: + current = openlist.get() + + if current == goal: + path = [] + #following from the succesors to the root our starting point + while current != start: + path.append(current) + current = origin[current] + path.reverse() + break + + # succescor function + for succ in neighbors(current): + #checking if didn't go out of the maze + if(succ[0] < 0 or succ[1] < 0 or succ[0] > 19 or succ[1] > 19): + continue + + gScore = fScore[current[0],current[1]] + checkCost(grid, current) + if succ not in closedlist or gScore < fScore[succ[0],succ[1]]: + closedlist.append(succ) + origin[succ[0],succ[1]] = current + fScore[succ[0],succ[1]] = gScore + priority = gScore + heuristic(goal, succ) + openlist.put(succ, priority) + return path diff --git a/environment.md b/environment.md new file mode 100644 index 0000000..2748dcc --- /dev/null +++ b/environment.md @@ -0,0 +1,46 @@ +# **Report** +## General information +This goal of this project is to create an Intelligent Dumpster/Garbage Truck that will be able to move on the grid and evaluate what kind of trash is it dealing with. The words "garbage truck","dumpster truck" are used as synonyms, as are the words "trash", "garbage". + +We assume that all dumpsters that are on the grid contain trash. + +## Technologies used and needed to run the program +The most important technology used thus far is the Python library [PyGame](https://www.pygame.org/news). +The following library imports are need to run the program: +``` +import pygame +import random +import numpy as np +import time +import collections +``` +## Grid +The grid is build with **20x20** squares. The program opens in a window that is **700x700**(a square). On the grid **40** random dumpsters of 5 different kinds appear. + +## How it works +To run the program you need to write the following command in the folder containing the whole project: +``` +python Main.py +``` +After that a Graphical Interface will start running and on the console commands concerning the capacity of the garbage truck will be displayed. The message "No more space for this kind of trash" colu mean either that or that Garbage Truck has already visited the dumpster. +## Objects +Two of the objects have a sepperate class in the catalogue model. There are three main objects appearing on the grid: +1. **Garbage Truck** - the agent, the only object that can move on the whole grid. It moves randomly one step at a time in a random direction. Has the capacity of 5 per kind of garbage(paper, glass, mixed, organic, plastic) making the total capacity of a garbage truck - 20. +2. **Dumpster** - contains some kind of trash. In future updates if a dumpster is emptied it will disappear and later reappear in a different place suggesting that some other person now has trash. Also planned in the near future is the possibility to increase the number of garbage in a dumpster if it is not picked up in some time. +3. **Garbage Dump** - a place in which the garbage truck will empty itself, appears randomly on the grid. The garbage dump has unlimited capacity. + +## Knowledge Representation +The konwledge representation is preserved by our agent - the Garbage Truck. +The Garbage Truck: +* collects garbage from dumpsters +* knows where the dumpsters are +* can determine what kind of a dumpster it is (ex. plastic or paper) +* is able to determine how much garbage it collected and whether it is full and needs to visit the Garbage Dump +* in future updates will be able to check if a specific dumpster is empty(clear message) +* in future it will go to the Garbage Dump when needed + +## Current graphic interface design +![Example](https://i.imgur.com/FGDTTPm.png) + +## Video presentation +https://drive.google.com/file/d/1dt2qdDTBU-VQIsZhiKf40HklcG18AvoB/view?usp=sharing