Revert "Revert "Merge branch 'master' of https://git.wmi.amu.edu.pl/s452625/projektAI""

This reverts commit 33446f9dbc.
This commit is contained in:
Miron 2021-05-21 20:43:17 +02:00
parent 33446f9dbc
commit 11ebd785ec
5 changed files with 612 additions and 10 deletions

131
src/a_star.py Normal file
View File

@ -0,0 +1,131 @@
WAREHOUSE_MAP = [
[0, 0, 0, 10, 10, 0, 0, 0, 0],
[0, 10, 0, 10, 0, 10, 0, 0, 0],
[0, 10, 0, 0, 0, 200, 200, 200, 200],
[0, 0, 10, 0, 0, 0, 0, 0, 0],
[0, 0, 10, 0, 0, 200, 200, 200, 200],
[0, 0, 10, 10, 0, 0, 0, 0, 0],
[10, 0, 0, 0, 10, 200, 200, 200, 200],
[0, 0, 10, 10, 10, 0, 0, 0, 0],
[0, 10, 0, 0, 0, 200, 200, 200, 200],
]
def a_star_search(start, end, agent_direction):
open_nodes = []
closed_nodes = []
start_node = NodeAStar(agent_direction, None, start)
start_node.g = start_node.h = start_node.f = 0
end_node = NodeAStar(None, None, end)
end_node.g = end_node.h = end_node.f = 0
open_nodes.append(start_node)
while len(open_nodes) != 0:
current_node = open_nodes[0]
current_node_index = 0
for index, node in enumerate(open_nodes):
if node.f < current_node.f:
current_node = node
current_node_index = index
open_nodes.pop(current_node_index)
closed_nodes.append(current_node)
# Sprawdzam czy jesteśmy u celu jeżeli tak zwracamy ścieżkę
neighbour_nodes = []
if current_node == end_node:
path = []
current = current_node
while current is not None:
#path.append(current.position)
if current.action is not None:
current.action.reverse()
for each_action in current.action:
path.append(each_action)
current = current.parent
#path = path.pop()
return path[::-1]
#
x = current_node.position[0]
y = current_node.position[1]
if x < 8: # DOWN NEIGHBOUR
if current_node.agent_direction == "right":
actions = ["rotate_right", "move"]
elif current_node.agent_direction == "left":
actions = ["rotate_left", "move"]
elif current_node.agent_direction == "up":
actions = ["rotate_right", "rotate_right", "move"]
elif current_node.agent_direction == "down":
actions = ["move"]
neighbour_nodes.append(
NodeAStar("down", current_node, (x + 1, y), actions))
if x > 0: # UP NEIGHBOUR
if current_node.agent_direction == "right":
actions = ["rotate_left", "move"]
elif current_node.agent_direction == "left":
actions = ["rotate_right", "move"]
elif current_node.agent_direction == "up":
actions = ["move"]
elif current_node.agent_direction == "down":
actions = ["rotate_right", "rotate_right", "move"]
neighbour_nodes.append(NodeAStar("up", current_node, (x - 1, y), actions))
if y > 0: # LEFT NEIGHBOUR
if current_node.agent_direction == "right":
actions = ["rotate_left", "rotate_left", "move"]
elif current_node.agent_direction == "left":
actions = ["move"]
elif current_node.agent_direction == "up":
actions = ["rotate_left", "move"]
elif current_node.agent_direction == "down":
actions = ["rotate_right", "move"]
neighbour_nodes.append(NodeAStar("left", current_node, (x, y - 1), actions))
if y < 8: # RIGHT NEIGHBOUR
if current_node.agent_direction == "right":
actions = ["move"]
elif current_node.agent_direction == "left":
actions = ["rotate_left", "rotate_left", "move"]
elif current_node.agent_direction == "up":
actions = ["rotate_right", "move"]
elif current_node.agent_direction == "down":
actions = ["rotate_left", "move"]
neighbour_nodes.append(NodeAStar("right", current_node, (x, y + 1), actions))
for neighbour in neighbour_nodes:
if len([closed_neighbour for closed_neighbour in closed_nodes if closed_neighbour == neighbour]) > 0:
continue
action_len = 0
if current_node.action is not None:
action_len = len(current_node.action)
neighbour.g = current_node.g + WAREHOUSE_MAP[neighbour.position[0]][neighbour.position[1]] + action_len
neighbour.h = abs(neighbour.position[0] - end_node.position[0]) + abs(
neighbour.position[1] - end_node.position[1])
neighbour.f = neighbour.g + neighbour.h
if len([open_node for open_node in open_nodes if
neighbour.position == open_node.position and neighbour.g > open_node.g]) > 0:
continue
open_nodes.append(neighbour)
class NodeAStar:
def __init__(self, agent_direction, parent=None, position=None, action=None):
self.agent_direction = agent_direction
self.parent = parent
self.position = position
self.action = action
self.g = 0
self.h = 0
self.f = 0
def __eq__(self, other):
return self.position == other.position

