From 2908d62fb329d2d2f41c0b41e8228a316d0c4cb3 Mon Sep 17 00:00:00 2001 From: Kacper Date: Fri, 10 Jun 2022 03:16:43 +0200 Subject: [PATCH] tsp v4 --- NeuralNetwork/prediction.py | 2 +- decision_tree/tree_as_txt.txt | 44 +++++++++++++------------- decision_tree/tree_model | Bin 5511 -> 5511 bytes last_map.nparr | Bin 896 -> 896 bytes main.py | 34 ++++++++++++++------ map/map_utils.py | 2 +- path_search_algorthms/a_star_utils.py | 4 ++- 7 files changed, 51 insertions(+), 35 deletions(-) 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/decision_tree/tree_as_txt.txt b/decision_tree/tree_as_txt.txt index 9b5c043..0bc5127 100644 --- a/decision_tree/tree_as_txt.txt +++ b/decision_tree/tree_as_txt.txt @@ -4,46 +4,46 @@ | | | |--- 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_4 <= 2.50 +| | | | | | | | |--- feature_2 <= 2.50 | | | | | | | | | |--- class: 1 -| | | | | | | | |--- feature_4 > 2.50 -| | | | | | | | | |--- feature_2 <= 2.00 +| | | | | | | | |--- feature_2 > 2.50 +| | | | | | | | | |--- feature_4 <= 2.00 | | | | | | | | | | |--- class: 1 -| | | | | | | | | |--- feature_2 > 2.00 +| | | | | | | | | |--- feature_4 > 2.00 | | | | | | | | | | |--- class: 0 | | | | | | | |--- feature_1 > 1.50 | | | | | | | | |--- class: 0 | | | | | | |--- feature_0 > 2.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_1 > 2.50 | | | | | | |--- feature_1 <= 3.50 | | | | | | | |--- feature_0 <= 3.50 | | | | | | | | |--- class: 0 | | | | | | | |--- feature_0 > 3.50 -| | | | | | | | |--- feature_4 <= 2.50 +| | | | | | | | |--- feature_2 <= 2.50 | | | | | | | | | |--- class: 1 -| | | | | | | | |--- feature_4 > 2.50 -| | | | | | | | | |--- feature_2 <= 2.00 +| | | | | | | | |--- feature_2 > 2.50 +| | | | | | | | | |--- feature_4 <= 2.00 | | | | | | | | | | |--- class: 1 -| | | | | | | | | |--- feature_2 > 2.00 +| | | | | | | | | |--- feature_4 > 2.00 | | | | | | | | | | |--- class: 0 | | | | | | |--- feature_1 > 3.50 | | | | | | | |--- class: 0 @@ -76,13 +76,13 @@ | |--- feature_4 <= 1.50 | | |--- feature_1 <= 1.50 | | | |--- feature_2 <= 4.50 -| | | | |--- feature_3 <= 4.50 -| | | | | |--- feature_0 <= 1.50 -| | | | | | |--- class: 0 -| | | | | |--- feature_0 > 1.50 -| | | | | | |--- class: 1 -| | | | |--- feature_3 > 4.50 +| | | | |--- feature_0 <= 1.50 | | | | | |--- class: 0 +| | | | |--- feature_0 > 1.50 +| | | | | |--- feature_3 <= 4.50 +| | | | | | |--- class: 1 +| | | | | |--- feature_3 > 4.50 +| | | | | | |--- class: 0 | | | |--- feature_2 > 4.50 | | | | |--- class: 0 | | |--- feature_1 > 1.50 diff --git a/decision_tree/tree_model b/decision_tree/tree_model index c9ec1be792e54f0351a46d1c12ee958766401369..6097820680e728428f7731b00b6e068f6bf71930 100644 GIT binary patch delta 138 zcmZqIZr9$R$;!wwS&KEFk!kZR)^M;!hjNh2<( zu? delta 156 zcmZqIZr9$R$;!wyS&KEFk!ABN)^M;!hjNh2<( z>u`%19>GTFXT)ki{F7xwvjH(eA-@0s diff --git a/last_map.nparr b/last_map.nparr index c7394756c0d409040b7c20f2f772aef006ad9e13..c7831e435df3745723623537a5b9c6c15af1dd79 100644 GIT binary patch delta 136 zcmZo*Z(yI$z{oi9qx!@LEE5l~Ok5%|@qxg^2`oTzvIC>a#0eZg)+8pGNz4M1A25kb zzQDvW@d8kR%wz#(mdSURWF~&#U}T(pkVzP*d-4QEgULFKAR|DUCoxJ){9phv25jI4 VMxb()i5~>OhzD%g35|&pL;#elGm!uQ delta 163 zcmW-Zu?@mN3`Nh*p`^ItB(6viDJPKv2?|D_U=bt+IX1BMn~Vb+kPV=xU<7|P&HvNX3bBrtR*3y{*u|Btk6MklSvp0}z pJZ^MU2^FbCTOE-V55@vs#BFrP)JFMp(GoJTX2z{s9~#E*}5@ diff --git a/main.py b/main.py index eb50486..b196ec0 100644 --- a/main.py +++ b/main.py @@ -101,10 +101,10 @@ class Game(): atrrs_container = i.get_attributes() x, y = i.get_coords() dec = decisionTree.decision(getTree(), *atrrs_container) - if dec[0] == 1: - self.positive_decision.append(i) - else: - self.negative_decision.append(i) + # if dec[0] == 1: + self.positive_decision.append(i) # zmiana po to by losowaƂo wszystkie smietniki a nie poprawne tylko, zeby ladniej bylo widac algorytm genetyczny + # else: + # self.negative_decision.append(i) print('positive actions') print(len(self.positive_decision)) @@ -114,12 +114,26 @@ class Game(): # print(i) # print('----') def decsion_tree_move(self): - - for i in self.positive_decision: + + for i in range(0,len(self.positive_decision)): # print(i.get_coords()) print('action') - trash_x, trash_y = i.get_coords() + + + # trash_x, trash_y = i.get_coords() + + # for ii in self.tsp_list: + temp_tsp = str(self.tsp_list[i]) + temp_tsp = temp_tsp.strip("()") + temp_tsp = temp_tsp.split(",") + trash_x = int(temp_tsp[0]) + trash_y = int(temp_tsp[1]) + + + print(trash_x, trash_y) + action = a_star_controller.get_actions_for_target_coords(trash_x, trash_y, self) + print(action) self.t.startAiController(action) @@ -127,7 +141,7 @@ class Game(): print('--rozpoczecie sortowania smietnika--') dir = "./resources/trash_dataset/test/all" files = os.listdir(dir) - for i in range(0, 10): + for j in range(0, 10): random = randint(0, 48) file = files[random] result = prediction.getPrediction(dir + '/' + file, 'trained_nn_20.pth') @@ -155,8 +169,8 @@ class Game(): # dist = a_star.get_cost - tsp_list = TSP.geneticAlgorithmPlot(population=city_list, popSize=100, eliteSize=20, mutationRate=0.01, generations=200) - print(tsp_list) + self.tsp_list = TSP.geneticAlgorithmPlot(population=city_list, popSize=100, eliteSize=20, mutationRate=0.01, generations=200) + print(self.tsp_list) def load_data(self): game_folder = os.path.dirname(__file__) diff --git a/map/map_utils.py b/map/map_utils.py index e3da2c7..6d4a583 100644 --- a/map/map_utils.py +++ b/map/map_utils.py @@ -21,7 +21,7 @@ def generate_map(): map[y][x] = 1 # generowanie smietnikow - for i in range(0, 30): + for i in range(0, 20): x = random.randint(0, MAP_WIDTH-1) y = random.randint(0, MAP_HEIGHT-1) map[y][x] = 2 diff --git a/path_search_algorthms/a_star_utils.py b/path_search_algorthms/a_star_utils.py index ee24f61..8ac26b9 100644 --- a/path_search_algorthms/a_star_utils.py +++ b/path_search_algorthms/a_star_utils.py @@ -84,7 +84,9 @@ 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: +def get_needed_rotation(start_node: Node or bool, target_node: Node) -> Rotation: + if(start_node == False): + return target_node.rotation if (start_node.x - target_node.x > 0): return Rotation.LEFT if (start_node.x - target_node.x < 0):