From 79463c2044e2b6f84cd6983ba73ac9af745db849 Mon Sep 17 00:00:00 2001 From: SaluSL Date: Tue, 18 May 2021 22:28:32 +0200 Subject: [PATCH 1/3] cleaning the code --- src/a_star.py | 131 +++++++++++++++++++++++++ src/bfs.py | 113 ++++++++++++++++++++++ src/main.py | 261 ++------------------------------------------------ 3 files changed, 252 insertions(+), 253 deletions(-) create mode 100644 src/a_star.py create mode 100644 src/bfs.py diff --git a/src/a_star.py b/src/a_star.py new file mode 100644 index 0000000..2f58549 --- /dev/null +++ b/src/a_star.py @@ -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 diff --git a/src/bfs.py b/src/bfs.py new file mode 100644 index 0000000..884de0f --- /dev/null +++ b/src/bfs.py @@ -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 diff --git a/src/main.py b/src/main.py index 77123c0..d1b16fe 100644 --- a/src/main.py +++ b/src/main.py @@ -2,6 +2,9 @@ import pygame import random import time +from src.a_star import a_star_search, WAREHOUSE_MAP +from src.bfs import breadth_first_search + pygame.init() WIDTH = 675 @@ -23,19 +26,6 @@ def get_position_from_pix(pix_pos): print("ERROR: THERE IS NO POSITION LIKE THIS (get_position_from_pix)") -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 create_positions(): size_agent = 40 x_position_pix = size_agent @@ -57,239 +47,6 @@ def generate_package(a, b): return p1 -# 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, 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 agent.goal_test(elem.state): - # 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) - - -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 - - - -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 - - class Package: def __init__(self, pos, content, content_size, pack_image): self.pos = pos @@ -414,7 +171,7 @@ board = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA) # transparently surfac create_positions() -#Rysowanie lini +# Rysowanie lini for x in range(9): for y in range(9): pygame.draw.rect(board, (0, 0, 0), (x * size, y * size, size, size), 3) @@ -442,15 +199,15 @@ Package_list = [ 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) print(agent.path) - -Stain_list = [] # Pętla służąca do tworzenia plam oleju na podstawie mapy magazynu +Stain_list = [] + for index_x in range(9): for index_y in range(9): if WAREHOUSE_MAP[index_x][index_y] == 10: @@ -466,8 +223,6 @@ while running: if event.type == pygame.KEYDOWN: pass - - if len(Package_list) < QTY_OF_PACKAGES: is_dock_empty = True @@ -491,7 +246,7 @@ while running: agent.goal = coord_goals.pop(0) 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.agent_direction) for package in Package_list: From db41867ceb84205a9adc712ad6d2c071f56a15d6 Mon Sep 17 00:00:00 2001 From: SaluSL Date: Wed, 19 May 2021 23:34:55 +0200 Subject: [PATCH 2/3] dodane wyrzucanie paczek i generowanie danych --- src/main.py | 10 +- .../scripts/data/training_list_id3.txt | 216 ++++++++++++++++++ src/training_data/scripts/gen_data_id3.py | 136 +++++++++++ 3 files changed, 359 insertions(+), 3 deletions(-) create mode 100644 src/training_data/scripts/data/training_list_id3.txt create mode 100644 src/training_data/scripts/gen_data_id3.py diff --git a/src/main.py b/src/main.py index d1b16fe..349dc1a 100644 --- a/src/main.py +++ b/src/main.py @@ -187,7 +187,7 @@ Shelf_list = [ Shelf((5, 8), 'Electronic', 5), Shelf((6, 8), 'Electronic', 5), Shelf((7, 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)] screen = pygame.display.set_mode([WIDTH, HEIGHT]) @@ -203,7 +203,7 @@ print(a_star_search((6, 8), (0, 0), "down")) agent.path = a_star_search((agent.pos_coord[1], agent.pos_coord[0]), (0, 0), agent.agent_direction) -print(agent.path) +#print(agent.path) # Pętla służąca do tworzenia plam oleju na podstawie mapy magazynu Stain_list = [] @@ -230,13 +230,16 @@ while running: for package in Package_list: if package.pos == (40, 40): 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: Package_list.append(generate_package(40, 40)) # PATHING if not agent.goal_achieved: - print(agent.path) + # print(agent.path) agent.move_bfs() else: @@ -257,6 +260,7 @@ while running: # DRAWING screen.blit(BACKGROUND, [0, 0]) screen.blit(DOCK, [0, 0]) + screen.blit(pygame.transform.flip(DOCK, True, False), [8*size, 0]) screen.blit(board, board.get_rect()) for shelf in Shelf_list: screen.blit(shelf.image, shelf.rect) diff --git a/src/training_data/scripts/data/training_list_id3.txt b/src/training_data/scripts/data/training_list_id3.txt new file mode 100644 index 0000000..3a6d212 --- /dev/null +++ b/src/training_data/scripts/data/training_list_id3.txt @@ -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 +4 dangerous 5 3 2 2 +2 electronics 1 0 0 4 +8 dangerous 7 5 4 7 +3 electronics 7 1 0 4 +4 electronics 7 4 0 3 +4 builders 6 0 0 2 +1 groceries 2 0 0 5 +3 electronics 7 5 1 9 +8 electronics 3 0 0 6 +3 electronics 2 0 0 4 +1 builders 7 0 0 5 +4 groceries 2 0 0 10 +1 dangerous 1 5 2 2 +10 electronics 7 0 2 5 +9 electronics 3 5 0 1 +1 groceries 3 0 0 1 +5 groceries 4 0 0 8 +4 groceries 1 0 0 1 +10 builders 2 0 0 6 +2 dangerous 9 4 3 10 +9 groceries 2 0 0 5 +2 electronics 1 0 0 1 +5 dangerous 5 4 2 3 +2 electronics 8 3 0 1 +9 dangerous 2 5 4 5 +8 electronics 5 0 0 3 +9 dangerous 3 3 4 1 +7 dangerous 6 3 3 2 +1 builders 2 4 0 5 +3 groceries 3 0 0 10 +3 electronics 2 0 0 2 +5 builders 7 0 1 1 +4 groceries 4 0 0 4 +8 dangerous 9 4 3 10 +2 groceries 3 0 0 3 +3 groceries 6 0 0 2 +4 groceries 3 0 0 5 +7 electronics 6 5 0 3 +3 electronics 2 0 0 10 +# 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 diff --git a/src/training_data/scripts/gen_data_id3.py b/src/training_data/scripts/gen_data_id3.py new file mode 100644 index 0000000..20d7bd8 --- /dev/null +++ b/src/training_data/scripts/gen_data_id3.py @@ -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') + + From ccb3484759e45e0e6bccbea81bb7c5364c89dfbc Mon Sep 17 00:00:00 2001 From: Jerzy Kwiatkowski Date: Fri, 21 May 2021 20:23:16 +0200 Subject: [PATCH 3/3] Zaktualizuj 'src/training_data/scripts/data/training_list_id3.txt' --- .../scripts/data/training_list_id3.txt | 80 +++++++++---------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/src/training_data/scripts/data/training_list_id3.txt b/src/training_data/scripts/data/training_list_id3.txt index 3a6d212..9fb08c9 100644 --- a/src/training_data/scripts/data/training_list_id3.txt +++ b/src/training_data/scripts/data/training_list_id3.txt @@ -45,46 +45,46 @@ # JERZY \/ #|WAGA|TYP|WYMIAR|LATWOPALNOSC|WYBUCHOWOSC|CENA|PRZYJETY_DO_MAGAZYNU # -10 builders 1 0 0 5 -4 dangerous 5 3 2 2 -2 electronics 1 0 0 4 -8 dangerous 7 5 4 7 -3 electronics 7 1 0 4 -4 electronics 7 4 0 3 -4 builders 6 0 0 2 -1 groceries 2 0 0 5 -3 electronics 7 5 1 9 -8 electronics 3 0 0 6 -3 electronics 2 0 0 4 -1 builders 7 0 0 5 -4 groceries 2 0 0 10 -1 dangerous 1 5 2 2 -10 electronics 7 0 2 5 -9 electronics 3 5 0 1 -1 groceries 3 0 0 1 -5 groceries 4 0 0 8 -4 groceries 1 0 0 1 -10 builders 2 0 0 6 -2 dangerous 9 4 3 10 -9 groceries 2 0 0 5 -2 electronics 1 0 0 1 -5 dangerous 5 4 2 3 -2 electronics 8 3 0 1 -9 dangerous 2 5 4 5 -8 electronics 5 0 0 3 -9 dangerous 3 3 4 1 -7 dangerous 6 3 3 2 -1 builders 2 4 0 5 -3 groceries 3 0 0 10 -3 electronics 2 0 0 2 -5 builders 7 0 1 1 -4 groceries 4 0 0 4 -8 dangerous 9 4 3 10 -2 groceries 3 0 0 3 -3 groceries 6 0 0 2 -4 groceries 3 0 0 5 -7 electronics 6 5 0 3 -3 electronics 2 0 0 10 +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 #