Compare commits
87 Commits
e1c7e6d6e7
...
master
Author | SHA1 | Date | |
---|---|---|---|
b19778b796 | |||
|
15839b4bc4 | ||
216de52583 | |||
|
58b53f5817 | ||
|
2d7ed16185 | ||
|
c267515ee1 | ||
|
aefba8e784 | ||
0824aaa0cf | |||
|
aa401ea87c | ||
73f6396a29 | |||
a21aa44601 | |||
6a364be40e | |||
2727588f82 | |||
|
aef7b97b28 | ||
d6ec2cda4c | |||
|
def9f33cf4 | ||
b997927e5d | |||
38c66cea53 | |||
|
43c13acdee | ||
26bcc9ddf1 | |||
|
301384ed80 | ||
5a56d61a5f | |||
5e2b673196 | |||
|
5339fbbe82 | ||
24f4c60e37 | |||
|
d8e34ad65b | ||
|
a1f2e4b298 | ||
|
f79fa2ee66 | ||
|
1e5a1b9254 | ||
c8bcea171e | |||
|
6a05f59d97 | ||
f60ed5d28f | |||
|
4589d3a49c | ||
9287f76ea3 | |||
|
271e3365f9 | ||
|
aacee0e493 | ||
|
15638862d3 | ||
|
af32df4474 | ||
|
311a2d0757 | ||
|
5440626353 | ||
dd34b7341a | |||
|
c708663d39 | ||
65bef51958 | |||
|
a2d6cc688b | ||
|
f7c91e92aa | ||
|
b25a36e872 | ||
|
76eb4771a2 | ||
|
4ab84deaf3 | ||
|
f0d6001efa | ||
|
af42c84186 | ||
aec533956d | |||
|
bb3fc2715f | ||
|
94f301cae5 | ||
eb7857323c | |||
|
69ce36b4e2 | ||
|
2ce57ea190 | ||
ef1b0c23a9 | |||
b78f3cfa7b | |||
2fbf2be1d7 | |||
9f7ca5d0ed | |||
cf525c5c4b | |||
ca91fd4726 | |||
675df76bfe | |||
|
c22e86e22b | ||
|
c5e6eadcae | ||
|
d0d9fb0309 | ||
|
7c44d65347 | ||
|
f12eb5ba0d | ||
7ef1ccb0a3 | |||
|
2b10bb8650 | ||
|
9323b5a507 | ||
|
aba8285469 | ||
|
ac19577346 | ||
bdd5d1defb | |||
|
5f1acbb6f8 | ||
77032672bc | |||
|
fa3868746a | ||
f937ce349f | |||
|
b4e77de22c | ||
9841406878 | |||
|
693121ac00 | ||
|
4fe38e3afc | ||
|
b8280a3854 | ||
|
ba49049cc3 | ||
|
51fdebbc00 | ||
|
9f0cdbc5ff | ||
5976f41f12 |
3
.idea/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
7
agentActionType.py
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
from enum import Enum
|
||||||
|
|
||||||
|
class AgentActionType (Enum):
|
||||||
|
MOVE_FORWARD = 0
|
||||||
|
TURN_LEFT = 1
|
||||||
|
TURN_RIGHT = 2
|
||||||
|
UNKNOWN = None
|
7
agentOrientation.py
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
from enum import Enum
|
||||||
|
|
||||||
|
class AgentOrientation (Enum):
|
||||||
|
UP = 0
|
||||||
|
RIGHT = 1
|
||||||
|
DOWN = 2
|
||||||
|
LEFT = 3
|
10
agentState.py
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
from agentOrientation import AgentOrientation
|
||||||
|
from typing import Tuple
|
||||||
|
|
||||||
|
class AgentState:
|
||||||
|
orientation: AgentOrientation
|
||||||
|
position: Tuple[int, int]
|
||||||
|
|
||||||
|
def __init__(self, position: Tuple[int, int], orientation: AgentOrientation) -> None:
|
||||||
|
self.orientation = orientation
|
||||||
|
self.position = position
|
165
bfs.py
Normal file
@ -0,0 +1,165 @@
|
|||||||
|
from agentState import AgentState
|
||||||
|
from typing import Dict, Tuple, List
|
||||||
|
from city import City
|
||||||
|
from gridCellType import GridCellType
|
||||||
|
from agentActionType import AgentActionType
|
||||||
|
from agentOrientation import AgentOrientation
|
||||||
|
from queue import Queue, PriorityQueue
|
||||||
|
from turnCar import turn_left_orientation, turn_right_orientation
|
||||||
|
|
||||||
|
|
||||||
|
class Successor: # klasa reprezentuje sukcesora, stan i akcję którą można po nim podjąć
|
||||||
|
|
||||||
|
def __init__(self, state: AgentState, action: AgentActionType, cost: int, predicted_cost: int) -> None:
|
||||||
|
self.state = state
|
||||||
|
self.action = action
|
||||||
|
self.cost = cost
|
||||||
|
self.predicted_cost = predicted_cost
|
||||||
|
|
||||||
|
|
||||||
|
class SuccessorList: # lista sukcesorów, czyli możliwych ścieżek po danym stanie
|
||||||
|
succ_list: list[Successor]
|
||||||
|
|
||||||
|
def __init__(self, succ_list: list[Successor]) -> None:
|
||||||
|
self.succ_list = succ_list
|
||||||
|
|
||||||
|
def __gt__(self, other):
|
||||||
|
return self.succ_list[-1].predicted_cost > other.succ_list[-1].predicted_cost
|
||||||
|
|
||||||
|
def __lt__(self, other):
|
||||||
|
return self.succ_list[-1].predicted_cost < other.succ_list[-1].predicted_cost
|
||||||
|
|
||||||
|
|
||||||
|
def find_path_to_nearest_can(startState: AgentState, grid: Dict[Tuple[int, int], GridCellType], city: City) -> List[
|
||||||
|
AgentActionType]: # znajduje ścieżkę do najbliższego kosza na smieci
|
||||||
|
visited: List[AgentState] = []
|
||||||
|
queue: PriorityQueue[SuccessorList] = PriorityQueue() # kolejka priorytetowa przechodująca listę sukcesorów
|
||||||
|
queue.put(SuccessorList([Successor(startState, AgentActionType.UNKNOWN, 0, _heuristics(startState.position, city))]))
|
||||||
|
|
||||||
|
while not queue.empty(): # dopóki kolejka nie jest pusta, pobiera z niej aktualny element
|
||||||
|
current = queue.get()
|
||||||
|
previous = current.succ_list[-1]
|
||||||
|
visited.append(previous.state)
|
||||||
|
|
||||||
|
if is_state_success(previous.state, grid): # jeśli ostatni stan w liście jest stanem końcowym (agent dotarł do śmietnika)
|
||||||
|
return extract_actions(current)
|
||||||
|
|
||||||
|
successors = get_successors(previous, grid, city)
|
||||||
|
for s in successors:
|
||||||
|
already_visited = False
|
||||||
|
for v in visited:
|
||||||
|
if v.position == s.state.position and v.orientation == s.state.orientation:
|
||||||
|
already_visited = True
|
||||||
|
break
|
||||||
|
if already_visited:
|
||||||
|
continue
|
||||||
|
if is_state_valid(s.state, grid):
|
||||||
|
new_list = current.succ_list.copy()
|
||||||
|
new_list.append(s)
|
||||||
|
queue.put(SuccessorList(new_list))
|
||||||
|
|
||||||
|
return []
|
||||||
|
|
||||||
|
|
||||||
|
def extract_actions(successors: SuccessorList) -> list[AgentActionType]: # wyodrębnienie akcji z listy sukcesorów, z pominięciem uknown
|
||||||
|
output: list[AgentActionType] = []
|
||||||
|
for s in successors.succ_list:
|
||||||
|
if s.action != AgentActionType.UNKNOWN:
|
||||||
|
output.append(s.action)
|
||||||
|
return output
|
||||||
|
|
||||||
|
|
||||||
|
def get_successors(succ: Successor, grid: Dict[Tuple[int, int], GridCellType], city: City) -> List[Successor]:
|
||||||
|
result: List[Successor] = [] # generuje następników dla danego stanu,
|
||||||
|
|
||||||
|
turn_left_cost = 1 + succ.cost
|
||||||
|
turn_left_state = AgentState(succ.state.position, turn_left_orientation(succ.state.orientation))
|
||||||
|
turn_left_heuristics = _heuristics(succ.state.position, city)
|
||||||
|
result.append(
|
||||||
|
Successor(turn_left_state, AgentActionType.TURN_LEFT, turn_left_cost, turn_left_cost + turn_left_heuristics))
|
||||||
|
|
||||||
|
turn_right_cost = 1 + succ.cost
|
||||||
|
turn_right_state = AgentState(succ.state.position, turn_right_orientation(succ.state.orientation))
|
||||||
|
turn_right_heuristics = _heuristics(succ.state.position, city)
|
||||||
|
result.append(
|
||||||
|
Successor(turn_right_state, AgentActionType.TURN_RIGHT, turn_right_cost,
|
||||||
|
turn_right_cost + turn_right_heuristics))
|
||||||
|
|
||||||
|
state_succ = move_forward_succ(succ, city, grid)
|
||||||
|
if state_succ is not None:
|
||||||
|
result.append(state_succ)
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def move_forward_succ(succ: Successor, city: City, grid: Dict[Tuple[int, int], GridCellType]) -> Successor:
|
||||||
|
position = get_next_cell(succ.state)
|
||||||
|
if position is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
cost = get_cost_for_action(AgentActionType.MOVE_FORWARD, grid[position]) + succ.cost
|
||||||
|
predicted_cost = cost + _heuristics(position, city)
|
||||||
|
new_state = AgentState(position, succ.state.orientation)
|
||||||
|
return Successor(new_state, AgentActionType.MOVE_FORWARD, cost, predicted_cost)
|
||||||
|
|
||||||
|
|
||||||
|
def get_next_cell(state: AgentState) -> Tuple[int, int]:
|
||||||
|
x, y = state.position
|
||||||
|
orientation = state.orientation
|
||||||
|
|
||||||
|
if orientation == AgentOrientation.UP:
|
||||||
|
if y - 1 < 1:
|
||||||
|
return None
|
||||||
|
return x, y - 1
|
||||||
|
elif orientation == AgentOrientation.DOWN:
|
||||||
|
if y + 1 > 27:
|
||||||
|
return None
|
||||||
|
return x, y + 1
|
||||||
|
elif orientation == AgentOrientation.LEFT:
|
||||||
|
if x - 1 < 1:
|
||||||
|
return None
|
||||||
|
return x - 1, y
|
||||||
|
elif x + 1 > 27:
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
return x + 1, y
|
||||||
|
|
||||||
|
|
||||||
|
def is_state_success(state: AgentState, grid: Dict[Tuple[int, int], GridCellType]) -> bool:
|
||||||
|
next_cell = get_next_cell(state)
|
||||||
|
try:
|
||||||
|
return grid[next_cell] == GridCellType.GARBAGE_CAN # agent dotarł do śmietnika
|
||||||
|
except KeyError:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def get_cost_for_action(action: AgentActionType, cell_type: GridCellType) -> int:
|
||||||
|
if action in [AgentActionType.TURN_LEFT, AgentActionType.TURN_RIGHT]:
|
||||||
|
return 1
|
||||||
|
if cell_type == GridCellType.SPEED_BUMP and action == AgentActionType.MOVE_FORWARD:
|
||||||
|
return -10000
|
||||||
|
if action == AgentActionType.MOVE_FORWARD:
|
||||||
|
return 3
|
||||||
|
|
||||||
|
|
||||||
|
def is_state_valid(state: AgentState, grid: Dict[Tuple[int, int], GridCellType]) -> bool:
|
||||||
|
try:
|
||||||
|
return grid[state.position] == GridCellType.STREET_HORIZONTAL or grid[
|
||||||
|
state.position] == GridCellType.STREET_VERTICAL or grid[state.position] == GridCellType.SPEED_BUMP
|
||||||
|
except KeyError:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def _heuristics(position: Tuple[int, int], city: City):
|
||||||
|
min_distance: int = 300
|
||||||
|
found_nonvisited: bool = False
|
||||||
|
for can in city.cans:
|
||||||
|
if can.is_visited:
|
||||||
|
continue
|
||||||
|
found_nonvisited = True
|
||||||
|
distance = 3 * (abs(position[0] - can.position[0]) + abs(position[1] - can.position[1]))
|
||||||
|
if distance < min_distance:
|
||||||
|
min_distance = distance
|
||||||
|
if found_nonvisited:
|
||||||
|
return min_distance
|
||||||
|
return -1
|
63
city.py
@ -1,39 +1,44 @@
|
|||||||
from typing import List, Tuple
|
from typing import List, Dict, Tuple
|
||||||
from garbageCan import GarbageCan
|
from garbageCan import GarbageCan
|
||||||
|
from speedBump import SpeedBump
|
||||||
|
from street import Street
|
||||||
|
from gameContext import GameContext
|
||||||
|
|
||||||
class Node:
|
|
||||||
garbageCan: GarbageCan
|
|
||||||
id: int
|
|
||||||
|
|
||||||
def __init__(self, id: int, can: GarbageCan) -> None:
|
|
||||||
self.id = id
|
|
||||||
self.can = can
|
|
||||||
|
|
||||||
class City:
|
class City:
|
||||||
nodes: List[Node]
|
cans: List[GarbageCan]
|
||||||
streets: List[Tuple[int, int]]
|
bumps: List[SpeedBump]
|
||||||
|
streets: List[Street]
|
||||||
|
cans_dict: Dict[Tuple[int, int], GarbageCan] = {}
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self.nodes = []
|
self.cans = []
|
||||||
self.streets = []
|
self.streets = []
|
||||||
|
self.bumps = []
|
||||||
|
|
||||||
def add_node(self, node: Node) -> None:
|
def add_can(self, can: GarbageCan) -> None:
|
||||||
self.nodes.append(node)
|
self.cans.append(can)
|
||||||
|
self.cans_dict[can.position] = can
|
||||||
def add_street(self, street: Tuple[int, int]) -> None:
|
|
||||||
firstFound: bool = False
|
|
||||||
secondFound: bool = False
|
|
||||||
|
|
||||||
for node in self.nodes:
|
|
||||||
if firstFound and secondFound:
|
|
||||||
break
|
|
||||||
if node.id == street.__getitem__(0):
|
|
||||||
firstFound = True
|
|
||||||
continue
|
|
||||||
if node.id == street.__getitem__(1):
|
|
||||||
secondFound = True
|
|
||||||
|
|
||||||
if not firstFound or not secondFound:
|
|
||||||
return
|
|
||||||
|
|
||||||
|
def add_street(self, street: Street) -> None:
|
||||||
self.streets.append(street)
|
self.streets.append(street)
|
||||||
|
|
||||||
|
def add_bump(self, bump: SpeedBump) -> None:
|
||||||
|
self.streets.append(bump)
|
||||||
|
|
||||||
|
def render_city(self, game_context: GameContext) -> None:
|
||||||
|
self._render_streets(game_context)
|
||||||
|
self._render_nodes(game_context)
|
||||||
|
self._render_bumps(game_context)
|
||||||
|
|
||||||
|
def _render_streets(self, game_context: GameContext) -> None:
|
||||||
|
for street in self.streets:
|
||||||
|
street.render(game_context)
|
||||||
|
|
||||||
|
def _render_nodes(self, game_context: GameContext) -> None:
|
||||||
|
for node in self.cans:
|
||||||
|
node.render(game_context)
|
||||||
|
|
||||||
|
def _render_bumps(self, game_context: GameContext) -> None:
|
||||||
|
for bump in self.bumps:
|
||||||
|
bump.render(game_context)
|
@ -1,5 +1,7 @@
|
|||||||
|
from typing import Tuple, List, Dict
|
||||||
import pygame
|
import pygame
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
from gridCellType import GridCellType
|
||||||
|
|
||||||
class GameContext:
|
class GameContext:
|
||||||
dust_car_speed = 20
|
dust_car_speed = 20
|
||||||
@ -8,12 +10,27 @@ class GameContext:
|
|||||||
dust_car_pygame = None
|
dust_car_pygame = None
|
||||||
dust_car_pil = None
|
dust_car_pil = None
|
||||||
canvas = None
|
canvas = None
|
||||||
|
_cell_size: int = 30
|
||||||
|
city = None
|
||||||
|
grid: Dict[Tuple[int, int], GridCellType] = {}
|
||||||
|
dust_car = None
|
||||||
|
landfill = None
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def __init__(self) -> None:
|
||||||
|
self._init_grid()
|
||||||
|
|
||||||
|
def _init_grid(self) -> None:
|
||||||
|
for i in range(1, 28):
|
||||||
|
for j in range(1, 28):
|
||||||
|
self.grid[(i, j)] = GridCellType.NOTHING
|
||||||
|
|
||||||
|
def render_in_cell(self, cell: Tuple[int, int], img_path: str):
|
||||||
|
img = Image.open(img_path)
|
||||||
|
pygame_img = pygame.image.frombuffer(img.tobytes(), (self._cell_size,self._cell_size), 'RGB')
|
||||||
|
start_x = (cell[0] - 1) * self._cell_size
|
||||||
|
start_y = (cell[1] - 1) * self._cell_size
|
||||||
|
self.canvas.blit(pygame_img, (start_x, start_y))
|
||||||
|
|
||||||
def startup(game_context: GameContext):
|
|
||||||
render_background(game_context)
|
|
||||||
game_context.canvas.blit(game_context.dust_car_pygame, (game_context.dust_car_position_x, game_context.dust_car_position_y))
|
|
||||||
|
|
||||||
def render_background(game_context: GameContext):
|
|
||||||
bg_img = Image.open("imgs/background.jpg")
|
|
||||||
pygame_bg_image = pygame.image.frombuffer(bg_img.tobytes(), bg_img.size, 'RGB')
|
|
||||||
game_context.canvas.blit(pygame_bg_image, (0, 0))
|
|
||||||
|
@ -2,23 +2,4 @@ import pygame
|
|||||||
from gameContext import GameContext
|
from gameContext import GameContext
|
||||||
|
|
||||||
def handle_game_event(event, game_context: GameContext):
|
def handle_game_event(event, game_context: GameContext):
|
||||||
dust_car_movement(event, game_context)
|
pass
|
||||||
return
|
|
||||||
|
|
||||||
def dust_car_movement(event, game_context:GameContext):
|
|
||||||
if event.type != pygame.KEYDOWN:
|
|
||||||
return
|
|
||||||
(width, height) = game_context.dust_car_pil.size
|
|
||||||
if event.key == pygame.K_LEFT:
|
|
||||||
pygame.draw.rect(game_context.canvas, (0, 0, 0), (game_context.dust_car_position_x, game_context.dust_car_position_y, width, height))
|
|
||||||
game_context.dust_car_position_x -= game_context.dust_car_speed
|
|
||||||
elif event.key == pygame.K_RIGHT:
|
|
||||||
pygame.draw.rect(game_context.canvas, (0, 0, 0), (game_context.dust_car_position_x, game_context.dust_car_position_y, width, height))
|
|
||||||
game_context.dust_car_position_x += game_context.dust_car_speed
|
|
||||||
elif event.key == pygame.K_UP:
|
|
||||||
pygame.draw.rect(game_context.canvas, (0, 0, 0), (game_context.dust_car_position_x, game_context.dust_car_position_y, width, height))
|
|
||||||
game_context.dust_car_position_y -= game_context.dust_car_speed
|
|
||||||
elif event.key == pygame.K_DOWN:
|
|
||||||
pygame.draw.rect(game_context.canvas, (0, 0, 0), (game_context.dust_car_position_x, game_context.dust_car_position_y, width, height))
|
|
||||||
game_context.dust_car_position_y += game_context.dust_car_speed
|
|
||||||
game_context.canvas.blit(game_context.dust_car_pygame, (game_context.dust_car_position_x, game_context.dust_car_position_y))
|
|
28
garbage.py
@ -1,21 +1,41 @@
|
|||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
class GarbageType (Enum):
|
|
||||||
|
class GarbageType(Enum):
|
||||||
PAPER = 0,
|
PAPER = 0,
|
||||||
PLASTIC_AND_METAL = 1
|
PLASTIC_AND_METAL = 1
|
||||||
GLASS = 3
|
GLASS = 3
|
||||||
BIO = 4
|
BIO = 4
|
||||||
MIXED = 5
|
MIXED = 5
|
||||||
|
|
||||||
|
|
||||||
class Garbage:
|
class Garbage:
|
||||||
img: str
|
img: str
|
||||||
|
shape: str
|
||||||
|
flexibility: str
|
||||||
|
does_smell: str
|
||||||
|
weight: str
|
||||||
|
size: str
|
||||||
|
color: str
|
||||||
|
softness: str
|
||||||
|
does_din: str
|
||||||
|
|
||||||
def __init__(self, img: str) -> None:
|
def __init__(self, img: str, shape: str, flexibility: str, does_smell: str, weight: str, size: str, color: str, softness: str, does_din: str) -> None:
|
||||||
self.img = img
|
self.img = img
|
||||||
|
self.shape = shape
|
||||||
|
self.flexibility = flexibility
|
||||||
|
self.does_smell = does_smell
|
||||||
|
self.weight = weight
|
||||||
|
self.size = size
|
||||||
|
self.color = color
|
||||||
|
self.softness = softness
|
||||||
|
self.does_din = does_din
|
||||||
|
|
||||||
class RecognizedGarbage (Garbage):
|
|
||||||
|
class RecognizedGarbage:
|
||||||
garbage_type: GarbageType
|
garbage_type: GarbageType
|
||||||
|
src: Garbage
|
||||||
|
|
||||||
def __init__(self, src: Garbage, garbage_type: GarbageType) -> None:
|
def __init__(self, src: Garbage, garbage_type: GarbageType) -> None:
|
||||||
super().__init__(src.img)
|
|
||||||
self.garbage_type = garbage_type
|
self.garbage_type = garbage_type
|
||||||
|
self.src = src
|
||||||
|
@ -1,16 +1,26 @@
|
|||||||
from garbage import Garbage
|
from garbage import Garbage
|
||||||
from typing import List, Tuple
|
from typing import List, Tuple
|
||||||
|
from gameContext import GameContext
|
||||||
|
from gridCellType import GridCellType
|
||||||
|
|
||||||
|
|
||||||
class GarbageCan:
|
class GarbageCan:
|
||||||
position: Tuple[int, int]
|
position: Tuple[int, int]
|
||||||
garbage: List[Garbage]
|
garbage: List[Garbage]
|
||||||
|
is_visited: bool
|
||||||
|
|
||||||
def __init__(self, position: Tuple[int, int]) -> None:
|
def __init__(self, position: Tuple[int, int]) -> None:
|
||||||
self.position = position
|
self.position = position
|
||||||
self.garbage = []
|
self.garbage = []
|
||||||
|
self.is_visited = False
|
||||||
|
|
||||||
def add_garbage(self, garbage: Garbage) -> None:
|
def add_garbage(self, garbage: Garbage) -> None:
|
||||||
self.garbage.append(garbage)
|
self.garbage.append(garbage)
|
||||||
|
|
||||||
def remove_garbage(self, garbage: Garbage) -> None:
|
def remove_garbage(self, garbage: Garbage) -> None:
|
||||||
self.garbage.remove(garbage)
|
self.garbage.remove(garbage)
|
||||||
|
|
||||||
|
|
||||||
|
def render(self, game_context: GameContext) -> None:
|
||||||
|
game_context.render_in_cell(self.position, "imgs/container.png")
|
||||||
|
game_context.grid[self.position] = GridCellType.GARBAGE_CAN
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
from typing import List, Tuple
|
from typing import List, Tuple
|
||||||
|
from agentOrientation import AgentOrientation
|
||||||
from garbage import RecognizedGarbage
|
from garbage import GarbageType, RecognizedGarbage
|
||||||
|
from gameContext import GameContext
|
||||||
|
|
||||||
class GarbageTruck:
|
class GarbageTruck:
|
||||||
position: Tuple[int, int]
|
position: Tuple[int, int]
|
||||||
@ -9,8 +10,9 @@ class GarbageTruck:
|
|||||||
glass: List[RecognizedGarbage]
|
glass: List[RecognizedGarbage]
|
||||||
bio: List[RecognizedGarbage]
|
bio: List[RecognizedGarbage]
|
||||||
mixed: List[RecognizedGarbage]
|
mixed: List[RecognizedGarbage]
|
||||||
truckSpritePath = 'imgs/dust_car.png'
|
orientation: AgentOrientation = AgentOrientation.RIGHT
|
||||||
def __init__(self, position: List[int, int]) -> None:
|
|
||||||
|
def __init__(self, position: Tuple[int, int]) -> None:
|
||||||
self.position = position
|
self.position = position
|
||||||
self.paper = []
|
self.paper = []
|
||||||
self.plastic_and_metal = []
|
self.plastic_and_metal = []
|
||||||
@ -20,17 +22,29 @@ class GarbageTruck:
|
|||||||
|
|
||||||
|
|
||||||
def sort_garbage(self, RecognizedGarbage) -> None:
|
def sort_garbage(self, RecognizedGarbage) -> None:
|
||||||
if RecognizedGarbage.garbage_type == 0:
|
if RecognizedGarbage.garbage_type == GarbageType.PAPER:
|
||||||
self.paper.append(RecognizedGarbage)
|
self.paper.append(RecognizedGarbage)
|
||||||
|
|
||||||
elif RecognizedGarbage.garbage_type == 1:
|
elif RecognizedGarbage.garbage_type == GarbageType.PLASTIC_AND_METAL:
|
||||||
self.plastic_and_metal.append(RecognizedGarbage)
|
self.plastic_and_metal.append(RecognizedGarbage)
|
||||||
|
|
||||||
elif RecognizedGarbage.garbage_type == 3:
|
elif RecognizedGarbage.garbage_type == GarbageType.GLASS:
|
||||||
self.glass.append(RecognizedGarbage)
|
self.glass.append(RecognizedGarbage)
|
||||||
|
|
||||||
elif RecognizedGarbage.garbage_type == 4:
|
elif RecognizedGarbage.garbage_type == GarbageType.BIO:
|
||||||
self.bio.append(RecognizedGarbage)
|
self.bio.append(RecognizedGarbage)
|
||||||
|
|
||||||
elif RecognizedGarbage.garbage_type == 5:
|
elif RecognizedGarbage.garbage_type == GarbageType.MIXED:
|
||||||
self.mixed.append(RecognizedGarbage)
|
self.mixed.append(RecognizedGarbage)
|
||||||
|
|
||||||
|
def render(self, game_context: GameContext) -> None:
|
||||||
|
path = None
|
||||||
|
if self.orientation == AgentOrientation.LEFT:
|
||||||
|
path = 'imgs/dust_car_left.png'
|
||||||
|
elif self.orientation == AgentOrientation.RIGHT:
|
||||||
|
path = 'imgs/dust_car_right.png'
|
||||||
|
elif self.orientation == AgentOrientation.UP:
|
||||||
|
path = 'imgs/dust_car_up.png'
|
||||||
|
elif self.orientation == AgentOrientation.DOWN:
|
||||||
|
path = 'imgs/dust_car_down.png'
|
||||||
|
game_context.render_in_cell(self.position, path)
|
11
gridCellType.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
from enum import Enum
|
||||||
|
|
||||||
|
class GridCellType(Enum):
|
||||||
|
NOTHING = 0
|
||||||
|
STREET_VERTICAL = 1
|
||||||
|
STREET_HORIZONTAL = 2
|
||||||
|
GARBAGE_CAN = 3
|
||||||
|
VISITED_GARBAGE_CAN = 4
|
||||||
|
LANDFILL = 5
|
||||||
|
SPEED_BUMP = 6
|
||||||
|
UNKNOWN = None
|
Before Width: | Height: | Size: 331 B After Width: | Height: | Size: 750 B |
Before Width: | Height: | Size: 348 B |
BIN
imgs/dust_car_down.png
Normal file
After Width: | Height: | Size: 1.0 KiB |
BIN
imgs/dust_car_left.png
Normal file
After Width: | Height: | Size: 751 B |
BIN
imgs/dust_car_right.png
Normal file
After Width: | Height: | Size: 751 B |
BIN
imgs/dust_car_up.png
Normal file
After Width: | Height: | Size: 1.1 KiB |
BIN
imgs/landfill.png
Normal file
After Width: | Height: | Size: 857 B |
BIN
imgs/speed_bump.png
Normal file
After Width: | Height: | Size: 293 B |
BIN
imgs/street_horizontal.png
Normal file
After Width: | Height: | Size: 165 B |
BIN
imgs/street_vertical.png
Normal file
After Width: | Height: | Size: 166 B |
39
landfill.py
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
from typing import Tuple
|
||||||
|
from gameContext import GameContext
|
||||||
|
from garbage import RecognizedGarbage
|
||||||
|
from gridCellType import GridCellType
|
||||||
|
|
||||||
|
class Landfill:
|
||||||
|
position: Tuple[int, int] = []
|
||||||
|
paper: list[RecognizedGarbage]
|
||||||
|
plastic_and_metal: list[RecognizedGarbage] = []
|
||||||
|
glass: list[RecognizedGarbage] = []
|
||||||
|
bio: list[RecognizedGarbage] = []
|
||||||
|
mixed: list[RecognizedGarbage] = []
|
||||||
|
|
||||||
|
def __init__(self, position: Tuple[int, int]) -> None:
|
||||||
|
self.position = position
|
||||||
|
|
||||||
|
def add_paper(self, paper: list[RecognizedGarbage]) -> None:
|
||||||
|
for p in paper:
|
||||||
|
self.paper.append(p)
|
||||||
|
|
||||||
|
def add_plastic_and_metal(self, plastic_and_metal: list[RecognizedGarbage]) -> None:
|
||||||
|
for p in plastic_and_metal:
|
||||||
|
self.plastic_and_metal.append(p)
|
||||||
|
|
||||||
|
def add_glass(self, glass: list[RecognizedGarbage]) -> None:
|
||||||
|
for g in glass:
|
||||||
|
self.glass.append(g)
|
||||||
|
|
||||||
|
def add_paper(self, bio: list[RecognizedGarbage]) -> None:
|
||||||
|
for b in bio:
|
||||||
|
self.bio.append(b)
|
||||||
|
|
||||||
|
def add_mixed(self, mixed: list[RecognizedGarbage]) -> None:
|
||||||
|
for m in mixed:
|
||||||
|
self.mixed.append(m)
|
||||||
|
|
||||||
|
def render(self, game_context: GameContext) -> None:
|
||||||
|
game_context.render_in_cell(self.position, 'imgs/landfill.png')
|
||||||
|
game_context.grid[self.position] = GridCellType.LANDFILL
|
95
machine_learning/decisionTree.py
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
import os
|
||||||
|
from trainingData import TrainingData
|
||||||
|
from sklearn import tree
|
||||||
|
import joblib
|
||||||
|
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
def _read_training_data() -> TrainingData:
|
||||||
|
attributes = []
|
||||||
|
classes = []
|
||||||
|
location = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
|
||||||
|
file = open(os.path.join(location, 'training_data.csv'))
|
||||||
|
lines = file.readlines()[1:]
|
||||||
|
file.close()
|
||||||
|
for line in lines:
|
||||||
|
actual_row = line.replace('\n', '')
|
||||||
|
values = actual_row.split(',')
|
||||||
|
line_attributes = values[:-1]
|
||||||
|
line_class = values[-1]
|
||||||
|
attributes.append(line_attributes)
|
||||||
|
classes.append(line_class.strip())
|
||||||
|
return TrainingData(attributes, classes)
|
||||||
|
|
||||||
|
def _attributes_to_floats(attributes: list[str]) -> list[float]:
|
||||||
|
output: list[float] = []
|
||||||
|
if attributes[0] == 'Longitiudonal':
|
||||||
|
output.append(0)
|
||||||
|
elif attributes[0] == 'Round':
|
||||||
|
output.append(1)
|
||||||
|
elif attributes[0] == 'Flat':
|
||||||
|
output.append(2)
|
||||||
|
elif attributes[0] == 'Irregular':
|
||||||
|
output.append(3)
|
||||||
|
|
||||||
|
if attributes[1] == 'Low':
|
||||||
|
output.append(0)
|
||||||
|
elif attributes[1] == 'Medium':
|
||||||
|
output.append(1)
|
||||||
|
elif attributes[1] == 'High':
|
||||||
|
output.append(2)
|
||||||
|
|
||||||
|
|
||||||
|
if attributes[2] == "Yes":
|
||||||
|
output.append(0)
|
||||||
|
else:
|
||||||
|
output.append(1)
|
||||||
|
|
||||||
|
if attributes[3] == 'Low':
|
||||||
|
output.append(0)
|
||||||
|
elif attributes[3] == 'Medium':
|
||||||
|
output.append(1)
|
||||||
|
elif attributes[3] == 'High':
|
||||||
|
output.append(2)
|
||||||
|
|
||||||
|
if attributes[4] == 'Low':
|
||||||
|
output.append(0)
|
||||||
|
elif attributes[4] == 'Medium':
|
||||||
|
output.append(1)
|
||||||
|
elif attributes[4] == 'High':
|
||||||
|
output.append(2)
|
||||||
|
|
||||||
|
if attributes[5] == 'Transparent':
|
||||||
|
output.append(0)
|
||||||
|
elif attributes[5] == 'Light':
|
||||||
|
output.append(1)
|
||||||
|
elif attributes[5] == 'Dark':
|
||||||
|
output.append(2)
|
||||||
|
elif attributes[5] == "Colorful":
|
||||||
|
output.append(3)
|
||||||
|
|
||||||
|
if attributes[6] == 'Low':
|
||||||
|
output.append(0)
|
||||||
|
elif attributes[6] == 'Medium':
|
||||||
|
output.append(1)
|
||||||
|
elif attributes[6] == 'High':
|
||||||
|
output.append(2)
|
||||||
|
|
||||||
|
if attributes[7] == "Yes":
|
||||||
|
output.append(0)
|
||||||
|
else:
|
||||||
|
output.append(1)
|
||||||
|
return output
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
trainning_data = _read_training_data()
|
||||||
|
|
||||||
|
X = trainning_data.attributes
|
||||||
|
Y = trainning_data.classes
|
||||||
|
|
||||||
|
|
||||||
|
model = tree.DecisionTreeClassifier()
|
||||||
|
encoded = [_attributes_to_floats(x) for x in X]
|
||||||
|
dtc = model.fit(encoded, Y)
|
||||||
|
joblib.dump(model, 'model.pkl')
|
29
machine_learning/garbage_infill.csv
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
Shape,Flexibility,DoesSmell,Weight,Size,Color,Softness,DoesDin
|
||||||
|
Irregular,High,No,High,Medium,Dark,Low,Yes
|
||||||
|
Longitiudonal,Low,No,Low,Low,Light,Medium,Yes
|
||||||
|
Longitiudonal,Medium,No,Medium,Low,Dark,High,No
|
||||||
|
Longitiudonal,Low,No,Medium,Low,Dark,High,Yes
|
||||||
|
Round,Low,Yes,High,High,Transparent,High,Yes
|
||||||
|
Irregular,Medium,Yes,High,Low,Transparent,Medium,No
|
||||||
|
Longitiudonal,Medium,Yes,Low,High,Colorful,Medium,Yes
|
||||||
|
Longitiudonal,Low,No,Low,Medium,Dark,Medium,Yes
|
||||||
|
Flat,Medium,Yes,High,Low,Transparent,Low,Yes
|
||||||
|
Irregular,Medium,Yes,High,Medium,Dark,Low,No
|
||||||
|
Longitiudonal,High,No,Low,High,Colorful,Low,Yes
|
||||||
|
Round,Medium,No,Medium,Medium,Dark,Low,No
|
||||||
|
Longitiudonal,Medium,No,Medium,Medium,Transparent,High,No
|
||||||
|
Flat,Medium,Yes,Low,Low,Light,Medium,No
|
||||||
|
Flat,Medium,Yes,Medium,High,Light,Medium,No
|
||||||
|
Flat,Low,No,High,Low,Dark,High,No
|
||||||
|
Longitiudonal,Medium,Yes,High,High,Dark,Low,Yes
|
||||||
|
Flat,Low,Yes,Low,Low,Transparent,Low,No
|
||||||
|
Flat,Low,No,Medium,Low,Colorful,Low,No
|
||||||
|
Longitiudonal,Low,Yes,High,Medium,Transparent,Low,No
|
||||||
|
Longitiudonal,Low,No,Medium,High,Dark,Low,Yes
|
||||||
|
Irregular,Medium,No,Medium,Medium,Light,Low,Yes
|
||||||
|
Longitiudonal,High,No,High,High,Colorful,Low,No
|
||||||
|
Flat,Low,No,Low,Low,Dark,High,No
|
||||||
|
Flat,Low,Yes,Low,High,Dark,Low,Yes
|
||||||
|
Irregular,Medium,Yes,High,High,Dark,Low,No
|
||||||
|
Flat,High,No,High,Low,Dark,Medium,Yes
|
||||||
|
Longitiudonal,High,Yes,Low,Medium,Colorful,Low,Yes
|
|
BIN
machine_learning/garbage_photos/bio/biological1.jpg
Normal file
After Width: | Height: | Size: 7.9 KiB |
BIN
machine_learning/garbage_photos/bio/biological10.jpg
Normal file
After Width: | Height: | Size: 7.3 KiB |
BIN
machine_learning/garbage_photos/bio/biological100.jpg
Normal file
After Width: | Height: | Size: 5.7 KiB |
BIN
machine_learning/garbage_photos/bio/biological101.jpg
Normal file
After Width: | Height: | Size: 7.0 KiB |
BIN
machine_learning/garbage_photos/bio/biological102.jpg
Normal file
After Width: | Height: | Size: 8.4 KiB |
BIN
machine_learning/garbage_photos/bio/biological103.jpg
Normal file
After Width: | Height: | Size: 5.3 KiB |
BIN
machine_learning/garbage_photos/bio/biological104.jpg
Normal file
After Width: | Height: | Size: 9.8 KiB |
BIN
machine_learning/garbage_photos/bio/biological105.jpg
Normal file
After Width: | Height: | Size: 6.5 KiB |
BIN
machine_learning/garbage_photos/bio/biological106.jpg
Normal file
After Width: | Height: | Size: 6.2 KiB |
BIN
machine_learning/garbage_photos/bio/biological107.jpg
Normal file
After Width: | Height: | Size: 7.6 KiB |
BIN
machine_learning/garbage_photos/bio/biological108.jpg
Normal file
After Width: | Height: | Size: 5.1 KiB |
BIN
machine_learning/garbage_photos/bio/biological109.jpg
Normal file
After Width: | Height: | Size: 5.9 KiB |
BIN
machine_learning/garbage_photos/bio/biological11.jpg
Normal file
After Width: | Height: | Size: 5.0 KiB |
BIN
machine_learning/garbage_photos/bio/biological110.jpg
Normal file
After Width: | Height: | Size: 6.7 KiB |
BIN
machine_learning/garbage_photos/bio/biological111.jpg
Normal file
After Width: | Height: | Size: 5.1 KiB |
BIN
machine_learning/garbage_photos/bio/biological112.jpg
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
machine_learning/garbage_photos/bio/biological113.jpg
Normal file
After Width: | Height: | Size: 8.7 KiB |
BIN
machine_learning/garbage_photos/bio/biological114.jpg
Normal file
After Width: | Height: | Size: 9.0 KiB |
BIN
machine_learning/garbage_photos/bio/biological115.jpg
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
machine_learning/garbage_photos/bio/biological116.jpg
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
machine_learning/garbage_photos/bio/biological117.jpg
Normal file
After Width: | Height: | Size: 9.2 KiB |
BIN
machine_learning/garbage_photos/bio/biological118.jpg
Normal file
After Width: | Height: | Size: 5.6 KiB |
BIN
machine_learning/garbage_photos/bio/biological119.jpg
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
machine_learning/garbage_photos/bio/biological12.jpg
Normal file
After Width: | Height: | Size: 4.4 KiB |
BIN
machine_learning/garbage_photos/bio/biological120.jpg
Normal file
After Width: | Height: | Size: 7.9 KiB |
BIN
machine_learning/garbage_photos/bio/biological121.jpg
Normal file
After Width: | Height: | Size: 7.9 KiB |
BIN
machine_learning/garbage_photos/bio/biological122.jpg
Normal file
After Width: | Height: | Size: 6.6 KiB |
BIN
machine_learning/garbage_photos/bio/biological123.jpg
Normal file
After Width: | Height: | Size: 13 KiB |
BIN
machine_learning/garbage_photos/bio/biological124.jpg
Normal file
After Width: | Height: | Size: 9.5 KiB |
BIN
machine_learning/garbage_photos/bio/biological125.jpg
Normal file
After Width: | Height: | Size: 9.1 KiB |
BIN
machine_learning/garbage_photos/bio/biological126.jpg
Normal file
After Width: | Height: | Size: 8.7 KiB |
BIN
machine_learning/garbage_photos/bio/biological127.jpg
Normal file
After Width: | Height: | Size: 5.3 KiB |
BIN
machine_learning/garbage_photos/bio/biological128.jpg
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
machine_learning/garbage_photos/bio/biological129.jpg
Normal file
After Width: | Height: | Size: 10 KiB |
BIN
machine_learning/garbage_photos/bio/biological13.jpg
Normal file
After Width: | Height: | Size: 8.5 KiB |
BIN
machine_learning/garbage_photos/bio/biological130.jpg
Normal file
After Width: | Height: | Size: 4.3 KiB |
BIN
machine_learning/garbage_photos/bio/biological131.jpg
Normal file
After Width: | Height: | Size: 3.5 KiB |
BIN
machine_learning/garbage_photos/bio/biological132.jpg
Normal file
After Width: | Height: | Size: 7.8 KiB |
BIN
machine_learning/garbage_photos/bio/biological133.jpg
Normal file
After Width: | Height: | Size: 8.2 KiB |
BIN
machine_learning/garbage_photos/bio/biological134.jpg
Normal file
After Width: | Height: | Size: 4.8 KiB |
BIN
machine_learning/garbage_photos/bio/biological135.jpg
Normal file
After Width: | Height: | Size: 12 KiB |
BIN
machine_learning/garbage_photos/bio/biological136.jpg
Normal file
After Width: | Height: | Size: 6.2 KiB |
BIN
machine_learning/garbage_photos/bio/biological137.jpg
Normal file
After Width: | Height: | Size: 12 KiB |
BIN
machine_learning/garbage_photos/bio/biological138.jpg
Normal file
After Width: | Height: | Size: 6.9 KiB |
BIN
machine_learning/garbage_photos/bio/biological139.jpg
Normal file
After Width: | Height: | Size: 7.7 KiB |
BIN
machine_learning/garbage_photos/bio/biological14.jpg
Normal file
After Width: | Height: | Size: 7.8 KiB |
BIN
machine_learning/garbage_photos/bio/biological140.jpg
Normal file
After Width: | Height: | Size: 8.2 KiB |
BIN
machine_learning/garbage_photos/bio/biological141.jpg
Normal file
After Width: | Height: | Size: 4.4 KiB |
BIN
machine_learning/garbage_photos/bio/biological142.jpg
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
machine_learning/garbage_photos/bio/biological143.jpg
Normal file
After Width: | Height: | Size: 9.8 KiB |
BIN
machine_learning/garbage_photos/bio/biological144.jpg
Normal file
After Width: | Height: | Size: 5.2 KiB |
BIN
machine_learning/garbage_photos/bio/biological145.jpg
Normal file
After Width: | Height: | Size: 7.1 KiB |
BIN
machine_learning/garbage_photos/bio/biological146.jpg
Normal file
After Width: | Height: | Size: 8.9 KiB |
BIN
machine_learning/garbage_photos/bio/biological147.jpg
Normal file
After Width: | Height: | Size: 4.7 KiB |
BIN
machine_learning/garbage_photos/bio/biological148.jpg
Normal file
After Width: | Height: | Size: 8.1 KiB |
BIN
machine_learning/garbage_photos/bio/biological149.jpg
Normal file
After Width: | Height: | Size: 8.3 KiB |
BIN
machine_learning/garbage_photos/bio/biological15.jpg
Normal file
After Width: | Height: | Size: 7.1 KiB |
BIN
machine_learning/garbage_photos/bio/biological150.jpg
Normal file
After Width: | Height: | Size: 3.3 KiB |
BIN
machine_learning/garbage_photos/bio/biological151.jpg
Normal file
After Width: | Height: | Size: 9.6 KiB |
BIN
machine_learning/garbage_photos/bio/biological152.jpg
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
machine_learning/garbage_photos/bio/biological153.jpg
Normal file
After Width: | Height: | Size: 10 KiB |
BIN
machine_learning/garbage_photos/bio/biological154.jpg
Normal file
After Width: | Height: | Size: 14 KiB |
BIN
machine_learning/garbage_photos/bio/biological155.jpg
Normal file
After Width: | Height: | Size: 6.3 KiB |
BIN
machine_learning/garbage_photos/bio/biological156.jpg
Normal file
After Width: | Height: | Size: 4.4 KiB |
BIN
machine_learning/garbage_photos/bio/biological157.jpg
Normal file
After Width: | Height: | Size: 13 KiB |
BIN
machine_learning/garbage_photos/bio/biological158.jpg
Normal file
After Width: | Height: | Size: 14 KiB |
BIN
machine_learning/garbage_photos/bio/biological159.jpg
Normal file
After Width: | Height: | Size: 6.2 KiB |
BIN
machine_learning/garbage_photos/bio/biological16.jpg
Normal file
After Width: | Height: | Size: 5.8 KiB |
BIN
machine_learning/garbage_photos/bio/biological160.jpg
Normal file
After Width: | Height: | Size: 8.8 KiB |
BIN
machine_learning/garbage_photos/bio/biological161.jpg
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
machine_learning/garbage_photos/bio/biological162.jpg
Normal file
After Width: | Height: | Size: 12 KiB |
BIN
machine_learning/garbage_photos/bio/biological163.jpg
Normal file
After Width: | Height: | Size: 6.3 KiB |
BIN
machine_learning/garbage_photos/bio/biological164.jpg
Normal file
After Width: | Height: | Size: 7.5 KiB |
BIN
machine_learning/garbage_photos/bio/biological165.jpg
Normal file
After Width: | Height: | Size: 7.3 KiB |
BIN
machine_learning/garbage_photos/bio/biological166.jpg
Normal file
After Width: | Height: | Size: 14 KiB |