From fc16e616d417e18c7fbdcb01fa333864ad6badbe Mon Sep 17 00:00:00 2001 From: trzmielewskiR Date: Fri, 10 Jun 2022 01:06:03 +0200 Subject: [PATCH] updated main, repaired astar, added method for get actions from coords, expected to be used in GA --- NeuralNetwork/prediction.py | 2 +- data_structures/__init__.py | 0 decision_tree/tree_as_txt.txt | 62 ++++++------- decision_tree/tree_model | Bin 5511 -> 5511 bytes last_map.nparr | Bin 0 -> 896 bytes main.py | 100 +++++++++------------ main_test.py | 10 +++ path_search_algorthms/a_star.py | 63 +++---------- path_search_algorthms/a_star_controller.py | 26 ++++++ path_search_algorthms/a_star_utils.py | 31 ++++--- 10 files changed, 141 insertions(+), 153 deletions(-) create mode 100644 data_structures/__init__.py create mode 100644 last_map.nparr create mode 100644 main_test.py create mode 100644 path_search_algorthms/a_star_controller.py diff --git a/NeuralNetwork/prediction.py b/NeuralNetwork/prediction.py index 94c2c6b..5f810cb 100644 --- a/NeuralNetwork/prediction.py +++ b/NeuralNetwork/prediction.py @@ -12,7 +12,7 @@ def getPrediction(img_path, network_name): img = Image.open(img_path) transform_tensor = transforms.ToTensor()(img).unsqueeze_(0) classes = ['glass', 'metal', 'paper', 'plastic'] - neural_net.load_state_dict(torch.load(PATH + network_name)) + neural_net.load_state_dict(torch.load(PATH + network_name, map_location='cpu')) neural_net.eval() outputs = neural_net(transform_tensor) diff --git a/data_structures/__init__.py b/data_structures/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/decision_tree/tree_as_txt.txt b/decision_tree/tree_as_txt.txt index aaa701a..2383fa8 100644 --- a/decision_tree/tree_as_txt.txt +++ b/decision_tree/tree_as_txt.txt @@ -4,40 +4,18 @@ | | | |--- class: 0 | | |--- feature_0 > 1.50 | | | |--- feature_3 <= 3.50 -| | | | |--- feature_2 <= 2.50 +| | | | |--- feature_4 <= 2.50 | | | | | |--- class: 1 -| | | | |--- feature_2 > 2.50 -| | | | | |--- feature_4 <= 2.50 +| | | | |--- feature_4 > 2.50 +| | | | | |--- feature_2 <= 2.50 | | | | | | |--- class: 1 -| | | | | |--- feature_4 > 2.50 +| | | | | |--- feature_2 > 2.50 | | | | | | |--- class: 0 | | | |--- feature_3 > 3.50 | | | | |--- feature_3 <= 4.50 | | | | | |--- feature_1 <= 2.50 | | | | | | |--- feature_0 <= 2.50 | | | | | | | |--- feature_1 <= 1.50 -| | | | | | | | |--- feature_2 <= 2.50 -| | | | | | | | | |--- class: 1 -| | | | | | | | |--- feature_2 > 2.50 -| | | | | | | | | |--- feature_4 <= 2.00 -| | | | | | | | | | |--- class: 1 -| | | | | | | | | |--- feature_4 > 2.00 -| | | | | | | | | | |--- class: 0 -| | | | | | | |--- feature_1 > 1.50 -| | | | | | | | |--- class: 0 -| | | | | | |--- feature_0 > 2.50 -| | | | | | | |--- feature_4 <= 2.50 -| | | | | | | | |--- class: 1 -| | | | | | | |--- feature_4 > 2.50 -| | | | | | | | |--- feature_2 <= 2.50 -| | | | | | | | | |--- class: 1 -| | | | | | | | |--- feature_2 > 2.50 -| | | | | | | | | |--- class: 0 -| | | | | |--- feature_1 > 2.50 -| | | | | | |--- feature_0 <= 3.50 -| | | | | | | |--- class: 0 -| | | | | | |--- feature_0 > 3.50 -| | | | | | | |--- feature_1 <= 3.50 | | | | | | | | |--- feature_4 <= 2.50 | | | | | | | | | |--- class: 1 | | | | | | | | |--- feature_4 > 2.50 @@ -45,8 +23,30 @@ | | | | | | | | | | |--- class: 1 | | | | | | | | | |--- feature_2 > 2.00 | | | | | | | | | | |--- class: 0 -| | | | | | | |--- feature_1 > 3.50 +| | | | | | | |--- feature_1 > 1.50 | | | | | | | | |--- class: 0 +| | | | | | |--- feature_0 > 2.50 +| | | | | | | |--- feature_2 <= 2.50 +| | | | | | | | |--- class: 1 +| | | | | | | |--- feature_2 > 2.50 +| | | | | | | | |--- feature_4 <= 2.50 +| | | | | | | | | |--- class: 1 +| | | | | | | | |--- feature_4 > 2.50 +| | | | | | | | | |--- class: 0 +| | | | | |--- feature_1 > 2.50 +| | | | | | |--- feature_1 <= 3.50 +| | | | | | | |--- feature_0 <= 3.50 +| | | | | | | | |--- class: 0 +| | | | | | | |--- feature_0 > 3.50 +| | | | | | | | |--- feature_4 <= 2.50 +| | | | | | | | | |--- class: 1 +| | | | | | | | |--- feature_4 > 2.50 +| | | | | | | | | |--- feature_2 <= 2.00 +| | | | | | | | | | |--- class: 1 +| | | | | | | | | |--- feature_2 > 2.00 +| | | | | | | | | | |--- class: 0 +| | | | | | |--- feature_1 > 3.50 +| | | | | | | |--- class: 0 | | | | |--- feature_3 > 4.50 | | | | | |--- class: 0 | |--- feature_4 > 3.50 @@ -73,8 +73,8 @@ | | |--- feature_2 > 1.50 | | | |--- class: 0 |--- feature_2 > 3.50 -| |--- feature_4 <= 1.50 -| | |--- feature_1 <= 1.50 +| |--- feature_1 <= 1.50 +| | |--- feature_4 <= 1.50 | | | |--- feature_2 <= 4.50 | | | | |--- feature_0 <= 1.50 | | | | | |--- class: 0 @@ -85,7 +85,7 @@ | | | | | | |--- class: 0 | | | |--- feature_2 > 4.50 | | | | |--- class: 0 -| | |--- feature_1 > 1.50 +| | |--- feature_4 > 1.50 | | | |--- class: 0 -| |--- feature_4 > 1.50 +| |--- feature_1 > 1.50 | | |--- class: 0 diff --git a/decision_tree/tree_model b/decision_tree/tree_model index bf4e0f6daad5d9a9e4a959444ffa735d29956628..66b76f8f490893d8d9c98e52c158a09f414c10a5 100644 GIT binary patch delta 161 zcmZqIZr9$R$;!wwS&KEFk!kZR)^^6phFl4pf(#HKGj!`s6@X4Sr6T2$)vdtia{SIQb^0z~(~k3CxU)lbQI< r8Cf=4@&96;>>@0{q~tJpo{-q&8X*rx>&;7rZ!@wR=ow8ZP0|AZ`5q&% delta 165 zcmZqIZr9$R$;!wyS&KEFk!ABN)^Faym^?l+VR}I{j15zd%tvN} O 0): - # node = search_list[0] - - # # find cheapest node in search_list - # for i in range(1, len(search_list)): - # if (search_list[i].f_cost() <= node.f_cost()): - # if(search_list[i].h_cost < node.h_cost): - # node = search_list[i] - - # search_list.remove(node) - # searched_list.append((node.x, node.y)) - - # # check for target node - # if ((node.x, node.y) == (target_x, target_y)): - # return trace_path(node) - - # # neightbours processing - # neighbours = utils.get_neighbours(node, searched_list, array) - # for neighbour in neighbours: - - # # calculate new g cost for neightbour (start -> node -> neightbour) - # new_neighbour_cost = node.g_cost + utils.get_neighbour_cost(node, neighbour) - - # if (new_neighbour_cost < neighbour.g_cost or neighbour not in search_list): - - # # replace cost and set parent node - # neighbour.g_cost = new_neighbour_cost - # neighbour.h_cost = utils.get_h_cost(neighbour, target_node) - # neighbour.parent = node - - # # add to search - # if(neighbour not in search_list): - # search_list.append(neighbour) def trace_path(end_node: utils.Node): path = [] @@ -91,9 +55,11 @@ def trace_path(end_node: utils.Node): # set final rotation of end_node because we don't do it before node.rotation = utils.get_needed_rotation(node.parent, node) - while (node.parent != 0): - move = utils.get_move(node.parent, node) - path += move + while (node.parent != False): + if (node.parent == utils.Rotation.NONE): + path += "forward" + else: + path += utils.get_move(node.parent, node) node = node.parent # delete move on initial tile @@ -107,6 +73,3 @@ def trace_path(end_node: utils.Node): return path - - - \ No newline at end of file diff --git a/path_search_algorthms/a_star_controller.py b/path_search_algorthms/a_star_controller.py new file mode 100644 index 0000000..ee1294f --- /dev/null +++ b/path_search_algorthms/a_star_controller.py @@ -0,0 +1,26 @@ +import math +from path_search_algorthms import a_star +from settings import * + +def get_actions_by_coords(x, y, game): + # print('get_actions_by_coords') + # print(x, y, x/TILESIZE, y/TILESIZE) + offset_x, offset_y = game.camera.offset() + # print('offset ' + str(self.camera.offset())) + clicked_coords = [math.floor(x / TILESIZE) - offset_x, math.floor(y / TILESIZE) - offset_y] + # print(self.player.pos[0], self.player.pos[1], clicked_coords) + actions = a_star.search_path(math.floor(game.player.pos[0] / TILESIZE), + math.floor(game.player.pos[1] / TILESIZE), game.player.rotation(), + clicked_coords[0], clicked_coords[1], game.mapArray) + return actions + +def get_actions_for_target_coords(x, y, game): + actions = a_star.search_path( + math.floor(game.player.pos[0] / TILESIZE), + math.floor(game.player.pos[1] / TILESIZE), + game.player.rotation(), + x / TILESIZE, + y / TILESIZE, + game.mapArray + ) + return actions \ No newline at end of file diff --git a/path_search_algorthms/a_star_utils.py b/path_search_algorthms/a_star_utils.py index ee24f61..628676c 100644 --- a/path_search_algorthms/a_star_utils.py +++ b/path_search_algorthms/a_star_utils.py @@ -3,6 +3,7 @@ from enum import Enum from map import map_utils from settings import * + class Rotation(Enum): UP = 0 RIGHT = 1 @@ -13,34 +14,37 @@ class Rotation(Enum): def __int__(self): return self.value + class Node: def __init__(self, x: int, y: int, rotation: Rotation): self.x = x self.y = y self.g_cost = 0 self.h_cost = 0 - self.parent = 0 - self.rotation = rotation - + self.parent: Node | bool = False + self.rotation: Rotation = rotation + def f_cost(self): return self.g_cost + self.h_cost + def get_neighbours(node, searched_list, array): neighbours = [] - for offset_x in range (-1, 2): - for offset_y in range (-1, 2): + for offset_x in range(-1, 2): + for offset_y in range(-1, 2): # don't look for cross neighbours - if(abs(offset_x) + abs(offset_y) == 1): + if (abs(offset_x) + abs(offset_y) == 1): x = node.x + offset_x y = node.y + offset_y # prevent out of map coords if (x >= 0 and x < MAP_WIDTH and y >= 0 and y < MAP_HEIGHT): - if(map_utils.isRoadTile(array[y][x]) and (x, y) not in searched_list): + if (map_utils.isRoadTile(array[y][x]) and (x, y) not in searched_list): neighbour = Node(x, y, Rotation.NONE) neighbour.rotation = get_needed_rotation(node, neighbour) neighbours.append(neighbour) return neighbours + # move cost schema: # - move from tile to tile: 10 # - add extra 10 (1 rotation) if it exists @@ -49,11 +53,12 @@ def get_h_cost(start_node: Node, target_node: Node): distance_y = abs(start_node.y - target_node.y) cost = (distance_x + distance_y) * 10 - if(distance_x > 0 and distance_y > 0): + if (distance_x > 0 and distance_y > 0): cost += 10 - + return cost + # move cost schema: # - move from tile to tile: 10 # - every rotation 90*: 10 @@ -67,6 +72,7 @@ def get_neighbour_cost(start_node: Node, target_node: Node) -> int: else: return 30 + # translate rotation change to move def get_move(start_node: Node, target_node: Node): rotate_change = get_rotate_change(start_node.rotation, target_node.rotation) @@ -79,10 +85,12 @@ def get_move(start_node: Node, target_node: Node): else: return ["left", "forward"] + # simple calc func def get_rotate_change(rotationA: Rotation, rotationB: Rotation) -> int: return int(rotationA) - int(rotationB) + # get new rotation for target_node as neighbour of start_node def get_needed_rotation(start_node: Node, target_node: Node) -> Rotation: if (start_node.x - target_node.x > 0): @@ -92,10 +100,9 @@ def get_needed_rotation(start_node: Node, target_node: Node) -> Rotation: if (start_node.y - target_node.y > 0): return Rotation.UP if (start_node.y - target_node.y < 0): - return Rotation.DOWN - + return Rotation.DOWN + - \ No newline at end of file