From 5c4a536c5a8cbc7aeb57bbe6f38a83be72ee21a6 Mon Sep 17 00:00:00 2001 From: aliaksei-kudravets Date: Thu, 26 May 2022 11:08:48 +0200 Subject: [PATCH 1/2] add backpropTrainer model with controled data set --- neurone/main.py | 71 ++++++++++++++++++++++++++++++++++++++++++++++ neurone/model.txt | Bin 0 -> 3478 bytes 2 files changed, 71 insertions(+) create mode 100644 neurone/main.py create mode 100644 neurone/model.txt diff --git a/neurone/main.py b/neurone/main.py new file mode 100644 index 0000000..64b5d2c --- /dev/null +++ b/neurone/main.py @@ -0,0 +1,71 @@ +import pybrain3 +import pickle +import matplotlib.pylab as plt +from numpy import ravel +from pybrain3.tools.shortcuts import buildNetwork +from pybrain3.datasets import SupervisedDataSet +from pybrain3.supervised.trainers import BackpropTrainer +from pybrain3.tools.xml.networkwriter import NetworkWriter +from pybrain3.tools.xml.networkreader import NetworkReader + +# https://www.machinelearningmastery.ru/how-to-configure-the-number-of-layers-and-nodes-in-a-neural-network/ +class SupervisedDataSetModel(): + def __init__(self, metrics:int = 4, + predictions:int = 1, + input_layer:int = 4, + hidden_layer:int = 3, + output_layer:int = 1): + # 4 метрики, 1 предикшн + self.metrics = metrics + self.predictions = predictions + self.input_layer = input_layer + self.hidden_layer = hidden_layer + self.output_layer = output_layer + self.ds = SupervisedDataSet(metrics, predictions) + + + def activateModel(self): + self.net = buildNetwork(self.input_layer, self.hidden_layer, self.output_layer, bias=True) + self.trainer = BackpropTrainer(self.net, dataset=self.ds, momentum=0.1, learningrate=0.01, verbose=True, weightdecay=0.01) + + self.trnerr, self.valerr = self.trainer.trainUntilConvergence() + + plt.plot(self.trnerr, 'b', self.valerr, 'r') + plt.show() + + def addDataToModel(self, input:list, target:list): + self.ds.addSample(inp=input, target=target) + + def predict(self, data:list): + y = self.net.activate(data) + print(y) + return y + + def saveModel(self): + fileObject = open('model.txt', 'wb') + pickle.dump(self.net, fileObject) + fileObject.close() + + +def getModel(): + fileObject = open('model.txt', 'rb') + net2 = pickle.load(fileObject) + fileObject.close() + return net2 + +model = SupervisedDataSetModel() + +# model.addDataToModel([2, 3, 80, 1], [5]) +# model.addDataToModel([5, 5, 50, 2], [4]) +# model.addDataToModel([10, 7, 40, 3], [3]) +# model.addDataToModel([15, 9, 20, 4], [2]) +# model.addDataToModel([20, 11, 10, 5], [1]) + +# model.activateModel() + +# model.saveModel() + +# USE MODEL - > +model = getModel() +print(model.activate([2, 3, 80, 1])) + diff --git a/neurone/model.txt b/neurone/model.txt new file mode 100644 index 0000000000000000000000000000000000000000..64f96946e2fe0cec6240475cc79911009fc77abd GIT binary patch literal 3478 zcmcInZ%iCT6u;{pJ^t2HXedE!BMpTQJ!^~D80s}b5W9q^)NiH7-t8We|EvaD!32o~6e zf{iLsEv`)1sgz=*RnsDauw9j^Wo?TKi6}fpa3zx{hM^l=P_5fmo=z1PttctDOx5^4 zcs)$=S5%KTYIY_&7ES5^ZpKbqs$>|_7&sp{l%%e479DwVO;$@_7LVJ4t_k|GvNA#~LwUk;GKjrbx%l;fdMq z`A2s3&OI>IF@LP>jcw|o&UpgUr~x&j7P=E^&E01QSaPNmw#>4zl;y?O4$oebcXmpZ zubd30FYjqRed^N3HA8!tWm753!ri!pS=NaH;G0UBiUsi43EbjCVvjCqRZ}FxWlL2b z8~Kw(fFmQQP7G3#^rMZy3{)=$0qq9XHnO&fwhG!T2GJIH>iwdiHh^4t=49*FGrtRa zi(~9*dhr&=)YHsa(-%WeGiOZ*3*F1WYAyj*LX}KgQ!Rqa?}68c=Yi;uRAB5j*!ODPb7_(J{^pn4GB7JdlPy}ZBG?c3cxQi2+}D{y6ETDZ50#IrHU5j&Y^0 zH`DNv^YDR#lUHvG%%f-j{Qm6hV$6B7qWArgB`4Y2^v;vt{t!EFzWm1xU;OAies1&R zV>^F}9cizEx`ds#`V(I?unzkKy6<3HYjB(qyF)Dg*2N1&Qc zf@d-vm-4Lm!Tzh6aaX&br}V&O`ltpK8wqwoLgFAnI;F*od;bbg@D76B6$5Avo^z1A z&covXi{_EIoP1f8cZKvAf@q--{ve0HyjI=1l8ir?L?0Gc^`d69{)Bp5!q1L;KLyuM zN*7urmgz#BXLJ$#{1mvKo9Y*SYgqdAbLZ?S{mQG07n~zQ3$K4R^PR(-@%P&^_59B3 z$5-M|Py~=K4to3QU<4QRhzq*F(zy#d6H?Gi26I`s(ACoXP3*4Ici(?|_hRhgrD09k zfAv2E!z%_m6v@VLh22yGNd`?z>=v(*h!VarqgzNZR%H`V#p_L;nkM~@qLv@Uiymc) z9m)TFqt}3*=XxFm@`vO3f;Eo4b&B-`%Q7nR${F;lue7MUpIE-1R=E}FIyKGrvo?2y c0tc4`%+rz|9&7s)!;!fA?^8U!>LWJ#H&-QRL;wH) literal 0 HcmV?d00001 From fc16e616d417e18c7fbdcb01fa333864ad6badbe Mon Sep 17 00:00:00 2001 From: trzmielewskiR Date: Fri, 10 Jun 2022 01:06:03 +0200 Subject: [PATCH 2/2] 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