From ed19e41bc612246ff2a18faff529df24d66988a4 Mon Sep 17 00:00:00 2001 From: aliaksei-kudravets Date: Fri, 13 May 2022 08:31:40 +0200 Subject: [PATCH] add decision tree model to trashbins, which are generated on map --- .DS_Store | Bin 0 -> 6148 bytes .ipynb_checkpoints/Untitled-checkpoint.ipynb | 109 ++++++++++++ Untitled.ipynb | 126 ++++++++++++++ decision_tree/decisionTree.py | 21 +-- decision_tree/tree_as_txt.txt | 169 +++++++++---------- decision_tree/tree_model | Bin 7076 -> 5545 bytes game_objects/trashbin.py | 34 ++-- main.py | 81 ++++++--- map/map_utils.py | 5 +- 9 files changed, 404 insertions(+), 141 deletions(-) create mode 100644 .DS_Store create mode 100644 .ipynb_checkpoints/Untitled-checkpoint.ipynb create mode 100644 Untitled.ipynb diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..b920e7d38d3eadd1d146255d3fd7f08720c94faa GIT binary patch literal 6148 zcmeHK!AiqG5S?wSO(=yP6nYGJE!f&3ikA@U4;aydN^MNgV9b^#wTDv3S%1hc@q3)v z-HN5^!GlPdf!Q}ZJG0Ba4Lex?5Z)y005kx=LM1HO*nA;0PP!r`;~^AkjtpXukir1+ zm1uVSM+WHZ>JUN%w|I7+&u@&rD41rWAdvw^y#}Ks&hmEqO_a)&jm@f6wQAP2e=nzg zKFlYXHymG5?@Y=hSlGkhJdUP=#@4Y+^I@DuW0eqx5r$k{#Azs}o}8p%rgD8fVA+;E zXf$WD?m@fd^m?6n%b6YSk=@;&&uwdaXZPrI@Q^&F@>$a=@C#H@H@JWoG=A9OlRr)q znciX4VunQ~BQwAZFazt#fVuCi+PZF#_r(k_1HWc~?gs~z&^1_SR96Q!^!rHT6+#lU z=`BI%7<3I58gT?g=u||VD$El@=ydcuCeAfjXw>N-)XF%ISy`ADicqVg-%;TpT#ei^ z1I)lO17+Q9Q2jso{{6q4#64z!8CWX@M5XWdJ=~J1tt*?OTI- 6.50 -| | | |--- feature_0 <= 3.00 -| | | | |--- class: 1 -| | | |--- feature_0 > 3.00 -| | | | |--- class: 0 -| |--- feature_5 > 1.50 -| | |--- class: 0 -|--- feature_4 > 1.50 -| |--- feature_1 <= 1.50 -| | |--- feature_0 <= 3.50 -| | | |--- class: 0 -| | |--- feature_0 > 3.50 -| | | |--- class: 1 -| |--- feature_1 > 1.50 -| | |--- feature_0 <= 3.50 -| | | |--- feature_0 <= 1.50 -| | | | |--- feature_3 <= 2.50 +| | |--- feature_0 > 1.50 +| | | |--- feature_3 <= 3.50 +| | | | |--- feature_2 <= 2.50 | | | | | |--- class: 1 -| | | | |--- feature_3 > 2.50 -| | | | | |--- feature_3 <= 3.50 -| | | | | | |--- feature_5 <= 7.50 -| | | | | | | |--- feature_2 <= 5.50 -| | | | | | | | |--- class: 0 -| | | | | | | |--- feature_2 > 5.50 -| | | | | | | | |--- class: 1 -| | | | | | |--- feature_5 > 7.50 -| | | | | | | |--- class: 0 -| | | | | |--- feature_3 > 3.50 +| | | | |--- feature_2 > 2.50 +| | | | | |--- feature_4 <= 2.50 | | | | | | |--- class: 1 -| | | |--- feature_0 > 1.50 -| | | | |--- feature_3 <= 1.50 -| | | | | |--- feature_5 <= 2.50 -| | | | | | |--- feature_5 <= 1.50 -| | | | | | | |--- class: 1 -| | | | | | |--- feature_5 > 1.50 +| | | | | |--- feature_4 > 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 +| | | | | | | | | |--- class: 1 +| | | | | | | | |--- feature_4 > 2.50 +| | | | | | | | | |--- feature_2 <= 2.00 +| | | | | | | | | | |--- class: 1 +| | | | | | | | | |--- feature_2 > 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_2 <= 2.50 +| | | | | | | | | |--- class: 1 +| | | | | | | | |--- feature_2 > 2.50 +| | | | | | | | | |--- feature_4 <= 2.00 +| | | | | | | | | | |--- class: 1 +| | | | | | | | | |--- feature_4 > 2.00 +| | | | | | | | | | |--- class: 0 +| | | | | | | |--- feature_1 > 3.50 +| | | | | | | | |--- class: 0 +| | | | |--- feature_3 > 4.50 +| | | | | |--- class: 0 +| |--- feature_4 > 3.50 +| | |--- feature_2 <= 1.50 +| | | |--- feature_4 <= 4.50 +| | | | |--- feature_3 <= 3.50 +| | | | | |--- feature_0 <= 1.50 +| | | | | | |--- class: 0 +| | | | | |--- feature_0 > 1.50 +| | | | | | |--- class: 1 +| | | | |--- feature_3 > 3.50 +| | | | | |--- feature_1 <= 2.50 +| | | | | | |--- feature_3 <= 4.50 | | | | | | | |--- feature_0 <= 2.50 | | | | | | | | |--- class: 0 | | | | | | | |--- feature_0 > 2.50 | | | | | | | | |--- class: 1 -| | | | | |--- feature_5 > 2.50 -| | | | | | |--- class: 1 -| | | | |--- feature_3 > 1.50 -| | | | | |--- feature_2 <= 3.50 -| | | | | | |--- feature_0 <= 2.50 -| | | | | | | |--- feature_5 <= 2.00 -| | | | | | | | |--- class: 1 -| | | | | | | |--- feature_5 > 2.00 -| | | | | | | | |--- feature_5 <= 4.00 -| | | | | | | | | |--- class: 0 -| | | | | | | | |--- feature_5 > 4.00 -| | | | | | | | | |--- feature_3 <= 2.50 -| | | | | | | | | | |--- feature_5 <= 7.00 -| | | | | | | | | | | |--- class: 0 -| | | | | | | | | | |--- feature_5 > 7.00 -| | | | | | | | | | | |--- class: 1 -| | | | | | | | | |--- feature_3 > 2.50 -| | | | | | | | | | |--- class: 1 -| | | | | | |--- feature_0 > 2.50 -| | | | | | | |--- class: 1 -| | | | | |--- feature_2 > 3.50 -| | | | | | |--- feature_5 <= 1.50 +| | | | | | |--- feature_3 > 4.50 | | | | | | | |--- class: 0 -| | | | | | |--- feature_5 > 1.50 -| | | | | | | |--- feature_3 <= 2.50 -| | | | | | | | |--- feature_5 <= 5.00 -| | | | | | | | | |--- class: 1 -| | | | | | | | |--- feature_5 > 5.00 -| | | | | | | | | |--- class: 0 -| | | | | | | |--- feature_3 > 2.50 -| | | | | | | | |--- feature_5 <= 5.50 -| | | | | | | | | |--- feature_0 <= 2.50 -| | | | | | | | | | |--- feature_2 <= 4.50 -| | | | | | | | | | | |--- class: 0 -| | | | | | | | | | |--- feature_2 > 4.50 -| | | | | | | | | | | |--- class: 0 -| | | | | | | | | |--- feature_0 > 2.50 -| | | | | | | | | | |--- feature_2 <= 4.50 -| | | | | | | | | | | |--- class: 0 -| | | | | | | | | | |--- feature_2 > 4.50 -| | | | | | | | | | | |--- truncated branch of depth 2 -| | | | | | | | |--- feature_5 > 5.50 -| | | | | | | | | |--- feature_5 <= 6.50 -| | | | | | | | | | |--- class: 1 -| | | | | | | | | |--- feature_5 > 6.50 -| | | | | | | | | | |--- feature_2 <= 8.50 -| | | | | | | | | | | |--- truncated branch of depth 3 -| | | | | | | | | | |--- feature_2 > 8.50 -| | | | | | | | | | | |--- truncated branch of depth 4 -| | |--- feature_0 > 3.50 -| | | |--- class: 1 +| | | | | |--- feature_1 > 2.50 +| | | | | | |--- class: 0 +| | | |--- feature_4 > 4.50 +| | | | |--- class: 0 +| | |--- feature_2 > 1.50 +| | | |--- class: 0 +|--- feature_2 > 3.50 +| |--- feature_1 <= 1.50 +| | |--- feature_4 <= 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 +| | | | | |--- class: 0 +| | | |--- feature_2 > 4.50 +| | | | |--- class: 0 +| | |--- feature_4 > 1.50 +| | | |--- class: 0 +| |--- feature_1 > 1.50 +| | |--- class: 0 diff --git a/decision_tree/tree_model b/decision_tree/tree_model index ef984c8271342ae5cf51591f715e0fdd93474b7f..b28c9a370a1f75d6e941f38a4420a2c206a32645 100644 GIT binary patch literal 5545 zcmdT|U2GIp6rL?x*p>yC(n6IArBqn3uB8Gk6r8Ii6c|z|K`GH>nBAQ+qx;9~ELehC zO=ydmAPKk8^hFHDM`JW5ec+)H!rv1?#0L4Ph!4iZU;?HZ{JAst+-2?*l1ZZvP6*#U z=X~ehbI<)|rn9}l!SWKpxvZ-EYbjMRGBMLo)tH<}Dfzsb$JU(nYC_NJ+01t0Z?nC6 zx2_uaC|>$Degj9XvV@_VszDgo3hvf3I<`XjTuL{IlX&84-o#c}T6tYgsyS1`9oDS0 zo{{rPI+s%OvO~pEi4}2sNh#e}3Ru;)bDyg3)=atEP!c9tMs|Yjv~EGL!uH(Vs$v!l zQgDYg)3$AnoXI8~PuNg0$!uEAn~JFtMwLCQp3W6WV6}zT%whs%Fgt9~; zCo8F(rnpEmGsRV9G9Q*ot@4bVEtt82nI~qz3PBfTY|VZ(8&B!+Sf-H9^~gCrLAs5t zSsk|doM9+EI}9b4bN4lrFT|aEY?V4$*qW9}DmIri-zv>(N=_w>J4nV^BWX7~&_>2d zvxoS#8f_6_mFO+_&_F-lhNIFHE3g)KbaZU~n_SL+l3?PvRj#B`*?n?4t>o}PAV;ciRr&j;K|hQoBt#q3Nk4wx%17B*B^n z>LWTgH6lVSYjZ+OVb~&4acTh3V{0zew3?JX5!0!9gI3QUofe&VaFWo6wRWvj3K85$ zFhX#Xo!RX=^ao~E6Xs^EOR6O(k<2K;E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`$(Rfqx}Haeoit z?Ein>yS*QNzvX@(B`f&CjruR>^^pETEC1F*e+B8iia(8=TmRYX#tZ!NUk2pp6(>&l2K$M@6=q%6*E@>anA)*+f%*Q;=fu8+qP9!c-sd(Z7^0$;taDhX~>+bnw9r3ZOra0w41wpL;~wm;FF}cnBg&yZj3*+><9=g@9_^!FD35w@ANv&fs85#1eZ+h1BOmXFD4#<+9s7i9 u=#WRn(!MN*G$I~Dq(g8Dp2KQX(BS8F?SkpCf3?wn@5~qHiu>cRTKo@9Tn7dK diff --git a/game_objects/trashbin.py b/game_objects/trashbin.py index 9b8ea8b..f6f37da 100644 --- a/game_objects/trashbin.py +++ b/game_objects/trashbin.py @@ -1,23 +1,23 @@ import pygame as pg from enum import Enum - +from random import randrange from map.tile import Tile - -class Waste_Type(Enum): - BIO = 0 - GLASS = 1 - PLASTIC = 2 - PAPER = 3 - MIX = 4 - - def __int__(self): - return self.value class Trashbin(Tile): - def __init__(self, img, x, y, width, height, waste_type: Waste_Type): + def __init__(self, img, x, y, width, height, waste_type): super().__init__(img, x, y, width, height) + # dis_dump dis_trash mass space trash_mass trash_space + self.x = x + self.y = y - self.waste_type = waste_type - self.days_after_pickup = 0 - self.max_capacity = 100 - self.used_capacity = 0 - self.access = True + self.season = randrange(4) + self.trash_type = randrange(5) + self.mass = randrange(5) + self.space = randrange(5) + self.trash_mass = randrange(5) + + + def get_coords(self): + return (self.x, self.y) + + def get_attributes(self): + return (self.season, self.trash_type, self.mass, self.space, self.trash_mass) diff --git a/main.py b/main.py index e7c2476..fe582db 100644 --- a/main.py +++ b/main.py @@ -13,17 +13,22 @@ from path_search_algorthms import a_star, a_star_utils from decision_tree import decisionTree from game_objects import aiPlayer +import itertools -def printTree(): +def getTree(): tree = decisionTree.tree() decisionTree.tree_as_txt(tree) - decisionTree.tree_to_png(tree) + # decisionTree.tree_to_png(tree) decisionTree.tree_to_structure(tree) drzewo = decisionTree.tree_from_structure('./decision_tree/tree_model') - print("Dla losowych danych predykcja czy wziąć kosz to: ") - dec = decisionTree.decision(drzewo, 4, 2, 7, 4, 2, 3) - print(dec) + # print("Dla losowych danych predykcja czy wziąć kosz to: ") + # dec = decisionTree.decision(drzewo, *(4,1,1,1)) + # print('---') + # print(f"decision is{dec}") + # print('---') + + return drzewo class Game(): @@ -31,6 +36,7 @@ class Game(): def __init__(self): pg.init() self.clock = pg.time.Clock() + self.dt = self.clock.tick(FPS) / 333.0 self.screen = pg.display.set_mode((WIDTH, HEIGHT)) pg.display.set_caption("Trashmaster") self.load_data() @@ -38,8 +44,17 @@ class Game(): # because dont work without data.txt # self.init_bfs() # self.init_a_star() + self.t = aiPlayer.aiPlayer(self.player, game=self) - self.dt = self.clock.tick(FPS) / 1000.0 + + def get_actions_by_coords(self,x,y): + pos = (x,y) + 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) + return actions def init_game(self): # initialize all variables and do all the setup for a new game @@ -47,16 +62,15 @@ class Game(): # sprite groups and map array for calculations (self.roadTiles, self.wallTiles, self.trashbinTiles), self.mapArray = map.get_tiles() self.agentSprites = pg.sprite.Group() - # player obj self.player = Player(self, 32, 32) - # camera obj self.camera = map_utils.Camera(MAP_WIDTH_PX, MAP_HEIGHT_PX) # other self.debug_mode = False + def init_bfs(self): start_node = (0, 0) target_node = (18, 18) @@ -81,6 +95,38 @@ class Game(): path = a_star.search_path(start_x, start_y, target_x, target_y, self.mapArray) print(path) + def init_decision_tree(self): + # logika pracy z drzewem + self.positive_decision = [] + self.negative_decision = [] + + self.positive_actions = [] + self.negative_actions = [] + for i in self.trashbinTiles: + 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) + self.positive_actions.append(self.get_actions_by_coords(x, y)) + else: + self.negative_decision.append(i) + self.negative_actions.append(i) + + # j = 0 + # for i in self.positive_actions: + + # print(f"step {j} actions is : {i}") + # j+=1 + + # vec = pg.math.Vector2 + # for i in self.positive_actions: + # self.t.startAiController(i) + # self.player.pos = vec(32, 32) + + self.t.startAiController(self.positive_actions[0]) + + def load_data(self): game_folder = path.dirname(__file__) img_folder = path.join(game_folder, 'resources/textures') @@ -91,7 +137,7 @@ class Game(): def run(self): # game loop - set self.playing = False to end the game self.playing = True - + self.init_decision_tree() while self.playing: self.dt = self.clock.tick(FPS) / 1000.0 self.events() @@ -142,24 +188,17 @@ class Game(): 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) + # print(actions) + if (actions != None): - t = aiPlayer.aiPlayer(self.player, game=self) - t.startAiController(actions) - - def show_start_screen(self): - pass - - def show_go_screen(self): - pass + self.t.startAiController(actions) + # 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/map/map_utils.py b/map/map_utils.py index 1a32047..1904528 100644 --- a/map/map_utils.py +++ b/map/map_utils.py @@ -21,13 +21,15 @@ def generate_map(): map[y][x] = 1 # generowanie smietnikow - for i in range(0, 5): + for i in range(0, 10): x = random.randint(0, MAP_WIDTH-1) y = random.randint(0, MAP_HEIGHT-1) map[y][x] = 2 return map +trashbins =[] + # tworzenie grup sprite'ow def get_sprites(map, pattern): roadTiles = pg.sprite.Group() @@ -54,6 +56,7 @@ def get_sprites(map, pattern): trashbin = Trashbin(trashbin_pattern[trashbinId], offsetX, offsetY, 32, 30, trashbinId) roadTiles.add(tile) trashbinTiles.add(trashbin) + trashbins.append(trashbin) return roadTiles, wallTiles, trashbinTiles