From 8e331a88038023d9ae306ad77a6776b55378ae62 Mon Sep 17 00:00:00 2001 From: trzmielewskiR Date: Mon, 9 May 2022 19:18:24 +0200 Subject: [PATCH] Dodanie drzewa decyzjnego v1 --- decision_tree/decisionTree.py | 66 +++++++++++++++++++ decision_tree/drzewo_decyzyjne.csv | 101 +++++++++++++++++++++++++++++ decision_tree/tree_as_txt.txt | 16 +++++ decision_tree/tree_model | Bin 0 -> 2729 bytes game_objects/player.py | 1 + main.py | 37 +++++++---- requirements.txt | 4 ++ 7 files changed, 213 insertions(+), 12 deletions(-) create mode 100644 decision_tree/decisionTree.py create mode 100644 decision_tree/drzewo_decyzyjne.csv create mode 100644 decision_tree/tree_as_txt.txt create mode 100644 decision_tree/tree_model diff --git a/decision_tree/decisionTree.py b/decision_tree/decisionTree.py new file mode 100644 index 0000000..9ed19a7 --- /dev/null +++ b/decision_tree/decisionTree.py @@ -0,0 +1,66 @@ +import joblib +import matplotlib.pyplot as plt +import pandas +from sklearn.tree import DecisionTreeClassifier, export_text, plot_tree + +''' +atrybuty w pliku csv muszą być integerami, wstępnie ustaliłem: +season = {"wiosna": 1, "lato": 2, "jesien":3, "zima":4} +enough_space_in_trashmaster = { "no": 1, "yes":2} +time_since_flush = [1,2,3,4,5,6,7,8,9,10] +type_of_trash = {"bio":1, "szklo":2, "plastik":3, "papier":4, "mieszane":5} +access_to_bin = { "no":1, "yes":2} +distance = [1,2,3,4,5,6,7,8,9,10] +decision = [1,2,3,4,5] +''' +decisions = ["decision"] +attributes = ["season", "enough_space_in_trashmaster", "time_since_flush", "type_of_trash", "access_to_bin", "distance", + "decision"] + + +# return tree made from attributes +def tree(): + dataset = pandas.read_csv('./decision_tree/drzewo_decyzyjne.csv') + + x = dataset[attributes] + y = dataset[decisions] + decision_tree = DecisionTreeClassifier() + decision_tree = decision_tree.fit(x, y) + + return decision_tree + + +# return decision made from tree and attributes +def decision(decision_tree, season, enough_space_in_trashmaster, time_since_flush, type_of_trash, access_to_bin, + distance): + decision = decision_tree.predict( + [[season, enough_space_in_trashmaster, time_since_flush, type_of_trash, access_to_bin, distance]]) + + return decision + + +''' +we shall save output of our decision tree. It is possible for a few ways: +txt, png or structure +''' + + +def tree_as_txt(decision_tree): + with open('./decision_tree/tree_as_txt.txt', "w") as file: + file.write(export_text(decision_tree)) + + +def tree_to_png(decision_tree): + plt.figure() + plot_tree(decision_tree, feature_names=attributes, filled=True) + plt.title("Decision tree") + plt.show() + + +def tree_to_structure(decision_tree): + joblib.dump(decision_tree, './decision_tree/tree_model') + +#drzewo = tree() +#tree_as_txt(drzewo) +#tree_to_png(drzewo) +#tree_to_structure(drzewo) diff --git a/decision_tree/drzewo_decyzyjne.csv b/decision_tree/drzewo_decyzyjne.csv new file mode 100644 index 0000000..e5b77f5 --- /dev/null +++ b/decision_tree/drzewo_decyzyjne.csv @@ -0,0 +1,101 @@ +season,enough_space_in_trashmaster,time_since_flush,type_of_trash,access_to_bin,distance,decision +2,2,1,1,2,10,1 +2,2,2,2,2,8,1 +2,2,3,3,2,6,1 +2,2,4,4,2,4,2 +2,2,5,5,2,2,3 +2,2,6,1,2,1,5 +2,2,7,2,2,3,4 +2,2,8,3,2,5,4 +2,2,9,4,2,7,3 +2,2,10,5,2,9,5 +2,2,1,1,2,2,1 +2,2,2,2,2,1,1 +2,2,3,3,2,3,2 +2,2,4,4,2,4,2 +2,2,5,5,2,5,3 +2,2,6,1,2,6,3 +2,2,7,2,2,7,2 +2,2,8,3,2,8,2 +2,2,9,4,2,9,4 +2,2,10,5,2,10,4 +2,2,1,1,2,7,1 +2,2,2,2,2,6,2 +2,2,3,3,2,5,3 +2,2,4,4,1,4,0 +2,2,5,5,1,3,0 +2,2,6,1,1,2,0 +2,2,7,2,1,1,0 +2,2,8,3,1,9,0 +2,2,9,4,1,8,0 +2,2,10,5,1,7,0 +2,2,1,1,1,3,0 +2,2,2,2,1,2,0 +2,2,3,3,1,1,0 +2,2,4,4,1,4,0 +2,1,5,5,1,5,0 +2,1,6,1,1,6,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/dev/null +++ b/decision_tree/tree_as_txt.txt @@ -0,0 +1,16 @@ +|--- feature_6 <= 0.50 +| |--- class: 0 +|--- feature_6 > 0.50 +| |--- feature_6 <= 1.50 +| | |--- class: 1 +| |--- feature_6 > 1.50 +| | |--- feature_6 <= 3.50 +| | | |--- feature_6 <= 2.50 +| | | | |--- class: 2 +| | | |--- feature_6 > 2.50 +| | | | |--- class: 3 +| | |--- feature_6 > 3.50 +| | | |--- feature_6 <= 4.50 +| | | | |--- class: 4 +| | | |--- feature_6 > 4.50 +| | | | |--- class: 5 diff --git a/decision_tree/tree_model b/decision_tree/tree_model new file mode 100644 index 0000000000000000000000000000000000000000..b8b86191f004fdf8dfbccdcfb91ff75fcb405c35 GIT binary patch literal 2729 zcmb7G&1)n@6z|E`OeP=Zd$Vg~vss9>n+@Lv$G9@LAVC;x(g2SG%^n|e}TbyvrkM#xx)e)V4c-bdB@m^m0Z z{i9^0ODx9kc?3s(okxV!k>lYwCb1MtFB8X&-N3&O@+~EGJ1&Xj$MVL9@&j2D<4)vq z5&=Srkv-RUrKrTA=W-BnJ9V!VsrM{N>vDw|@q>-s3h-OwX3N}aBzdt!&kGo4MR*S+Z}>=5<$RCF|BxoL4MFqC6f{M+d&t_9CHGQ zMP=3Ph6zNz4`DeG!7-6NCe!O{$DlCoI3dDbNO2EnR{KOg?!raDq!jb-2DazgbwBBb z`zUlBcvLB7n~HrYitzr!2!~MC_RzQX7jbbVWl6d!KE##*z zAt!ZOcY=u2yNSo$)O8g_#0mVEM~TC$%*aqx9axo>EaQ+3Imm$*cH%1W)uzgZTdJIF zHqsgc>S6Bsx7p|s8{1*!V^-N=<29=UcVSg+Fug^UO|*spzrD#OA$~Ts!=~W|sjVmM znc^+jjD51pW^1XB7%~_5Y_9bz7$AO->`^Ew>=2a?9^sgFaSWBn=36s>;BJ?o*!AJO z<0Ua=3#}<7hPOmPCsSaHsSG;~iDSeAWV=3FYE=N&cARTfnr1qlxm9wSxR72RP-(xt4E=w-S>pR>jeCA~^l={0&?47q(7eX)>H z12Rf)JWcG(t6}J7Hq>jXHJlM+MAK1CPe0Kc71enlV(~v~L5i{1!5&sUPdBzIDz$3K zBd9bn)!Th=v$7af69jr;u79E^Da->kM!=9&E?m2Ge(jC)O1YQQb$uzTq$(RG4js2f zAZ{o)D8Iz#(wczQNn5>ObK>0Qbus+lg}eWD2YtR-6IG9NIC7}#wWXMd(jlX{vhHn| zT%5vKp@G6w@|hu3g3}1}7?a{sHl;`#6;5~>^ETbEj3Y^32U-F82GALxJ1Wbao-ETl z{q?SLY%K%wO}cBX0=*BKHK1<+f9psl)+V&nOkN4a3do-^OW6;Nhwps;)vura*l1@0 z{e9b1W1@Hcr5O$QI)`uOo0Q%&ixt-I@6X0}?w1d~pZ>XVvrq4pf&ESN>8+c21*>jU ztKZ+G`97NVqh(zlDCj7^_UB6Y|kcnOY E2QC8WbpQYW literal 0 HcmV?d00001 diff --git a/game_objects/player.py b/game_objects/player.py index ce88e6d..984ec48 100644 --- a/game_objects/player.py +++ b/game_objects/player.py @@ -19,6 +19,7 @@ class Player(pg.sprite.Sprite): self.pos = vec(x, y) self.rot = 0 self.__rotation = a_star_utils.Rotation.RIGHT + self.mass = 0 def rotation(self) -> a_star_utils.Rotation: return self.__rotation diff --git a/main.py b/main.py index 598f5b4..aa5d4f3 100644 --- a/main.py +++ b/main.py @@ -10,9 +10,18 @@ from map import map from map import map_utils from path_search_algorthms import bfs from path_search_algorthms import a_star, a_star_utils - +from decision_tree import decisionTree from game_objects import aiPlayer + + +def printTree(): + tree = decisionTree.tree() + decisionTree.tree_as_txt(tree) + decisionTree.tree_to_png(tree) + decisionTree.tree_to_structure(tree) + + class Game(): def __init__(self): @@ -52,7 +61,7 @@ class Game(): # print(path) realPath = [] nextNode = target_node - for i in range(len(path)-1, 0, -1): + for i in range(len(path) - 1, 0, -1): node = path[i] if node[0] == nextNode: realPath.insert(0, node[0]) @@ -72,15 +81,15 @@ class Game(): game_folder = path.dirname(__file__) img_folder = path.join(game_folder, 'resources/textures') - self.player_img = pg.image.load(path.join(img_folder,PLAYER_IMG)).convert_alpha() - self.player_img = pg.transform.scale(self.player_img, (PLAYER_WIDTH,PLAYER_HEIGHT) ) - + self.player_img = pg.image.load(path.join(img_folder, PLAYER_IMG)).convert_alpha() + self.player_img = pg.transform.scale(self.player_img, (PLAYER_WIDTH, PLAYER_HEIGHT)) + def run(self): # game loop - set self.playing = False to end the game self.playing = True while self.playing: - self.dt = self.clock.tick(FPS) / 1000.0 + self.dt = self.clock.tick(FPS) / 1000.0 self.events() self.update() self.draw() @@ -96,20 +105,20 @@ class Game(): # pygame.display.update() def draw(self): - #display fps as window title + # display fps as window title pg.display.set_caption("{:.2f}".format(self.clock.get_fps())) - #rerender map + # rerender map map.render_tiles(self.roadTiles, self.screen, self.camera) map.render_tiles(self.wallTiles, self.screen, self.camera, self.debug_mode) - #rerender additional sprites + # rerender additional sprites for sprite in self.agentSprites: self.screen.blit(sprite.image, self.camera.apply(sprite)) if self.debug_mode: pg.draw.rect(self.screen, CYAN, self.camera.apply_rect(sprite.hit_rect), 1) - - #finally update screen + + # finally update screen pg.display.flip() def events(self): @@ -125,7 +134,9 @@ class Game(): pos = pg.mouse.get_pos() offset_x, offset_y = self.camera.offset() clicked_coords = [math.floor(pos[0] / TILESIZE) - offset_x, math.floor(pos[1] / TILESIZE) - offset_y] - actions = a_star.search_path(math.floor(self.player.pos[0] / TILESIZE), math.floor(self.player.pos[1] / TILESIZE), self.player.rotation(), clicked_coords[0], clicked_coords[1], self.mapArray) + actions = a_star.search_path(math.floor(self.player.pos[0] / TILESIZE), + math.floor(self.player.pos[1] / TILESIZE), self.player.rotation(), + clicked_coords[0], clicked_coords[1], self.mapArray) print(actions) if (actions != None): t = aiPlayer.aiPlayer(self.player, game=self) @@ -137,11 +148,13 @@ class Game(): def show_go_screen(self): pass + # create the game object if __name__ == "__main__": g = Game() g.show_start_screen() + printTree() g.run() g.show_go_screen() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index a3b0748..c2cc61c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -15,3 +15,7 @@ toml==0.10.2 tomli==2.0.1 typing_extensions==4.1.1 wrapt==1.14.0 +joblib~=1.1.0 +matplotlib~=3.5.1 +pandas~=1.1.1 +scikit-learn~=1.0.2 \ No newline at end of file