From 135ab3e9c63b38de0137e68da66f0c3878ee7476 Mon Sep 17 00:00:00 2001 From: egotd Date: Thu, 19 May 2022 12:26:48 +0200 Subject: [PATCH] Improve performance of decision tree --- classes/ai.py | 4 ++-- classes/bfs.py | 4 ++-- classes/decisionTrees.py | 9 ++++++--- classes/minesweeper.py | 6 +++--- main.py | 7 ++++++- 5 files changed, 19 insertions(+), 11 deletions(-) diff --git a/classes/ai.py b/classes/ai.py index 653ded6..07e2b98 100644 --- a/classes/ai.py +++ b/classes/ai.py @@ -29,7 +29,7 @@ class AI: pass #co ma zrobić przy każdym ruchu <------------------------- najważniejsze - def updateTile(self): + def updateTile(self, model): #aktualne pola (do debugu) sensor = self.saper.sensor() @@ -40,7 +40,7 @@ class AI: #podniesienie bomby jeśli jest jakaś na tym polu - self.saper.pick_up() + self.saper.pick_up(model) #poruszenie się if self.user_controlled: diff --git a/classes/bfs.py b/classes/bfs.py index fec5fc6..c1338a4 100644 --- a/classes/bfs.py +++ b/classes/bfs.py @@ -164,7 +164,7 @@ class BFS: # jesli tmp node to goaltest if tmp_node_position[:2] == goaltest: - print('Find') + print('Find\n') while tmp_node[1].get_parent() is not None: final_action_list.append(tmp_node[1].get_action()) @@ -177,7 +177,7 @@ class BFS: explored.append(tmp_node[1]) # add node to array of visited nodes neighbours_list_of_our_node = self.successor(tmp_node_position) # lista możliwych akcij - print(neighbours_list_of_our_node) + # print(neighbours_list_of_our_node) for node_ in neighbours_list_of_our_node: # node_ is tuple(action, [x, y, gdzie_patczy], cost) diff --git a/classes/decisionTrees.py b/classes/decisionTrees.py index bb95422..6adaecd 100644 --- a/classes/decisionTrees.py +++ b/classes/decisionTrees.py @@ -8,7 +8,11 @@ from numpy import random ############### class DecisionTrees: - def return_predict(self): + def create_model(self): + model = chef.fit(pd.read_csv("D:\\1 Python projects\Saper\data\db.txt"), {'algorithm': 'ID3'}) + return model + + def return_predict(self, mod): # read data df = pd.read_csv("D:\\1 Python projects\Saper\data\db.txt") @@ -24,7 +28,6 @@ class DecisionTrees: # ID3 config config = {'algorithm': 'ID3'} # create decision tree - model = chef.fit(df, config) # print predict # print(chef.predict(model, [1, 2022, 0, 0, 0, 10])) @@ -52,4 +55,4 @@ class DecisionTrees: cnt += 1 # return prediction - return chef.predict(model, mine_characteristics) + return chef.predict(mod, mine_characteristics) diff --git a/classes/minesweeper.py b/classes/minesweeper.py index 64197b9..cae0a8b 100644 --- a/classes/minesweeper.py +++ b/classes/minesweeper.py @@ -389,15 +389,15 @@ class Minesweeper: pygame.mixer.Channel(2).set_volume(0.5) pygame.mixer.Channel(2).play(pygame.mixer.Sound("assets/sounds/collision.wav")) - def pick_up(self): + def pick_up(self, model): if self.offset_x != 0 or self.offset_y != 0: return for mine in self.current_map.mines: if (self.position_x, self.position_y) == (mine.position_x, mine.position_y): tree = decisionTrees.DecisionTrees() - decision = tree.return_predict() - print("Decision : ", decision) + decision = tree.return_predict(model) + print("Decision : ", decision, "\n") self.current_map.mines.remove(mine) pygame.mixer.Channel(3).set_volume(0.7) diff --git a/main.py b/main.py index 028adbd..fcbd179 100644 --- a/main.py +++ b/main.py @@ -58,6 +58,11 @@ def main(): # główna pętla game_loop = True clock = pygame.time.Clock() + + # create decision tree + tree = decisionTrees.DecisionTrees() + model = tree.create_model() + while game_loop: # wdrożenie FPS, delta - czas od ostatniej klatki delta = clock.tick(FPS) @@ -66,7 +71,7 @@ def main(): AI.updateFPS() if saper.offset_x == 0 and saper.offset_y == 0: - AI.updateTile() + AI.updateTile(model) # narysowanie terenu i obiektów map.draw_tiles()