Compare commits
2 Commits
master
...
s464840-ob
Author | SHA1 | Date | |
---|---|---|---|
83d0918595 | |||
95a3ac8755 |
@ -1,47 +1,10 @@
|
|||||||
from pathlib import Path
|
import sys
|
||||||
|
|
||||||
import numpy as np
|
|
||||||
|
|
||||||
import astar
|
|
||||||
import pygame
|
import pygame
|
||||||
import snn
|
|
||||||
import joblib
|
|
||||||
import os
|
|
||||||
|
|
||||||
screen = []
|
screen = []
|
||||||
objectArray = []
|
objectArray = []
|
||||||
collisionsMap = []
|
collisionsMap = []
|
||||||
|
|
||||||
decision_tree = joblib.load(Path('../tree/decisionTreeClassifier'))
|
|
||||||
|
|
||||||
type_map = {
|
|
||||||
'glass': 2,
|
|
||||||
'others': 0,
|
|
||||||
'paper': 4,
|
|
||||||
'plastic': 3
|
|
||||||
}
|
|
||||||
testset_path = Path('../testset')
|
|
||||||
testset = [Path(f'{testset_path}/{file}') for file in os.listdir(testset_path)]
|
|
||||||
|
|
||||||
season = 2
|
|
||||||
truck_full = 0
|
|
||||||
truck_working = 0
|
|
||||||
|
|
||||||
weightsMap = ([1, 2, 1, 4, 5, 2, 7, 8, 5, 4, 15, 3, 4, 5, 8],
|
|
||||||
[1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 3, 4, 5, 1],
|
|
||||||
[1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 3, 4, 5, 3],
|
|
||||||
[1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 3, 3, 8, 5, 4],
|
|
||||||
[1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 3, 9, 5, 2],
|
|
||||||
[1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 12, 4, 5, 6],
|
|
||||||
[1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 7, 3, 4, 5, 7],
|
|
||||||
[5, 2, 1, 4, 5, 2, 7, 8, 1, 4, 17, 14, 4, 5, 1],
|
|
||||||
[1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 3, 4, 14, 3],
|
|
||||||
[1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 3, 4, 14, 2],
|
|
||||||
[5, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 3, 4, 14, 6],
|
|
||||||
[1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 13, 14, 15, 7],
|
|
||||||
[1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 14, 4, 14, 1],
|
|
||||||
[1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 3, 4, 15, 2],
|
|
||||||
[1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 3, 4, 15, 2])
|
|
||||||
|
|
||||||
class Position:
|
class Position:
|
||||||
def __init__(self, x, y):
|
def __init__(self, x, y):
|
||||||
@ -83,12 +46,23 @@ class Agent(Object):
|
|||||||
circleX = 52 + self.pos.x * square
|
circleX = 52 + self.pos.x * square
|
||||||
circleY = 12 + self.pos.y * square
|
circleY = 12 + self.pos.y * square
|
||||||
|
|
||||||
truck = pygame.image.load("../car.png").convert_alpha() # tu ścieżka do zdjęcia w tle
|
truck = pygame.image.load("car.png").convert_alpha() # tu ścieżka do zdjęcia w tle
|
||||||
truck = pygame.transform.scale(truck, (square, square))
|
truck = pygame.transform.scale(truck, (square, square))
|
||||||
screen.blit(truck, (circleX, circleY))
|
screen.blit(truck, (circleX, circleY))
|
||||||
|
|
||||||
def move(self, newPos):
|
def move(self, event, gridLength):
|
||||||
self.pos = newPos
|
if event.key == pygame.K_LEFT:
|
||||||
|
newPos = self.pos.get_moved(-1, 0)
|
||||||
|
self.move_if_possible(newPos, gridLength)
|
||||||
|
if event.key == pygame.K_RIGHT:
|
||||||
|
newPos = self.pos.get_moved(1, 0)
|
||||||
|
self.move_if_possible(newPos, gridLength)
|
||||||
|
if event.key == pygame.K_UP:
|
||||||
|
newPos = self.pos.get_moved(0, -1)
|
||||||
|
self.move_if_possible(newPos, gridLength)
|
||||||
|
if event.key == pygame.K_DOWN:
|
||||||
|
newPos = self.pos.get_moved(0, 1)
|
||||||
|
self.move_if_possible(newPos, gridLength)
|
||||||
|
|
||||||
def move_if_possible(self, newPos, gridLength):
|
def move_if_possible(self, newPos, gridLength):
|
||||||
if movement_allowed(newPos, gridLength):
|
if movement_allowed(newPos, gridLength):
|
||||||
@ -96,20 +70,20 @@ class Agent(Object):
|
|||||||
|
|
||||||
|
|
||||||
class House(Object):
|
class House(Object):
|
||||||
def __init__(self, name, pos, **kwargs):
|
def __init__(self, name, pos):
|
||||||
super().__init__(name, pos)
|
super().__init__(name, pos)
|
||||||
self.trash_can = {
|
self.trash_cans = {
|
||||||
"type": kwargs.get('type', np.random.choice(testset)),
|
"paper": False,
|
||||||
'animal': kwargs.get('animal', np.random.randint(0, 2)),
|
"glass": False,
|
||||||
'quantity': kwargs.get('quantity', np.random.random() * 0.7 + 0.3),
|
"plastic": False,
|
||||||
'time': kwargs.get('time', np.random.randint(0, 6))
|
"bio": False
|
||||||
}
|
}
|
||||||
|
|
||||||
def draw(self, square):
|
def draw(self, square):
|
||||||
x = 52 + self.pos.x * square
|
x = 52 + self.pos.x * square
|
||||||
y = 12 + self.pos.y * square
|
y = 12 + self.pos.y * square
|
||||||
|
|
||||||
house = pygame.image.load("../house.png").convert_alpha() # tu ścieżka do zdjęcia w tle
|
house = pygame.image.load("house.png").convert_alpha() # tu ścieżka do zdjęcia w tle
|
||||||
house = pygame.transform.scale(house, (square, square))
|
house = pygame.transform.scale(house, (square, square))
|
||||||
screen.blit(house, (x, y))
|
screen.blit(house, (x, y))
|
||||||
|
|
||||||
@ -121,14 +95,14 @@ class Junkyard(Object):
|
|||||||
"paper": True,
|
"paper": True,
|
||||||
"glass": True,
|
"glass": True,
|
||||||
"plastic": True,
|
"plastic": True,
|
||||||
"others": True
|
"bio": True
|
||||||
}
|
}
|
||||||
|
|
||||||
def draw(self, square):
|
def draw(self, square):
|
||||||
x = 52 + self.pos.x * square
|
x = 52 + self.pos.x * square
|
||||||
y = 12 + self.pos.y * square
|
y = 12 + self.pos.y * square
|
||||||
|
|
||||||
junkyard = pygame.image.load("../junkyard.png").convert_alpha() # tu ścieżka do zdjęcia w tle
|
junkyard = pygame.image.load("junkyard.png").convert_alpha() # tu ścieżka do zdjęcia w tle
|
||||||
junkyard = pygame.transform.scale(junkyard, (square, square))
|
junkyard = pygame.transform.scale(junkyard, (square, square))
|
||||||
screen.blit(junkyard, (x, y))
|
screen.blit(junkyard, (x, y))
|
||||||
|
|
||||||
@ -141,7 +115,7 @@ class Hole(Object):
|
|||||||
x = 52 + self.pos.x * square
|
x = 52 + self.pos.x * square
|
||||||
y = 12 + self.pos.y * square
|
y = 12 + self.pos.y * square
|
||||||
|
|
||||||
hole = pygame.image.load("../hole.png").convert_alpha() # tu ścieżka do zdjęcia w tle
|
hole = pygame.image.load("hole.png").convert_alpha() # tu ścieżka do zdjęcia w tle
|
||||||
hole = pygame.transform.scale(hole, (square, square))
|
hole = pygame.transform.scale(hole, (square, square))
|
||||||
screen.blit(hole, (x, y))
|
screen.blit(hole, (x, y))
|
||||||
|
|
||||||
@ -172,9 +146,14 @@ def draw(square_num, objectArr):
|
|||||||
(a + grid_size, b + grid_size), 2)
|
(a + grid_size, b + grid_size), 2)
|
||||||
|
|
||||||
|
|
||||||
def kb_listen(objectArray, gridLength, path):
|
def kb_listen(objectArray, gridLength):
|
||||||
agent = objectArray[0]
|
agent = objectArray[0]
|
||||||
#agent.move(gridLength, path)
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.KEYDOWN:
|
||||||
|
agent.move(event, gridLength)
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
pygame.init() # inicjalizacja modułów, na razie niepotrzebna
|
pygame.init() # inicjalizacja modułów, na razie niepotrzebna
|
||||||
@ -184,11 +163,10 @@ if __name__ == '__main__':
|
|||||||
agent = Agent("smieciarka", Position(0, 0))
|
agent = Agent("smieciarka", Position(0, 0))
|
||||||
junkyard = Junkyard("wysypisko", Position(10, 10))
|
junkyard = Junkyard("wysypisko", Position(10, 10))
|
||||||
houses = [House(f'dom-{i}', pos) for i, pos in enumerate([Position(x, y) for x, y in [
|
houses = [House(f'dom-{i}', pos) for i, pos in enumerate([Position(x, y) for x, y in [
|
||||||
(7, 4), (3, 10), (8, 10), (4, 5), (1, 2), (10, 4), (13, 14), (6, 9)
|
(7, 4), (3, 10), (8, 10), (4, 5), (1, 2), (10, 4), (14, 14), (6, 9)
|
||||||
]])]
|
]])]
|
||||||
holes = [Hole(f'dziura-{i}', pos) for i, pos in enumerate([Position(x, y) for x, y in [
|
holes = [Hole(f'dziura-{i}', pos) for i, pos in enumerate([Position(x, y) for x, y in [
|
||||||
(4, 9), (5, 11), (11, 7), (13, 8), (0, 1), (1, 1), (2, 1), (14, 13),
|
(4, 9), (5, 11), (11, 7), (13, 8)
|
||||||
(13, 13), (4, 8), (5, 9), (7, 9), (6, 8), (3, 5), (4, 6), (5, 5)
|
|
||||||
]])]
|
]])]
|
||||||
objectArray.append(agent)
|
objectArray.append(agent)
|
||||||
objectArray.append(junkyard)
|
objectArray.append(junkyard)
|
||||||
@ -196,60 +174,16 @@ if __name__ == '__main__':
|
|||||||
objectArray += holes
|
objectArray += holes
|
||||||
|
|
||||||
collisionsMap = [[False] * gridSize for _ in range(gridSize)]
|
collisionsMap = [[False] * gridSize for _ in range(gridSize)]
|
||||||
for object in holes:
|
for object in objectArray[1:]:
|
||||||
collisionsMap[object.pos.x][object.pos.y] = True
|
collisionsMap[object.pos.x][object.pos.y] = True
|
||||||
|
|
||||||
width = 610
|
width = 610
|
||||||
height = 530
|
height = 530
|
||||||
screen = pygame.display.set_mode((width, height)) # ustalanie rozmiarów okna
|
screen = pygame.display.set_mode((width, height)) # ustalanie rozmiarów okna
|
||||||
|
|
||||||
startPos = (0, 0)
|
while 1:
|
||||||
finalPos = (14, 14)
|
|
||||||
astarPath = astar.aStar(weightsMap, collisionsMap, startPos, finalPos)
|
|
||||||
checkpoints = [startPos]
|
|
||||||
for house in houses:
|
|
||||||
checkpoints.append((house.pos.x, house.pos.y))
|
|
||||||
checkpoints.append(finalPos)
|
|
||||||
astarPath = []
|
|
||||||
for i in range(len(checkpoints) - 1):
|
|
||||||
path = astar.aStar(weightsMap, collisionsMap, checkpoints[i], checkpoints[i + 1])
|
|
||||||
if i == 0:
|
|
||||||
astarPath += path
|
|
||||||
else:
|
|
||||||
astarPath += path[1:]
|
|
||||||
|
|
||||||
print(astarPath)
|
|
||||||
|
|
||||||
pathPos = 0
|
|
||||||
nextCheckpoint = 1
|
|
||||||
while True:
|
|
||||||
agent_x, agent_y = astarPath[pathPos]
|
|
||||||
checkpoint_x, checkpoint_y = checkpoints[nextCheckpoint]
|
|
||||||
agent.pos = Position(agent_x, agent_y)
|
|
||||||
for house in houses:
|
|
||||||
if house.pos.x == agent_x and house.pos.y == agent_y \
|
|
||||||
and agent_x == checkpoint_x and agent_y == checkpoint_y:
|
|
||||||
nextCheckpoint += 1
|
|
||||||
house.trash_can['evaluated_type'] = type_map[snn.getPredict(house.trash_can['type'])[0]]
|
|
||||||
print('House:', house.name, 'pos:', astarPath[pathPos], 'type:', house.trash_can['type'],
|
|
||||||
'evaluated_type:', house.trash_can['evaluated_type'])
|
|
||||||
tree_input = np.array([
|
|
||||||
house.trash_can['evaluated_type'],
|
|
||||||
season,
|
|
||||||
house.trash_can['animal'],
|
|
||||||
truck_full,
|
|
||||||
house.trash_can['quantity'],
|
|
||||||
house.trash_can['time'],
|
|
||||||
truck_working
|
|
||||||
]).reshape(1, -1)
|
|
||||||
should_get = decision_tree.predict(tree_input)
|
|
||||||
print('Desicion tree input:', tree_input, 'result:', should_get)
|
|
||||||
pathPos = pathPos + 1 if pathPos < len(astarPath) - 1 else pathPos
|
|
||||||
c = (255, 255, 255) # tymczasowy kolor tła - do usunięcia, jak już będzie zdjęcie
|
c = (255, 255, 255) # tymczasowy kolor tła - do usunięcia, jak już będzie zdjęcie
|
||||||
screen.fill(c)
|
screen.fill(c)
|
||||||
draw(gridSize, objectArray)
|
draw(gridSize, objectArray)
|
||||||
kb_listen(objectArray, gridSize, astarPath)
|
kb_listen(objectArray, gridSize)
|
||||||
pygame.display.update() # by krata pojawiła się w okienku - update powierzc
|
pygame.display.update() # by krata pojawiła się w okienku - update powierzchni
|
||||||
pygame.time.wait(100)
|
|
||||||
|
|
||||||
|
|
Before Width: | Height: | Size: 8.1 KiB |
Before Width: | Height: | Size: 8.5 KiB |
Before Width: | Height: | Size: 21 KiB |
Before Width: | Height: | Size: 15 KiB |
Before Width: | Height: | Size: 12 KiB |
Before Width: | Height: | Size: 11 KiB |
Before Width: | Height: | Size: 14 KiB |
Before Width: | Height: | Size: 11 KiB |
Before Width: | Height: | Size: 11 KiB |
Before Width: | Height: | Size: 15 KiB |
Before Width: | Height: | Size: 14 KiB |
Before Width: | Height: | Size: 18 KiB |
Before Width: | Height: | Size: 11 KiB |
Before Width: | Height: | Size: 20 KiB |
Before Width: | Height: | Size: 18 KiB |
Before Width: | Height: | Size: 12 KiB |
Before Width: | Height: | Size: 9.4 KiB |
Before Width: | Height: | Size: 12 KiB |
Before Width: | Height: | Size: 8.5 KiB |
Before Width: | Height: | Size: 17 KiB |
Before Width: | Height: | Size: 23 KiB |
Before Width: | Height: | Size: 15 KiB |
Before Width: | Height: | Size: 12 KiB |
Before Width: | Height: | Size: 7.3 KiB |
Before Width: | Height: | Size: 9.0 KiB |
Before Width: | Height: | Size: 16 KiB |
Before Width: | Height: | Size: 24 KiB |
Before Width: | Height: | Size: 17 KiB |
Before Width: | Height: | Size: 13 KiB |
Before Width: | Height: | Size: 14 KiB |
Before Width: | Height: | Size: 12 KiB |
Before Width: | Height: | Size: 8.3 KiB |
Before Width: | Height: | Size: 16 KiB |
Before Width: | Height: | Size: 14 KiB |
Before Width: | Height: | Size: 7.5 KiB |
Before Width: | Height: | Size: 11 KiB |
Before Width: | Height: | Size: 12 KiB |
Before Width: | Height: | Size: 9.0 KiB |
Before Width: | Height: | Size: 11 KiB |
Before Width: | Height: | Size: 14 KiB |
Before Width: | Height: | Size: 14 KiB |
Before Width: | Height: | Size: 10 KiB |
Before Width: | Height: | Size: 17 KiB |
Before Width: | Height: | Size: 17 KiB |
Before Width: | Height: | Size: 16 KiB |
Before Width: | Height: | Size: 7.7 KiB |
Before Width: | Height: | Size: 9.3 KiB |
Before Width: | Height: | Size: 12 KiB |
Before Width: | Height: | Size: 15 KiB |
Before Width: | Height: | Size: 12 KiB |
Before Width: | Height: | Size: 14 KiB |
Before Width: | Height: | Size: 17 KiB |
Before Width: | Height: | Size: 11 KiB |
Before Width: | Height: | Size: 10 KiB |
Before Width: | Height: | Size: 16 KiB |
Before Width: | Height: | Size: 12 KiB |
Before Width: | Height: | Size: 8.9 KiB |
Before Width: | Height: | Size: 18 KiB |
Before Width: | Height: | Size: 17 KiB |
Before Width: | Height: | Size: 14 KiB |
Before Width: | Height: | Size: 15 KiB |
Before Width: | Height: | Size: 16 KiB |
Before Width: | Height: | Size: 12 KiB |
Before Width: | Height: | Size: 18 KiB |
Before Width: | Height: | Size: 16 KiB |
Before Width: | Height: | Size: 12 KiB |
Before Width: | Height: | Size: 15 KiB |
Before Width: | Height: | Size: 9.2 KiB |
Before Width: | Height: | Size: 14 KiB |
Before Width: | Height: | Size: 10 KiB |
Before Width: | Height: | Size: 14 KiB |
Before Width: | Height: | Size: 19 KiB |
Before Width: | Height: | Size: 14 KiB |
Before Width: | Height: | Size: 18 KiB |
Before Width: | Height: | Size: 10 KiB |
Before Width: | Height: | Size: 13 KiB |
Before Width: | Height: | Size: 13 KiB |
Before Width: | Height: | Size: 11 KiB |
Before Width: | Height: | Size: 19 KiB |
Before Width: | Height: | Size: 9.4 KiB |
Before Width: | Height: | Size: 11 KiB |
Before Width: | Height: | Size: 10 KiB |
Before Width: | Height: | Size: 8.8 KiB |
Before Width: | Height: | Size: 16 KiB |
Before Width: | Height: | Size: 9.1 KiB |
Before Width: | Height: | Size: 16 KiB |
Before Width: | Height: | Size: 17 KiB |
Before Width: | Height: | Size: 8.5 KiB |
Before Width: | Height: | Size: 9.5 KiB |
Before Width: | Height: | Size: 12 KiB |
Before Width: | Height: | Size: 8.8 KiB |
Before Width: | Height: | Size: 15 KiB |
Before Width: | Height: | Size: 8.5 KiB |
Before Width: | Height: | Size: 9.2 KiB |
Before Width: | Height: | Size: 17 KiB |
Before Width: | Height: | Size: 20 KiB |
Before Width: | Height: | Size: 18 KiB |
Before Width: | Height: | Size: 8.5 KiB |