Merge pull request 'Dodanie drzewa decyzjnego v1' (#24) from decisionTree into master

Reviewed-on: #24
This commit is contained in:
Aliaksei Kudravets 2022-05-10 12:14:51 +02:00
commit 42d09e21f8
7 changed files with 213 additions and 12 deletions

View File

@ -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)

View File

@ -0,0 +1,101 @@
season,enough_space_in_trashma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-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

BIN
decision_tree/tree_model Normal file

Binary file not shown.

View File

@ -19,6 +19,7 @@ class Player(pg.sprite.Sprite):
self.pos = vec(x, y) self.pos = vec(x, y)
self.rot = 0 self.rot = 0
self.__rotation = a_star_utils.Rotation.RIGHT self.__rotation = a_star_utils.Rotation.RIGHT
self.mass = 0
def rotation(self) -> a_star_utils.Rotation: def rotation(self) -> a_star_utils.Rotation:
return self.__rotation return self.__rotation

37
main.py
View File

@ -10,9 +10,18 @@ from map import map
from map import map_utils from map import map_utils
from path_search_algorthms import bfs from path_search_algorthms import bfs
from path_search_algorthms import a_star, a_star_utils from path_search_algorthms import a_star, a_star_utils
from decision_tree import decisionTree
from game_objects import aiPlayer 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(): class Game():
def __init__(self): def __init__(self):
@ -52,7 +61,7 @@ class Game():
# print(path) # print(path)
realPath = [] realPath = []
nextNode = target_node nextNode = target_node
for i in range(len(path)-1, 0, -1): for i in range(len(path) - 1, 0, -1):
node = path[i] node = path[i]
if node[0] == nextNode: if node[0] == nextNode:
realPath.insert(0, node[0]) realPath.insert(0, node[0])
@ -72,15 +81,15 @@ class Game():
game_folder = path.dirname(__file__) game_folder = path.dirname(__file__)
img_folder = path.join(game_folder, 'resources/textures') 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.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.transform.scale(self.player_img, (PLAYER_WIDTH, PLAYER_HEIGHT))
def run(self): def run(self):
# game loop - set self.playing = False to end the game # game loop - set self.playing = False to end the game
self.playing = True self.playing = True
while self.playing: while self.playing:
self.dt = self.clock.tick(FPS) / 1000.0 self.dt = self.clock.tick(FPS) / 1000.0
self.events() self.events()
self.update() self.update()
self.draw() self.draw()
@ -96,21 +105,21 @@ class Game():
# pygame.display.update() # pygame.display.update()
def draw(self): def draw(self):
#display fps as window title # display fps as window title
pg.display.set_caption("{:.2f}".format(self.clock.get_fps())) 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.roadTiles, self.screen, self.camera)
map.render_tiles(self.wallTiles, self.screen, self.camera, self.debug_mode) map.render_tiles(self.wallTiles, self.screen, self.camera, self.debug_mode)
map.render_tiles(self.trashbinTiles, self.screen, self.camera) map.render_tiles(self.trashbinTiles, self.screen, self.camera)
#rerender additional sprites # rerender additional sprites
for sprite in self.agentSprites: for sprite in self.agentSprites:
self.screen.blit(sprite.image, self.camera.apply(sprite)) self.screen.blit(sprite.image, self.camera.apply(sprite))
if self.debug_mode: if self.debug_mode:
pg.draw.rect(self.screen, CYAN, self.camera.apply_rect(sprite.hit_rect), 1) pg.draw.rect(self.screen, CYAN, self.camera.apply_rect(sprite.hit_rect), 1)
#finally update screen # finally update screen
pg.display.flip() pg.display.flip()
def events(self): def events(self):
@ -126,7 +135,9 @@ class Game():
pos = pg.mouse.get_pos() pos = pg.mouse.get_pos()
offset_x, offset_y = self.camera.offset() offset_x, offset_y = self.camera.offset()
clicked_coords = [math.floor(pos[0] / TILESIZE) - offset_x, math.floor(pos[1] / TILESIZE) - offset_y] 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) print(actions)
if (actions != None): if (actions != None):
t = aiPlayer.aiPlayer(self.player, game=self) t = aiPlayer.aiPlayer(self.player, game=self)
@ -138,11 +149,13 @@ class Game():
def show_go_screen(self): def show_go_screen(self):
pass pass
# create the game object # create the game object
if __name__ == "__main__": if __name__ == "__main__":
g = Game() g = Game()
g.show_start_screen() g.show_start_screen()
printTree()
g.run() g.run()
g.show_go_screen() g.show_go_screen()

View File

@ -15,3 +15,7 @@ toml==0.10.2
tomli==2.0.1 tomli==2.0.1
typing_extensions==4.1.1 typing_extensions==4.1.1
wrapt==1.14.0 wrapt==1.14.0
joblib~=1.1.0
matplotlib~=3.5.1
pandas~=1.1.1
scikit-learn~=1.0.2