113
src/bfs.py Normal file
View File

@ -0,0 +1,113 @@
# FRINGE - struktura danych przechowująca wierzchołki do odwiedzenia
# EXPLORED - lista odwiedzonych stanów
# istate - stan początkowy
# Succ - funkcja następnika
# Goaltest -test spełnienia celu
def breadth_first_search(istate, goal, agent_direction): # COORDINATES OF A START PLACE
fringe = []
explored = []
start = Node(istate, agent_direction)
fringe.append(start)
path = []
while True:
if not fringe:
return False
elem = fringe.pop(0)
if elem.state == goal:
# return ciag akcji zbudowany z wykorzystaniem pol parent i action
while elem.parent is not None:
if type(elem.action) == list:
elem.action.reverse()
for each_action in elem.action:
path.append(each_action)
else:
path.append(elem.action)
elem = elem.parent
path.reverse()
return path
explored.append(elem)
for action, state, direction in elem.successor():
fringe_states = []
explored_states = []
for node in fringe:
fringe_states.append(node.state)
for node2 in explored:
explored_states.append(node2.state)
if state not in fringe_states and state not in explored_states:
x = Node(state, direction)
x.parent = elem
x.action = action
fringe.append(x)
class Node:
def __init__(self, state, agent_direction, action=None, parent=None):
self.state = state
self.action = action
self.parent = parent
self.agent_direction = agent_direction
def successor(self):
neighbours = []
x = self.state[0]
y = self.state[1]
if x < 8: # RIGHT NEIGHBOUR
if self.agent_direction == "right":
actions = "move"
elif self.agent_direction == "left":
actions = ["rotate_right", "rotate_right", "move"]
elif self.agent_direction == "up":
actions = ["rotate_right", "move"]
elif self.agent_direction == "down":
actions = ["rotate_left", "move"]
neighbours.append((actions, (x + 1, y), "right"))
if x > 0: # LEFT NEIGHBOUR
if self.agent_direction == "right":
actions = ["rotate_left", "rotate_left", "move"]
elif self.agent_direction == "left":
actions = "move"
elif self.agent_direction == "up":
actions = ["rotate_left", "move"]
elif self.agent_direction == "down":
actions = ["rotate_right", "move"]
neighbours.append((actions, (x - 1, y), "left"))
if y > 0: # UP NEIGHBOUR
if self.agent_direction == "right":
actions = ["rotate_left", "move"]
elif self.agent_direction == "left":
actions = ["rotate_right", "move"]
elif self.agent_direction == "up":
actions = "move"
elif self.agent_direction == "down":
actions = ["rotate_left", "rotate_left", "move"]
neighbours.append((actions, (x, y - 1), "up"))
if y < 8: # DOWN NEIGHBOUR
if self.agent_direction == "right":
actions = ["rotate_right", "move"]
elif self.agent_direction == "left":
actions = ["rotate_left", "move"]
elif self.agent_direction == "up":
actions = ["rotate_left", "rotate_left", "move"]
elif self.agent_direction == "down":
actions = "move"
neighbours.append((actions, (x, y + 1), "down"))
return neighbours

View File

