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,0 +2,1,7,2,1,10,0 +2,1,8,3,1,9,0 +2,1,9,4,1,8,0 +2,1,10,5,1,7,0 +2,1,1,1,1,2,0 +2,1,2,2,1,4,0 +2,1,3,3,1,6,0 +2,1,4,4,1,8,0 +2,1,5,5,2,10,0 +2,1,6,1,2,1,0 +2,1,7,2,2,3,0 +2,1,8,3,2,5,0 +2,1,9,4,2,7,0 +2,1,10,5,2,9,0 +3,2,1,1,2,2,1 +3,2,2,2,2,1,1 +3,2,3,3,2,4,2 +3,2,4,4,2,3,3 +3,2,5,5,2,6,4 +3,2,6,1,2,5,4 +3,2,7,2,2,8,3 +3,2,8,3,2,7,3 +3,2,9,4,2,9,4 +3,2,10,5,2,10,5 +3,2,1,1,2,7,1 +3,2,2,2,2,6,1 +3,2,3,3,2,4,3 +3,2,4,4,2,1,3 +3,2,5,5,2,2,4 +3,2,6,1,2,3,4 +3,2,7,2,2,9,3 +3,2,8,3,2,8,3 +3,2,9,4,2,5,5 +3,2,10,5,2,4,5 +3,2,1,1,2,1,1 +3,2,2,2,1,7,0 +3,2,3,3,1,9,0 +3,2,4,4,1,10,0 +3,2,5,5,1,3,0 +3,2,6,1,1,2,0 +3,2,7,2,1,5,0 +3,2,8,3,1,6,0 +3,2,9,4,1,8,0 +3,1,10,5,1,3,0 +3,1,1,1,1,1,0 +3,1,2,2,1,2,0 +3,1,3,3,1,6,0 +3,1,4,4,1,9,0 +3,1,5,5,1,7,0 +3,1,6,1,1,4,0 +3,1,7,2,1,3,0 +3,1,8,3,1,5,0 +3,1,9,4,1,10,0 +3,1,10,5,1,8,0 +3,1,1,1,2,2,0 +3,1,2,2,2,4,0 +3,1,3,3,2,6,0 +3,1,4,4,2,7,0 +3,1,5,5,2,1,0 +3,1,6,1,2,9,0 +3,1,7,2,2,3,0 +3,1,8,3,2,9,0 +3,1,9,4,2,9,0 +3,1,10,5,2,1,0 diff --git a/decision_tree/tree_as_txt.txt b/decision_tree/tree_as_txt.txt new file mode 100644 index 0000000..3d68d31 --- /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 0000000..b8b8619 Binary files /dev/null and b/decision_tree/tree_model differ 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