@ -2,6 +2,9 @@ import pygame
import random import random
import time import time
from src.a_star import a_star_search, WAREHOUSE_MAP
from src.bfs import breadth_first_search
pygame.init() pygame.init()
WIDTH = 675 WIDTH = 675
@ -427,7 +430,7 @@ Shelf_list = [
Shelf((5, 8), 'Electronic', 5), Shelf((6, 8), 'Electronic', 5), Shelf((7, 8), 'Electronic', 5), Shelf((5, 8), 'Electronic', 5), Shelf((6, 8), 'Electronic', 5), Shelf((7, 8), 'Electronic', 5),
Shelf((8, 8), 'Electronic', 5)] Shelf((8, 8), 'Electronic', 5)]
coord_goals = [(6, 8), (0, 0), (5, 8), (0, 0), (7, 8), (0, 0), (8, 8), (0, 0), coord_goals = [(8, 0), (0, 0), (5, 8), (0, 0), (8, 0), (0, 0), (8, 8), (0, 0),
(5, 6), (0, 0), (6, 6), (0, 0), (7, 6), (0, 0), (8, 6), (0, 8)] (5, 6), (0, 0), (6, 6), (0, 0), (7, 6), (0, 0), (8, 6), (0, 8)]
screen = pygame.display.set_mode([WIDTH, HEIGHT]) screen = pygame.display.set_mode([WIDTH, HEIGHT])
@ -438,13 +441,15 @@ Package_list = [
print(a_star_search((6, 8), (0, 0), "down")) print(a_star_search((6, 8), (0, 0), "down"))
# agent.path = breadth_first_search(agent.pos_coord, agent.agent_direction) #agent.path = breadth_first_search(agent.pos_coord, agent.goal, agent.agent_direction)
agent.path = a_star_search((agent.pos_coord[1], agent.pos_coord[0]), (0, 0), agent.agent_direction) agent.path = a_star_search((agent.pos_coord[1], agent.pos_coord[0]), (0, 0), agent.agent_direction)
print(agent.path)
Stain_list = [] #print(agent.path)
# Pętla służąca do tworzenia plam oleju na podstawie mapy magazynu # Pętla służąca do tworzenia plam oleju na podstawie mapy magazynu
Stain_list = []
for index_x in range(9): for index_x in range(9):
for index_y in range(9): for index_y in range(9):
if WAREHOUSE_MAP[index_x][index_y] == 10: if WAREHOUSE_MAP[index_x][index_y] == 10:
@ -461,8 +466,6 @@ while running:
if event.type == pygame.KEYDOWN: if event.type == pygame.KEYDOWN:
pass pass
if len(Package_list) < QTY_OF_PACKAGES: if len(Package_list) < QTY_OF_PACKAGES:
is_dock_empty = True is_dock_empty = True
@ -470,13 +473,16 @@ while running:
for package in Package_list: for package in Package_list:
if package.pos == (40, 40): if package.pos == (40, 40):
is_dock_empty = False is_dock_empty = False
elif package.pos == get_pix_from_position[(8, 0)] and not package.is_package_up:
print("Paczka wyrzucona")
Package_list.remove(package)
if is_dock_empty: if is_dock_empty:
Package_list.append(generate_package(40, 40)) Package_list.append(generate_package(40, 40))
# PATHING # PATHING
if not agent.goal_achieved: if not agent.goal_achieved:
print(agent.path) # print(agent.path)
agent.move_bfs() agent.move_bfs()
else: else:
@ -486,9 +492,8 @@ while running:
agent.goal = coord_goals.pop(0) agent.goal = coord_goals.pop(0)
agent.goal_achieved = False agent.goal_achieved = False
# agent.path = breadth_first_search(agent.pos_coord, agent.agent_direction) #agent.path = breadth_first_search(agent.pos_coord, agent.goal, agent.agent_direction)
agent.path = a_star_search((agent.pos_coord[1], agent.pos_coord[0]), (agent.goal[1], agent.goal[0]), agent.path = a_star_search((agent.pos_coord[1], agent.pos_coord[0]), (agent.goal[1], agent.goal[0]), agent.agent_direction)
agent.agent_direction)
for package in Package_list: for package in Package_list:
if package.is_package_up: if package.is_package_up:
@ -498,6 +503,7 @@ while running:
# DRAWING # DRAWING
screen.blit(BACKGROUND, [0, 0]) screen.blit(BACKGROUND, [0, 0])
screen.blit(DOCK, [0, 0]) screen.blit(DOCK, [0, 0])
screen.blit(pygame.transform.flip(DOCK, True, False), [8*size, 0])
screen.blit(board, board.get_rect()) screen.blit(board, board.get_rect())
for shelf in Shelf_list: for shelf in Shelf_list:
screen.blit(shelf.image, shelf.rect) screen.blit(shelf.image, shelf.rect)

View File

@ -0,0 +1,216 @@
# POZIOM WAGI(1-10), TYP, STOPIEN ROZMIARU(1-10), STOPIEN LATWOPALNOSCI(0-5), STOPIEN WYBUCHOWOSCI(0-5), POZIOM CENY(1-10), PRZYJETY DO MAGAZYNU(tak, nie)
# SZYMON \/
#|WAGA|TYP|WYMIAR|LATWOPALNOSC|WYBUCHOWOSC|CENA|PRZYJETY_DO_MAGAZYNU
#
10 builders 2 0 0 4 nie
1 groceries 4 0 0 5 tak
5 groceries 10 0 0 10 nie
2 groceries 2 0 0 3 tak
1 dangerous 9 3 5 3 nie
1 dangerous 2 4 5 2 nie
5 builders 7 0 0 1 tak
1 electronics 4 0 0 7 tak
1 groceries 2 0 0 4 tak
2 dangerous 8 3 4 2 tak
1 builders 2 0 2 3
5 electronics 5 4 0 3
9 electronics 3 0 0 2
6 groceries 1 0 0 5
2 groceries 3 0 0 1
1 electronics 8 0 0 6
5 builders 1 3 1 5
3 dangerous 1 5 4 5
3 dangerous 7 5 5 1
3 electronics 2 0 0 10
1 builders 6 0 0 8
9 dangerous 8 5 4 1
6 dangerous 3 3 5 5
2 builders 9 0 0 3
5 dangerous 2 3 5 5
4 electronics 6 3 0 9
5 dangerous 2 4 3 4
4 dangerous 1 5 5 2
5 electronics 3 0 0 1
5 builders 3 0 0 7
2 builders 1 5 0 4
1 groceries 7 0 0 5
3 builders 4 2 0 5
9 builders 4 0 0 3
10 groceries 5 0 0 3
5 dangerous 10 3 5 5
4 electronics 3 0 0 4
1 builders 3 2 0 9
4 groceries 3 0 0 2
10 electronics 3 0 0 3
# JERZY \/
#|WAGA|TYP|WYMIAR|LATWOPALNOSC|WYBUCHOWOSC|CENA|PRZYJETY_DO_MAGAZYNU
#
10 builders 1 0 0 5 nie
4 dangerous 5 3 2 2 nie
2 electronics 1 0 0 4 tak
8 dangerous 7 5 4 7 nie
3 electronics 7 1 0 4 tak
4 electronics 7 4 0 3 tak
4 builders 6 0 0 2 tak
1 groceries 2 0 0 5 tak
3 electronics 7 5 1 9 nie
8 electronics 3 0 0 6 tak
3 electronics 2 0 0 4 nie
1 builders 7 0 0 5 tak
4 groceries 2 0 0 10 nie
1 dangerous 1 5 2 2 tak
10 electronics 7 0 2 5 nie
9 electronics 3 5 0 1 nie
1 groceries 3 0 0 1 tak
5 groceries 4 0 0 8 tak
4 groceries 1 0 0 1 tak
10 builders 2 0 0 6 nie
2 dangerous 9 4 3 10 nie
9 groceries 2 0 0 5 nie
2 electronics 1 0 0 1 tak
5 dangerous 5 4 2 3 tak
2 electronics 8 3 0 1 tak
9 dangerous 2 5 4 5 nie
8 electronics 5 0 0 3 tak
9 dangerous 3 3 4 1 tak
7 dangerous 6 3 3 2 tak
1 builders 2 4 0 5 nie
3 groceries 3 0 0 10 nie
3 electronics 2 0 0 2 tak
5 builders 7 0 1 1 tak
4 groceries 4 0 0 4 tak
8 dangerous 9 4 3 10 nie
2 groceries 3 0 0 3 tak
3 groceries 6 0 0 2 tak
4 groceries 3 0 0 5 tak
7 electronics 6 5 0 3 tak
3 electronics 2 0 0 10 nie
# MIRON \/
#|WAGA|TYP|WYMIAR|LATWOPALNOSC|WYBUCHOWOSC|CENA|PRZYJETY_DO_MAGAZYNU
#
1 electronics 4 1 0 9
1 dangerous 6 4 3 4
1 electronics 5 0 0 6
9 builders 5 0 0 10
10 builders 5 5 1 3
7 electronics 7 0 0 10
4 electronics 1 0 0 4
6 groceries 6 0 0 2
10 electronics 5 0 0 2
4 builders 7 5 0 2
4 dangerous 10 4 2 9
9 electronics 6 0 0 5
4 builders 1 0 0 3
2 builders 1 1 2 8
10 dangerous 4 4 4 4
7 builders 1 0 0 1
2 dangerous 4 5 2 3
3 electronics 7 4 0 5
1 electronics 1 3 0 1
9 electronics 4 3 0 9
2 groceries 2 0 0 1
2 electronics 3 0 0 2
4 groceries 7 0 0 3
9 electronics 7 0 0 1
3 electronics 6 0 0 4
8 dangerous 4 3 5 2
5 electronics 3 0 0 3
5 groceries 3 0 0 5
1 groceries 4 0 0 1
4 electronics 1 0 0 3
2 builders 9 0 2 9
5 groceries 2 0 0 5
4 dangerous 7 3 3 3
1 dangerous 7 4 4 1
3 groceries 4 0 0 1
1 electronics 3 0 0 4
6 groceries 6 0 0 2
10 groceries 2 0 0 5
7 groceries 3 0 0 4
7 electronics 4 0 0 8
# BOGDAN \/
#|WAGA|TYP|WYMIAR|LATWOPALNOSC|WYBUCHOWOSC|CENA|PRZYJETY_DO_MAGAZYNU
#
1 builders 10 2 0 3
3 electronics 2 2 0 5
3 groceries 10 0 0 1
10 groceries 7 0 0 4
7 dangerous 2 3 3 4
8 builders 7 2 0 4
4 electronics 1 0 0 10
5 builders 1 0 0 9
6 electronics 4 0 0 4
2 groceries 2 0 0 1
9 electronics 1 0 0 4
4 groceries 6 0 0 3
4 builders 7 0 0 5
4 builders 7 0 0 3
4 builders 5 0 1 2
4 groceries 2 0 0 1
1 groceries 9 0 0 4
2 electronics 5 0 0 2
5 groceries 7 0 0 2
1 builders 1 0 0 1
2 electronics 5 0 0 8
3 electronics 5 0 0 4
4 dangerous 9 5 5 10
3 builders 3 1 0 4
1 groceries 7 0 0 4
2 builders 4 0 0 10
7 dangerous 1 3 2 3
9 groceries 1 0 0 1
4 groceries 7 0 0 4
5 groceries 5 0 0 1
3 builders 2 3 2 2
2 groceries 7 0 0 5
1 builders 3 0 2 4
9 builders 5 0 0 2
4 electronics 8 5 0 5
2 builders 6 4 0 1
3 electronics 4 0 2 3
1 electronics 5 0 0 4
2 dangerous 4 3 3 9
1 builders 6 0 0 2
# MACIEJ \/
#|WAGA|TYP|WYMIAR|LATWOPALNOSC|WYBUCHOWOSC|CENA|PRZYJETY_DO_MAGAZYNU
#
1 electronics 7 0 0 2
4 groceries 6 0 0 5
1 electronics 2 0 0 1
3 dangerous 5 4 3 2
2 groceries 2 0 0 1
1 electronics 3 0 2 2
4 dangerous 5 4 3 5
7 electronics 10 0 0 2
2 electronics 8 3 0 4
4 electronics 1 0 0 2
2 electronics 3 0 0 5
3 builders 7 0 0 3
5 builders 7 0 0 8
2 builders 10 3 0 1
2 builders 1 2 0 9
2 builders 1 3 0 2
4 dangerous 5 3 3 3
1 dangerous 1 3 5 5
2 groceries 3 0 0 4
4 electronics 7 0 0 4
2 groceries 7 0 0 5
4 groceries 1 0 0 5
4 dangerous 7 3 5 4
5 builders 5 0 0 3
1 electronics 7 0 0 4
10 electronics 6 1 0 7
3 builders 5 5 0 8
1 groceries 1 0 0 1
3 electronics 6 0 0 3
2 electronics 3 0 0 5
4 builders 3 0 1 10
4 builders 2 0 0 5
3 electronics 7 0 0 5
3 builders 6 5 0 2
9 builders 1 5 1 3
7 groceries 8 0 0 1
2 groceries 9 0 0 3
6 electronics 1 4 0 9
4 electronics 5 0 0 4
5 dangerous 6 3 3 5

View File

@ -0,0 +1,136 @@
from random import randint
#waga,
MAX_WEIGHT = 10
SECOND_BORDER_WEIGHT = 5
MIN_WEIGHT = 1
#kategoria,
DANGEROUS = "dangerous"
GROCERIES = "groceries"
ELECTRONICS = "electronics"
BUILDERS = "builders"
# rozmiar,
MAX_SIZE = 10
SECOND_BORDER_SIZE = 7
MIN_SIZE = 1
# łatwopalność
MAX_FLAMMABILITY = 5
MIN_FLAMMABILITY = 1
NO_FLAMMABILITY = 0
# ,wybuchowość,
MAX_EXPLOSIVENESS = 5
MIN_EXPLOSIVENESS = 1
NO_EXPLOSIVENESS = 0
# cena
MAX_PRICE = 10
SECOND_BORDER_PRICE = 5
MIN_PRICE = 1
QTY_OF_CASES = 200
OUTPUT_FILEPATH = "data/training_list_id3.txt"
def generate_weight():
second_border = SECOND_BORDER_WEIGHT
# 1/omega_size = ODDS TO GET A VERY HEAVY PACKAGE IN RANGE SECOND_BORDER_WEIGHT+1-4000
omega_size = 4
first_rand = randint(1, omega_size)
if first_rand == omega_size:
return randint(second_border+1, MAX_WEIGHT)
else:
return randint(MIN_WEIGHT, second_border)
def generate_category():
# QTY OF CATEGORIES
omega_size = 4
rand = randint(1, omega_size)
if rand == 1:
return DANGEROUS
elif rand == 2:
return GROCERIES
elif rand == 3:
return ELECTRONICS
elif rand == 4:
return BUILDERS
def generate_size():
second_border = SECOND_BORDER_SIZE
# 1/omega_size = ODDS TO GET A VERY BIG PACKAGE IN RANGE SECOND_BORDER_SIZE+1-10000
omega_size = 7
first_rand = randint(1, omega_size)
if first_rand == omega_size:
return randint(second_border+1, MAX_SIZE)
else:
return randint(MIN_SIZE, second_border)
def generate_flammability(category):
if category == GROCERIES:
return NO_FLAMMABILITY
elif category == DANGEROUS:
return randint(3, 5)
# 1/omega_size = ODDS TO GET A FLAMMABLE PACKAGE THAT'S NOT LABELED "DANGEROUS" IN RANGE 1-5
omega_size = 4
first_rand = randint(1, omega_size)
if first_rand == omega_size:
return randint(MIN_FLAMMABILITY, MAX_FLAMMABILITY)
else:
return NO_FLAMMABILITY
def generate_explosiveness(category):
if category == GROCERIES:
return NO_EXPLOSIVENESS
elif category == DANGEROUS:
return randint(2, 5)
# 1/omega_size = ODDS TO GET A EXPLOSIVE PACKAGE THAT'S NOT LABELED "DANGEROUS" IN RANGE 1-2
omega_size = 7
first_rand = randint(1, omega_size)
if first_rand == omega_size:
return randint(MIN_EXPLOSIVENESS, 2)
else:
return NO_EXPLOSIVENESS
def generate_price():
second_border = SECOND_BORDER_PRICE
# 1/omega_size = ODDS TO GET A VERY EXPENSIVE PACKAGE IN RANGE SECOND_BORDER_PRICE+1-100000
omega_size = 6
first_rand = randint(1, omega_size)
if first_rand == omega_size:
return randint(second_border+1, MAX_PRICE)
else:
return randint(MIN_PRICE, second_border)
if __name__ == "__main__":
generated_data = []
for case in range(QTY_OF_CASES):
data_row = []
data_row.append(generate_weight())
data_row.append(generate_category())
data_row.append(generate_size())
data_row.append(generate_flammability(data_row[1]))
data_row.append(generate_explosiveness(data_row[1]))
data_row.append(generate_price())
generated_data.append(data_row)
with open(OUTPUT_FILEPATH, "w") as open_file:
for row in generated_data:
for atribute in row:
open_file.write(str(atribute))
open_file.write(' ')
open_file.write('\n')